动态查找之二叉树查找 c++实现
算法思想
二叉搜索树(又称二叉查找树或二叉排序树)BST树
二叉查找树
二叉查找树,也称二叉搜索树,或二叉排序树。其定义也比较简单,要么是一颗空树,要么就是具有如下性质的二叉树:
(1)若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
(2) 若任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
(3) 任意节点的左、右子树也分别为二叉查找树;
(4) 没有键值相等的节点。
二叉查找树的性质总结:
a.二叉查找树还有一个性质,即对二叉查找树进行中序遍历,即可得到有序的数列。
b.二叉查找树的查询复杂度,和二分查找一样,插入和查找的时间复杂度均为 O(logn) ,但是在最坏的情况下仍然会有 O(n) 的时间复杂度。原因在于插入和删除元素的时候,树没有保持平衡。
具体实现:
1 /**** 2 * BinarySortTree. 3 * 4 ****/ 5 #include"stdafx.h" 6 #include <iostream> 7 #include<queue> 8 using namespace std; 9 typedef struct node 10 { 11 int elem; 12 struct node* leftchild; 13 struct node* rightchild; 14 }BitNode,*BinTree; 15 //insert binary tree function 16 BinTree Insert_BinaryTree(BinTree &bt,int key) 17 { 18 if (bt == 0) 19 { 20 bt = new BitNode; 21 bt->elem = key; 22 bt->leftchild = 0; 23 bt->rightchild = 0; 24 return bt; 25 } 26 if (key < bt->elem) 27 { 28 bt->leftchild = Insert_BinaryTree(bt->leftchild,key); 29 } 30 else 31 { 32 bt->rightchild = Insert_BinaryTree(bt->rightchild, key); 33 } 34 return bt; 35 } 36 //for one search binary tree function 37 int Search_BinaryTree(BinTree &bt,int key) 38 { 39 if (bt == 0) return 0; 40 if (bt->elem == key) return 1; 41 42 if (key < bt->elem) 43 { 44 return Search_BinaryTree(bt->leftchild,key); 45 } 46 else 47 { 48 return Search_BinaryTree(bt->rightchild, key); 49 } 50 } 51 // for another one search binary tree function 52 int Search_BinaryTree(BinTree &bt, int key, BitNode ** p, BitNode** pf) 53 { 54 *p = bt; 55 *pf = 0; 56 while (*p != 0) 57 { 58 if ((*p)->elem == key) 59 return 1; 60 if ((*p)->elem > key) 61 { 62 *pf =*p; 63 *p = (*p)->leftchild; 64 } 65 else 66 { 67 *pf = *p; 68 *p = (*p)->rightchild; 69 } 70 } 71 return 0; 72 } 73 //delete binary tree function 74 int Delete_BinaryTree(BinTree *bt,int key) 75 { 76 BitNode *p=*bt; 77 BitNode *pf=0; 78 int findflag; 79 if (bt == 0) return 0; 80 findflag = Search_BinaryTree(*bt,key,&p,&pf); 81 if (findflag == 0) return 0; 82 //删除的节点是叶子节点 83 if (p->leftchild == 0 && p->rightchild == 0) 84 { 85 if (pf == 0) 86 { 87 delete bt; 88 bt = 0; 89 return 1; 90 } 91 if (p == pf->leftchild) 92 { 93 pf->leftchild = 0; 94 delete p; 95 p = 0; 96 return 1; 97 } 98 else 99 { 100 pf->rightchild = 0; 101 delete p; 102 p = 0; 103 return 1; 104 } 105 } 106 //删除的节点只有一个子节点 107 if (p->leftchild == 0) 108 { 109 if (pf = 0) 110 { 111 *bt = p->rightchild; 112 delete p; 113 return 1; 114 } 115 if(p==pf->leftchild) 116 { 117 pf->leftchild = p->rightchild; 118 delete p; 119 return 1; 120 } 121 else 122 { 123 pf->rightchild = p->rightchild; 124 delete p; 125 return 1; 126 } 127 } 128 129 if (p->rightchild == 0) 130 { 131 if (p == pf->leftchild) 132 { 133 pf->leftchild = p->leftchild; 134 delete p; 135 return 1; 136 } 137 if (p == pf->rightchild) 138 { 139 pf->rightchild = p->leftchild; 140 delete p; 141 return 1; 142 } 143 } 144 //3.删除的节点含有两个子节点 145 BitNode * prf = p; 146 BitNode * pr = p->rightchild; 147 while (pr->leftchild != 0) 148 { 149 prf = pr; 150 pr = pr->leftchild; 151 } 152 if(prf == p) 153 { 154 p->elem = pr->elem; 155 prf->rightchild = pr->rightchild; 156 } 157 else 158 { 159 p->elem = pr->elem; 160 prf->leftchild = pr->rightchild; 161 } 162 delete pr; 163 return 1; 164 165 } 166 167 //print binary tree function 168 void printTree(BitNode * bt) 169 { 170 queue<BitNode*> q; 171 q.push(bt); 172 while (!q.empty()) 173 { 174 BitNode* p = q.front(); q.pop(); 175 if (p) 176 { 177 cout << p->elem << "->"; 178 q.push(p->leftchild); 179 q.push(p->rightchild); 180 } 181 } 182 cout << endl; 183 } 184 //test function 185 int main() 186 { 187 int a[10] = { 12, 52, 65, 84, 63, 14, 68, 69, 99,77 }; 188 189 // initialization and creat the Binary Sort Tree. 190 BinTree bt = 0; 191 for (int i = 0; i < 10; i++) 192 { 193 bt = Insert_BinaryTree(bt, a[i]); 194 } 195 printTree(bt); 196 //search start. 197 cout << Search_BinaryTree(bt, 14) << endl; 198 cout << Search_BinaryTree(bt, 55) << endl; 199 200 //delete start. 201 cout << Delete_BinaryTree(&bt, 14) << endl; 202 203 //search 14 again. 204 cout << Search_BinaryTree(bt, 14) << endl; 205 printTree(bt); 206 system("pause"); 207 return 0; 208 }