算法思想node
二叉搜索樹(又稱二叉查找樹或二叉排序樹)BST樹ios
二叉查找樹算法
二叉查找樹,也稱二叉搜索樹,或二叉排序樹。其定義也比較簡單,要麼是一顆空樹,要麼就是具備以下性質的二叉樹:spa
(1)若任意節點的左子樹不空,則左子樹上全部結點的值均小於它的根結點的值;code
(2) 若任意節點的右子樹不空,則右子樹上全部結點的值均大於它的根結點的值;blog
(3) 任意節點的左、右子樹也分別爲二叉查找樹;排序
(4) 沒有鍵值相等的節點。it
二叉查找樹的性質總結: io
a.二叉查找樹還有一個性質,即對二叉查找樹進行中序遍歷,便可獲得有序的數列。function
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 }