Binary search tree (binary search tree)

Binary search tree (binary search tree)
Each node 1. The tree has a unique keyword
2. If the left subtree, all the left sub-tree value is less than the root keyword keywords
3. If the right subtree, right subtree value of all keywords than keyword root of
about 4. sub-trees are binary search tree


1. Job:
Y = (S *) -> RChild
the right of FIG. Analysis
Code

2. binary search test code

Copy the code
  //BinarySearchTree.cpp. 1 
  2 
  . 3 #include <the iostream> 
  . 4 the using namespace STD; 
  . 5 
  . 6 the Node typedef struct 
  . 7 { 
  . 8 int Data; 
  . 9 struct LChild the Node *; 
 10 * struct the Node RChild; 
 . 11} BITREE, * LPBITREE; 
 12 is int BSTInsert (LPBITREE T *, int X) 
 13 is { 
 14 LPBITREE P, CUR, parent = NULL; 
 15 * CUR = T; T is the second pointer //   
 16 the while (CUR = NULL!) 
 . 17 { 
 18 is inserted // found position 
 19 // if present in the tree 
 20 is IF (cur> Data == X) 
 21 is { 
 22 is return 0; 
 23 is} 
 24 = CUR parent;
 @ 25 is larger than the value of the current node ---> right subtree 
 26 is IF (X> cur> Data) 
 27 { 
 28 CUR = cur> RChild; 
 29} 
 30 // smaller than the current value of the node --- > left subtree 
 31 is the else 
 32 { 
 33 is CUR = cur> LChild; 
 34 is} 
 35} 
 space 36 // newly inserted node 
 37 [P = new new BITREE; 
 38 is // determines whether a failure memory 
 39 iF (! P) 
 40 { 
 41 is Exit (0); 
 42 is} 
 43 is substantially // initializes the data member 
 44 is p-> data = X; 
 45 p-> LChild = NULL; 
 46 is p-> RChild = NULL; 
 proper inserted position 47 // 
 48 / / not enter parent = NULL cur = NULL is inserted in the root node 
 49 IF (! parent) 
 50 { 
 51 is P = T *; 
 52 is}
 53     //如果关键字小于父结点  当作parent->LChild
 54     else if (x < parent->data)
 55         parent->LChild = p;
 56     else
 57         parent->RChild = p;
 58     return 1;
 59 }
 60 
 61 void MidOrderTraverse(LPBITREE T)
 62 {
 63     if (T)
 64     {
 65         MidOrderTraverse(T->LChild);
 66         cout << T->data <<"-->";
 67         MidOrderTraverse(T->RChild);
 68     }
 69 }
 70 
 71 LPBITREE BSTSearch(LPBITREE T, int x)
 72 {
 73     LPBITREE p;
 74     if (T != NULL)
 75     {
 P = T 76; 
 77         while (p != NULL)
 78 { 
 79 IF (p-> Data == X) 
 80 { 
 81 return P; 
 82} 
 83 the else IF (p-> Data> X) 
 84 { 
 85 P = p-> LChild; 
 86} 
 87 IF the else (p-> Data <X) 
 88 { 
 89 P = p-> RChild; 
 90} 
 91 is} 
 92} 
 93 return NULL; 
 94} 
 95 DeleteNode void (* LPBITREE S) 
 96 { 
 97 LPBITREE Q, X, Y; 
 98 is a left subtree // right subtree to null can take up 
 99 IF ((S *) -!> LChild) 
100 {
101 q = * s; // save the original and then the first release 
102 * S = (S *) -> RChild; 
103 Delete Q; 
104} 
105 the else IF (! (S *) -> RChild) 
106 { 
107 * Q = S; 
108 = S * (S *) -> LChild; 
109 Delete Q; 
110} 
111 to the right of the else // job analysis 
112 { 
113 * X = S; 
114 Y = (S *) -> LChild; 
115 the while (Y-> RChild) 
1 16 { 
117 X = Y; 
1 18 Y = Y-> RChild; 
119} 
120 (S *) -> Data = Y-> Data; 
121 IF (X == (* S)) 
122 { 
X-123> LChild = Y-> LChild; 
124}
125         else
126         {
127             x->RChild = y->LChild;
128         }
129         delete y; //注意空间释放哪一个
130     }
131 }
132 
133 int BSTDelete(LPBITREE *T, int x)
134 {
135     if (!*T)
136     {
137         return 0;
138     }
139     else
140     {
141         if (x == (*T)->data)
142         {
143             DeleteNode(T);
144         }
145         else if ((*T)->data > x)
146         {
147             BSTDelete(&(*T)->LChild, x);
148         }
149         else if ((*T)->data < x)
150         {
151             BSTDelete(&(*T)->RChild, x);
152         }
153         return 1; 
154     }
155 }
156 
157 int main()
158 {
159     int Array[] = { 89, 23, 47, 58, 25, 78, 100, 29, 68, 38 };
160     int ArrayNum = sizeof(Array) / sizeof(Array[0]);
161     LPBITREE T = NULL;
162     for (int i = 0; i < ArrayNum; i++)
163     {
164         BSTInsert(&T, Array[i]);
165     }
166     cout << "中序遍历:" << endl;
167     MidOrderTraverse(T);
168     cout << endl;
169     BSTDelete(&T, 78);
170     LPBITREE p = BSTSearch(T, 78);
171     if (!p)
172         cout << "未找到指定位置" << endl;
173     else
174         cout << "查找得到的数据是:" << p->data << endl;
175     system("pause");
176     return 0;
177 }
Copy the code
Copy the code
 1 //二分查找.cpp
 2 
 3 #include <iostream>
 4 using namespace std;
 5 int BinarySearch(int Array[], int value, int start, int end)
 6 {
 7     if (start > end)
 8     {
 9         return -1;
10     }
11     int mid = start + (end - start) / 2;
12     if (Array[mid] == value)
13         return mid;
14     else if (value < Array[mid])
15     {
16         end = mid - 1;
17         return BinarySearch(Array, value, start, end);
18     }
19     else
20     {
21         start = mid + 1;
22         return BinarySearch(Array, value, start, end);
23     }
24 }
25 
26 //防御性编程
27 int BinarySearchPos(int Array[], int len, int value)
28 {
29     if (Array == NULL || len <= 0)
30         return 1;
31     int start = 0;
32     int end = len - 1;
33     return BinarySearch(Array, value, start, end);
34 }
35 int main()
36 {
37     //测试  作业
38 
39 
40     return 0;
41 }

Guess you like

Origin www.cnblogs.com/mjgw/p/12590749.html