二叉搜索树——插入、查找、删除
生活随笔
收集整理的這篇文章主要介紹了
二叉搜索树——插入、查找、删除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
二叉搜索樹
二叉搜索樹的特點
-
根節點的值大于左結點的值,小于右結點的值
-
根節點的左、右子樹也是一個二叉搜索樹
-
沒有重復值
-
中序遍歷得到的序列是從小到大排列的
-
二叉樹的存儲結構
這里我多定義了一個結構,來操縱根節點,這個結構看個人習慣,可有可無。
二叉搜索樹的算法
以下建立的二叉搜索樹如圖
二叉搜索樹的建立/插入
插入只有在位置為空的時候才插入,并不會在位置不為空的時候搶占其他結點的位置。
void insert_Binode(pBST pB, int val) {BiTree pNew = (BiTree)malloc(sizeof(BiNode));//創建一個新的樹結點同時初始化pNew->data = val;pNew->leftchild = NULL;pNew->rightchild = NULL;if (pB->root == NULL) //樹根節點為空{pB->root= pNew;}else{BiTree temp = pB->root; //創建一個臨時結點來遍歷二叉搜索樹中的結點來進行值比較while(temp!=NULL) {if(val>temp->data) //大于根節點,說明要插入右子樹{if(temp->rightchild == NULL)//根節點的右子樹結點為空,說明可以插入{temp->rightchild = pNew;return;}elsetemp=temp->rightchild; //不為空接著遍歷比較}else{if (temp->leftchild == NULL){temp->leftchild = pNew;//小于根節點,說明要插入右子樹return;}elsetemp=temp->leftchild;}}}}二叉搜索樹的查找
用跟插入一樣的迭代方法可能更好理解,我第一次用的是遞歸方法.
int search_Binode(BiTree pBC,int val) {if(pBC==NULL){return 0;}if(pBC->data==val){return 1;}else if(pBC->data<val) //val更大在右子樹,{return search_Binode(pBC->rightchild, val);//接著在右子樹看是否=val}else{return search_Binode(pBC->leftchild, val);} } int search_Binode(BiTree pBC,int val) {BiTree p=pBC;if(pBC==NULL) //根節點為空{return 0;}while(p!=NULL){if(p->data==val) //找到了返回1{return 1;}else if(pBC->data<val) //val更大在右子樹{p=p->rightchild //更新p}else{p=p->leftchild}} }二叉搜索樹刪除結點
刪除結點更復雜一點,需要討論三種情況
-
刪除節點為葉子節點
-
刪除節點只有左子樹或者右子樹
-
刪除節點既有右子樹又有左子樹
從圖里可以看出第一種、第二種情況都是將待刪除結點的父節點指向待刪除結點的子結點(待刪除結點為葉子節點可以認為它子結點為NULL)
代碼書寫上我一般把第一個和第二個情況一起寫(只考慮了第一種第二種情況)
void delete_Binode(pBST pB, int val) {if(pB->root==NULL){return;}else{BiTree temp = pB->root;BiTree temp_father = NULL;BiTree temp_child = NULL;while (temp->data != val && temp != NULL){if(val>temp->data){temp_father = temp;temp = temp->rightchild;}else{temp_father = temp;temp = temp->leftchild;}}if(temp==NULL){return;}else{if (temp->leftchild != NULL) //判斷要刪除結點是只有一個左子結點還是只有一個右子結點 還是沒有左右子節點temp_child = temp->leftchild;else if (temp->rightchild != NULL)temp_child = temp->rightchild;else temp_child = NULL; //待刪除結點為葉子節點則其子結點為空if(temp_father->leftchild==temp)//把要刪除結點的父節點指向要刪除結點的子結點,先判斷是 父節點的哪個結點temp_father->leftchild = temp_child;elsetemp_father->rightchild = NULL;}} }第三種情況需要做的是
左子樹最大的結點或者右子樹最小的結點覆蓋待刪除結點。然后再把左子樹最大節點或者右子樹最小節點刪除
void delete_Binode(pBST pB, int val) {if(pB->root==NULL){return;}else{BiTree temp = pB->root;BiTree temp_father;BiTree temp_child;while (temp->data != val && temp != NULL){if(val>temp->data){temp_father = temp;temp = temp->rightchild;}else{temp_father = temp;temp = temp->leftchild;}}if(temp==NULL){return;}if (temp->rightchild != NULL && temp->leftchild != NULL)//要刪除結點有兩個子結點{temp_father = temp; //準備好兩個節點用來找要刪除結點的左子樹最大節點temp_child = temp->leftchild; //定位到左子樹while(temp_child->rightchild!=NULL) //找左子樹的最大結點——>一直取右子樹結點{temp_father = temp_child;temp_child = temp_child->rightchild;}temp->data = temp_child->data;if(temp_child->leftchild!=NULL) //找到最大結點的時候要考慮兩種情況,看下圖更好理解{temp_father->leftchild = temp_child->leftchild;}else{temp_father->rightchild = NULL;}}(v表示是左子樹的最大節點,樹的前部分結構沒有畫出,就畫出了左子樹最大節點那一小部分)
如果左子樹最大結點是第二類,就非常方便此時child指向的就是左子樹最大節點,可以直接覆蓋刪除
但是如果是第一類,覆蓋之后直接刪除就會導致有一個結點丟失,所以就需要判斷是否左子樹最大結點的左節點是否為空,如果不為空需要把結點接在最大節點位置
如果是第二類,直接把父節點的右孩子指針域置空就刪除了。
完整代碼
#include <stdio.h> #include <stdbool.h> #include <windows.h> #include <malloc.h> typedef struct tree {int data;struct tree *leftchild;struct tree *rightchild; } BiNode, *BiTree; typedef struct Binary_Search_Tree {BiTree root; } BST,*pBST; void create_searchtree(BiTree *); void insert_Binode(pBST pB, int val); int search_Binode(BiTree pBC, int val); void delete_Binode(pBST pBC, int val); void Midtraverse_tree(BiTree pBC); int main() {pBST pB=(pBST)malloc(sizeof(BST)); //要記得初始化,這里踩了坑,不初始化地址只讀,程序無法運行pB->root = NULL;int i;int a[7] = {6, 3, 8, 2, 5, 1, 7};for (i = 0; i<7;i++){insert_Binode(pB, a[i]);}Midtraverse_tree(pB->root);printf("\n");delete_Binode(pB,3);Midtraverse_tree(pB->root); }void Midtraverse_tree(BiTree pBC) {if (pBC == NULL){return;}else{Midtraverse_tree(pBC->leftchild);printf("%d", pBC->data);Midtraverse_tree(pBC->rightchild);} } void insert_Binode(pBST pB, int val) {BiTree pNew = (BiTree)malloc(sizeof(BiNode));pNew->data = val;pNew->leftchild = NULL;pNew->rightchild = NULL;if (pB->root == NULL){pB->root= pNew;}else{BiTree temp = pB->root;while(temp!=NULL){if(val>temp->data){if(temp->rightchild == NULL){temp->rightchild = pNew;return;}elsetemp=temp->rightchild;}else{if (temp->leftchild == NULL){temp->leftchild = pNew;return;}elsetemp=temp->leftchild;}}}}int search_Binode(BiTree pBC,int val) {if(pBC==NULL){return 0;}if(pBC->data==val){return 1;}else if(pBC->data<val){return search_Binode(pBC->rightchild, val);}else{return search_Binode(pBC->leftchild, val);} }void delete_Binode(pBST pB, int val) {if(pB->root==NULL){return;}else{BiTree temp = pB->root;BiTree temp_father;BiTree temp_child;while (temp->data != val && temp != NULL){if(val>temp->data){temp_father = temp;temp = temp->rightchild;}else{temp_father = temp;temp = temp->leftchild;}}if(temp==NULL){return;}if (temp->rightchild != NULL && temp->leftchild != NULL)//要刪除結點有兩個子結點{temp_father = temp;temp_child = temp->leftchild;while(temp_child->rightchild!=NULL) //找左子樹的最大結點{temp_father = temp_child;temp_child = temp_child->rightchild;}temp->data = temp_child->data;if(temp_child->leftchild!=NULL){temp_father->leftchild = temp_child->leftchild;}else{temp_father->rightchild = NULL;}}else{if (temp->leftchild != NULL) //判斷要刪除結點是只有一個左子結點還是只有一個右子結點還是沒有左右子節點temp_child = temp->leftchild;else if (temp->rightchild != NULL)temp_child = temp->rightchild;elsetemp_child = NULL;if(temp_father->leftchild==temp)//把要刪除結點的父節點指向要刪除結點的子結點,先判斷是父節點的哪個結點temp_father->leftchild = temp_child;else if (temp_father->rightchild == temp)temp_father->rightchild = temp_child;} } }總結
以上是生活随笔為你收集整理的二叉搜索树——插入、查找、删除的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何合理选择压力传感器
- 下一篇: 压力传感器与数据采集