基于C语言实现二叉搜索树

二叉搜索树的特点是左子树<根节点<右子树,并且任何节点的左右子树的高度差不超过1,搜索二叉树与满二叉树无关,只要满足上面的条件即可
1.初始化操作
此时传入的为根节点的指针的指针
 40 //初始化
 41 void SearchTreeInit(SearchNode** pRoot)
 42 {
 43     if(pRoot == NULL)
 44     {
 45         //非法操作
 46         return ;
 47     }
 48     *pRoot = NULL;
 49     return ;
 50 }

2.销毁二叉搜索树
第一种方法是传入二叉树的根节点的指针,第二种是传入二叉树的根节点的指针的指针
 52 //销毁
 53 void DestroySearchNode(SearchNode* node)
 54 {
 55     free(node);
 56 }
 57 //方法1
 58 void SearchTreeDestroy(SearchNode* root)
 59 {
 60     if(root == NULL)
 61     {
 62         return ;
 63     }
 64     SearchTreeDestroy(root->lchild);
 65     SearchTreeDestroy(root->rchild);
 66     DestroySearchNode(root);
 67 }
 68 //方法2
 69 void SearchTreeDestroy2(SearchNode** pRoot)
 70 {
 71     if(pRoot == NULL)
 72     {
 73         //非法操作
 74         return ;
 75     }
 76     if(*pRoot == NULL)
 77     {
 78         return ;
 79     }
 80     SearchNode* node = *pRoot;
 81     SearchTreeDestroy2(&node->lchild);
 82     SearchTreeDestroy2(&node->rchild);
 83     DestroySearchNode(node);
 84     *pRoot = NULL;
 85     return ;                                                                                                
 86 }

3.向二叉搜索树中插入元素
若二叉树为空,则将元素直接插入到根节点的位置即可,否则就采用递归地方式,按照二叉搜索树的定义将元素插入到二叉搜索树中
 98 void SearchTreeInsert(SearchNode** pRoot,SearchNodeType to_insert)
 99 {
100     if(pRoot == NULL)
101     {
102         //非法操作
103         return ;
104     }
105     if(*pRoot == NULL)
106     {
107         //空树,插到root位置
108         SearchNode* new_node = CreateSearchNode(to_insert);
109         *pRoot = new_node;
110         return ;
111     }
112     //对于树非空的情况
113     //采用递归的方式进行插入
114     SearchNode* cur = *pRoot;
115     if(to_insert < cur->data)
116     {
117         //递归地往左子树中插入
118         SearchTreeInsert(&cur->lchild,to_insert);                                                           
119     }
120     else if(to_insert > cur->data)
121     {
122         //递归地往右子树中插入
123         SearchTreeInsert(&cur->rchild,to_insert);
124     }
125     else
126     {
127         //等于的情况,采用下面的约定方式
128         //约定二叉搜索树中所有的元素不能重复,
129         return ;//表示不做任何动作,插入失败
130         //也可以有其他的约定方式:将该元素放到相等元素的左子树的最右边,或者右子树的最左边
131     }
132 }                                                                                                           
133 
 89 SearchNode* CreateSearchNode(SearchNodeType value)
 90 {
 91     SearchNode* new_node = (SearchNode*)malloc(sizeof(SearchNode));
 92     new_node->data=value;
 93     new_node->lchild=NULL;
 94     new_node->rchild=NULL;
 95     return new_node;
 96 }

4.使用递归的方法查找搜索二叉树中的元素
134 //递归的方式查找元素
135 SearchNode* SearchTreeFind(SearchNode* root,SearchNodeType to_find)
136 {
137     if(root == NULL)
138     {
139         return NULL;
140     }
141     if(to_find < root->data)
142     {
143         //递归地查找左子树
144         return SearchTreeFind(root->lchild,to_find);
145     }
146     else if(to_find > root->data)
147     {
148         //递归地查找右子树
149         return SearchTreeFind(root->rchild,to_find);
150     }
151     else                                                                                                    
152     {
153         //相等,找到了
154         return root;
155     }
156 }

猜你喜欢

转载自blog.csdn.net/l_x_y_hh/article/details/80300200