二叉搜索树(二叉查找树)
1.树中的每个结点都有一个唯一的关键字
2.如果有左子树,所有的左子树关键字的值小于根结点的关键字
3.如果有右子树,所有的右子树关键字的值大于根结点的关键字
4.左右子树也是二叉查找树
1.作业:
y=(*s)->RChild
右边分析图
代码
2. 二叉查找的测试代码
1 //BinarySearchTree.cpp 2 3 #include <iostream> 4 using namespace std; 5 6 typedef struct Node 7 { 8 int data; 9 struct Node* LChild; 10 struct Node* RChild; 11 }BITREE,*LPBITREE; 12 int BSTInsert(LPBITREE *T, int x) 13 { 14 LPBITREE p, cur, parent = NULL; 15 cur = *T; //T是二级指针 16 while (cur != NULL) 17 { 18 //找到插入的位置 19 //如果存在树中 20 if (cur->data == x) 21 { 22 return 0; 23 } 24 parent = cur; 25 //比当前节点的值大--->右子树 26 if (x >cur->data) 27 { 28 cur = cur->RChild; 29 } 30 //比当前节点的值小--->左子树 31 else 32 { 33 cur = cur->LChild; 34 } 35 } 36 //新建插入的结点的空间 37 p = new BITREE; 38 //判断内存是否失败 39 if (!p) 40 { 41 exit(0); 42 } 43 //初始化基本数据成员 44 p->data = x; 45 p->LChild = NULL; 46 p->RChild = NULL; 47 //插入到合适的位置 48 //没有进入 parent=NULL cur=NULL 插入的结点是树根 49 if (!parent) 50 { 51 *T = p; 52 } 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 { 76 p = T; 77 while (p != NULL) 78 { 79 if (p->data == x) 80 { 81 return p; 82 } 83 else if (p->data > x) 84 { 85 p = p->LChild; 86 } 87 else if (p->data < x) 88 { 89 p = p->RChild; 90 } 91 } 92 } 93 return NULL; 94 } 95 void DeleteNode(LPBITREE *s) 96 { 97 LPBITREE q, x, y; 98 //左子树为空 把右子树拿上来就可以 99 if (!(*s)->LChild) 100 { 101 q = *s; //先保存原来 再释放 102 *s = (*s)->RChild; 103 delete q; 104 } 105 else if (!(*s)->RChild) 106 { 107 q = *s; 108 *s = (*s)->LChild; 109 delete q; 110 } 111 else //作业改为右边分析 112 { 113 x = *s; 114 y = (*s)->LChild; 115 while (y->RChild) 116 { 117 x = y; 118 y = y->RChild; 119 } 120 (*s)->data = y->data; 121 if (x == (*s)) 122 { 123 x->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 }
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 }