二叉排序樹(BST,Binary Sort Tree)具有這樣的性質:對於二叉樹中的任意節點,如果它有左子樹或右子樹,則該節點的數據成員大於左子樹所有節點的數據成員,且小於右子樹所有節點的數據成員。排序二叉樹的中序遍歷結果是從小到大排列的。
二叉排序樹的查找和插入比較好理解,主要來看一下刪除時的情況。
如果需要查找並刪除如圖8-6-8中的37, 51, 73,93這些在二叉排序樹中是葉子的結點,那是很容易的,畢竟刪除它們對整棵樹來說,其他結點的結構並未受到影響。
對於要刪除的結點只有左子樹或只有右子樹的情況,相對也比較好解決。那就是結點刪除后,將它的左子樹或右子樹整個移動到刪除結點的位置即可,可以理解為獨子繼承父業。比如圖8-6-9,就是先刪除35和99兩結點,再刪除58結點的變化圖,最終,整個結構還是一個二叉排序樹。
但是對於要刪除的結點既有左子樹又有右子樹的情況怎么辦呢?比如圖8-6-10中的47結點若要刪除了,它的兩兒子和子孫們怎么辦呢?
前人總結的比較好的方法就是,找到需要刪除的結點p的直接前驅(或直接后繼)s,用s來替換結點p,然后再刪除此結點s,如圖8-6-12所示。
注意:這里的前驅和后繼是指中序遍歷時的順序。
Deletion
There are three possible cases to consider:
• Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree.
• Deleting a node with one child: Remove the node and replace it with its child.
• Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-
order predecessor node, R. Replace the value of N with the value of R, then delete R.
As with all binary trees, a node's in-order successor is the left-most child of its right subtree, and a node's in-order predecessor is the right-most
child of its left subtree. In either case, this node will have zero or one children. Delete it according to one of the two simpler cases above.
下面來看代碼:(參考《linux c 編程一站式學習》
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/************************************************************************* > File Name: binarysearchtree.h > Author: Simba > Mail: dameng34@163.com > Created Time: Sat 29 Dec 2012 06:05:55 PM CST ************************************************************************/ #ifndef BST_H #define BST_H typedef struct node *link; struct node { unsigned char item; link left, right; }; link search(link t, unsigned char key); link insert(link t, unsigned char key); link delete(link t, unsigned char key); void print_tree(link t); #endif |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
/************************************************************************* > File Name: binarysearchtree.c > Author: Simba > Mail: dameng34@163.com > Created Time: Sat 29 Dec 2012 06:08:08 PM CST ************************************************************************/ #include<stdio.h> #include<stdlib.h> #include "binarysearchtree.h" static link make_node(unsigned char item) { link p = malloc(sizeof(*p)); p->item = item; p->left = p->right = NULL; return p; } static void free_node(link p) { free(p); } link search(link t, unsigned char key) { if (!t) return NULL; if (t->item > key) return search(t->left, key); if (t->item < key) return search(t->right, key); /* if (t->item == key) */ return t; } link insert(link t, unsigned char key) { if (!t) return make_node(key); if (t->item > key) /* insert to left subtree */ t->left = insert(t->left, key); else /* if (t->item <= key), insert to right subtree */ t->right = insert(t->right, key); return t; } link delete(link t, unsigned char key) { link p; if (!t) return NULL; if (t->item > key) /* delete from left subtree */ t->left = delete(t->left, key); else if (t->item < key) /* delete from right subtree */ t->right = delete(t->right, key); else /* if (t->item == key) */ { if (t->left == NULL && t->right == NULL) { /* if t is a leaf node */ free_node(t); t = NULL; } else if (t->left) /* if t has left subtree */ { /* replace t with the rightmost node in left subtree */ for (p = t->left; p->right; p = p->right); t->item = p->item; /* 將左子樹下最靠右的節點值賦予想要刪除的節點 */ t->left = delete(t->left, t->item); } else /* if t has right subtree */ { /* replace t with the leftmost node in right subtree */ for (p = t->right; p->left; p = p->left); t->item = p->item; t->right = delete(t->right, t->item); } } return t; } void print_tree(link t) { if (t) { printf("("); printf("%d", t->item); print_tree(t->left); print_tree(t->right); printf(")"); } else printf("()"); } |
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
/************************************************************************* > File Name: main2.c > Author: Simba > Mail: dameng34@163.com > Created Time: Sat 29 Dec 2012 06:22:57 PM CST ************************************************************************/ #include<stdio.h> #include<stdlib.h> #include<time.h> #include "binarysearchtree.h" #define RANGE 100 #define N 6 void print_item(link p) { printf("%d", p->item); } int main(void) { int i, key; link root = NULL; srand(time(NULL)); for (i = 0; i < N; i++) { root = insert(root, rand() % RANGE); /* 第一次循環root從NULL變成根節點值,接下去 的循環雖然迭代root,但在插入節點過程中root的值始終不變 */ printf("root = 0x%x\n", (unsigned int)root); } printf("\t\\tree"); print_tree(root); printf("\n\n"); while (root) { key = rand() % RANGE; if (search(root, key)) { printf("delete %d in tree\n", key); root = delete(root, key); /* root雖然迭代,但返回的仍是先前的值,即根節點的值保持不變 直到全部節點被刪除,root變成NULL即0x0 */ printf("root = 0x%x\n", (unsigned int)root); printf("\t\\tree"); print_tree(root); /* 傳遞給函數的一直是根節點的值,直到樹清空,root變成NULL */ printf("\n\n"); } } return 0; } |
輸出為:
如果我們使用了The Tree Preprocessor,可以將以括號展示的排序二叉樹轉換成樹形展示,如下圖
以前此工具可以在 http://www.essex.ac.uk/linguistics/clmt/latex4ling/trees/tree/ 下載,現已找不到鏈接,我將其上傳到csdn,需要的可以去下載。
http://download.csdn.net/detail/simba888888/5334093
最后提一下,我們希望構建出來的二叉排序樹是比較平衡的,即其深度與完全二叉樹相同,那么查找的時間復雜度研究度O(logn),近似於折半查找,
但如果出現構造的樹嚴重不平衡,如完全是左斜樹或者右斜樹,那么查找時間復雜度為O(n),近似於順序查找。那如何讓二叉排序樹平衡呢?
事實上還有一種平衡二叉樹(AVL樹),也是一種二叉排序樹,其中每個結點的左子樹和右子樹的高度差至多等於1。
補充:delete() 在《data structure and algorithm analysis in c》 中的實現,個人覺得比較清晰,也挺好理解的,如下:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
link FindMin(link T)
{ if (T != NULL) while (T->left != NULL) T = T->left; return T; } link delete(unsigned char X, link T) { link tmp; if (T == NULL) { printf("Tree is empty!\n"); return NULL; } if (X < T->key) //go left T->left = delete(X, T->left); else if (X > T->key) // go right T->right = delete(X, T->right); /* found elem to be deleted*/ else if (T->left && T->right) //have two children { // replace with smallest in right subtree tmp = FindMin(T->right); T->key = tmp->key; T->right = delete(T->key, T->right); } else //one or zero children { tmp = T; if (T->left == NULL) T = T->right; else if (T->right == NULL) T = T->left; free(tmp); } return T; } |
參考:
《大話數據結構》
《linux c 編程一站式學習》
《Data Structures》