這一篇開始總結的是二叉排序樹。構造一棵二叉排序樹的目的,其實並非爲了排序,而是爲了提升查找和插入刪除的效率。node
那麼什麼是二叉排序樹呢?二叉排序樹具備如下幾個特色。數組
1,若根節點有左子樹,則左子樹的全部節點都比根節點小。spa
2,若根節點有右子樹,則右子樹的全部節點都比根節點大。指針
3,根節點的左,右子樹也分別爲二叉排序樹。code
下面是二叉排序樹的圖示,經過圖能夠加深對二叉排序樹的理解。blog
下面是二叉排序樹常見的操做及思路。排序
1,插入節點遞歸
思路:好比咱們要插入數字20到這棵二叉排序樹中。那麼步驟以下:ip
1) 首先將20與根節點進行比較,發現比根節點小,因此繼續與根節點的左子樹30比較。get
2) 發現20比30也要小,因此繼續與30的左子樹10進行比較。
3) 發現20比10要大,因此就將20插入到10的右子樹中。
此時二叉排序樹效果如圖:
2,查找節點
好比咱們要查找節點10,那麼思路以下:
1) 仍是同樣,首先將10與根節點50進行比較大小,發現比根節點要小,因此繼續與根節點的左子樹30進行比較。
2) 發現10比左子樹30要小,因此繼續與30的左子樹10進行比較。
3) 發現兩值相等,即查找成功,返回10的位置。
過程與插入相同,這裏就不貼圖了。
3,刪除節點
刪除節點的狀況相對複雜,主要分如下三種情形:
1) 刪除的是葉節點(即沒有孩子節點的)。好比20,刪除它不會破壞原來樹的結構,最簡單。如圖所示。
2) 刪除的是單孩子節點。好比90,刪除它後須要將它的孩子節點與本身的父節點相連。情形比第一種複雜一些。
3) 刪除的是有左右孩子的節點。好比根節點50,這裏有一個問題就是刪除它後將誰作爲根節點的問題?利用二叉樹的中序遍歷,就是右節點的左子樹的最左孩子。
分析完了,有了思路以後,下面就開始寫代碼來實現這些功能了。
C#版:
namespace DS.BLL { /// <summary> /// Description:二叉排序樹的常見操做 /// Author:McgradyLu /// Time:8/24/2013 4:12:18 PM /// </summary> public class BSTreeBLL { /// <summary> /// 建立二叉排序樹 /// </summary> /// <param name="list"></param> /// <returns></returns> public static BSTree Create(List<int> list) { //建立根節點 BSTree bsTree = new BSTree() { Data=list[0], Left=null, Right=null }; //將list中的節點一個一個地插入到二叉排序樹中 for (int i = 1; i < list.Count; i++) //注意這裏從1開始,由於0位置上元素已經給了根節點 { bool isExcute = false; Insert(bsTree, list[i], ref isExcute); } return bsTree; } /// <summary> /// 插入節點 /// </summary> /// <param name="bsTree">二叉排序樹</param> /// <param name="key">待插入值</param> /// <param name="isExcute">是否執行了if語句(節點是否插入)</param> public static void Insert(BSTree bsTree, int key, ref bool isExcute) { if (bsTree == null) return; //若是小於根節點,遍歷左子樹,不然遍歷右子樹(找到當前要插入節點的父節點) if (key < bsTree.Data) Insert(bsTree.Left, key, ref isExcute); else Insert(bsTree.Right, key, ref isExcute); if (!isExcute) { //建立當前節點 BSTree current = new BSTree() { Data=key, Left=null, Right=null }; //插入到父節點中 if (key < bsTree.Data) bsTree.Left = current; else bsTree.Right = current; isExcute = true; } } /// <summary> /// 中序遍歷 /// </summary> /// <param name="bsTree"></param> public static void LDR(BSTree bsTree) { if (bsTree != null) { //遍歷左子樹 LDR(bsTree.Left); //輸出節點數據 Console.Write(bsTree.Data+" "); //遍歷右子樹 LDR(bsTree.Right); } } /// <summary> /// 查找節點 /// </summary> /// <param name="bsTree">待查找的二叉排序樹</param> /// <param name="key"></param> /// <returns>true表示查找成功,false表示查找失敗</returns> public static bool Search(BSTree bsTree, int key) { //遍歷完沒有找到,查找失敗 if (bsTree == null) return false; //要查找的元素爲當前節點,查找成功 if (key == bsTree.Data) return true; //繼續去當前節點的左子樹中查找,不然去當前節點的右子樹中查找 if (key < bsTree.Data) return Search(bsTree.Left, key); else return Search(bsTree.Right,key); } /// <summary> /// 刪除節點 /// </summary> /// <param name="bsTree"></param> /// <param name="key"></param> public static void Delete(ref BSTree bsTree, int key) { //空樹 if (bsTree == null) return; //判斷是不是要刪除的節點 if (key == bsTree.Data) { //第一種狀況:葉子節點(沒有孩子節點) if (bsTree.Left == null && bsTree.Right == null) { bsTree = null; return; } //第二種狀況:僅有左子樹 if (bsTree.Left != null && bsTree.Right == null) { bsTree = bsTree.Left; return; } //第三種狀況:僅有右子樹 if (bsTree.Left == null && bsTree.Right != null) { bsTree = bsTree.Right; return; } //第四種狀況:有左,右子樹 if (bsTree.Left != null && bsTree.Right != null) { //利用中序遍歷找到右節點的左子樹的最左孩子 var node = bsTree.Right; while (node.Left != null) { node = node.Left; } node.Left = bsTree.Left; if (node.Right == null) { Delete(ref bsTree,node.Data); node.Right = bsTree.Right; } bsTree = node; } } //遍歷找到要刪除的節點 if (key < bsTree.Data) { Delete(ref bsTree.Left, key); } else { Delete(ref bsTree.Right, key); } } } /// <summary> /// 封裝二叉排序樹結構 /// </summary> public class BSTree { public int Data; public BSTree Left; public BSTree Right; } } namespace BSTSearch.CSharp { class Program { static void Main(string[] args) { List<int> list = new List<int> { 50,30,70,10,40,90,80}; Console.WriteLine("***************建立二叉排序樹***************"); BSTree bsTree = BSTreeBLL.Create(list); Console.Write("中序遍歷的原始數據:\n"); BSTreeBLL.LDR(bsTree); Console.WriteLine("\n********************查找節點********************"); Console.WriteLine("元素40是否在樹中:{0}",BSTreeBLL.Search(bsTree,40)); Console.WriteLine("\n********************插入節點********************"); Console.WriteLine("將元素20插入到樹中"); bool isExcute=false; BSTreeBLL.Insert(bsTree,20,ref isExcute); Console.Write("中序遍歷後:\n"); BSTreeBLL.LDR(bsTree); Console.WriteLine("\n********************刪除節點1********************"); Console.WriteLine("刪除葉子節點20,\n中序遍歷後:\n"); BSTreeBLL.Delete(ref bsTree,20); BSTreeBLL.LDR(bsTree); Console.WriteLine("\n********************刪除節點2********************"); Console.WriteLine("刪除單孩子節點90,\n中序遍歷後:\n"); BSTreeBLL.Delete(ref bsTree, 90); BSTreeBLL.LDR(bsTree); Console.WriteLine("\n********************刪除節點2********************"); Console.WriteLine("刪除根節點50,\n中序遍歷後:\n"); BSTreeBLL.Delete(ref bsTree, 50); BSTreeBLL.LDR(bsTree); Console.ReadKey(); } } }
程序輸出結果如圖:
C語言版:
/*包含頭文件*/ #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h" #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 #define MAXSIZE 20 typedef int Status; /* 二叉樹的二叉鏈表結點結構定義 */ typedef struct BiTNode /* 結點結構 */ { int data; /* 結點數據 */ struct BiTNode *lchild, *rchild; /* 左右孩子指針 */ } BiTNode, *BiTree; /**BiTree等價於typedef BiTNode *BiTree*/ /*查找二叉排序樹T中是否存在key(遞歸查找)*/ Status Search(BiTree T, int key, BiTree f, BiTree *p) { if (!T) /* 查找不成功 */ { *p = f; return FALSE; } else if (key==T->data) /* 查找成功 */ { *p = T; return TRUE; } else if (key<T->data) return Search(T->lchild, key, T, p); /* 在左子樹中繼續查找 */ else return Search(T->rchild, key, T, p); /* 在右子樹中繼續查找 */ } /* 當二叉排序樹T中不存在關鍵字等於key的數據元素時, */ /* 插入key並返回TRUE,不然返回FALSE */ Status Insert(BiTree *T, int key) { BiTree p,s; if (!Search(*T, key, NULL, &p)) /* 查找不成功 */ { s = (BiTree)malloc(sizeof(BiTNode)); s->data = key; s->lchild = s->rchild = NULL; if (!p) *T = s; /* 插入s爲新的根結點 */ else if (key<p->data) p->lchild = s; /* 插入s爲左孩子 */ else p->rchild = s; /* 插入s爲右孩子 */ return TRUE; } else return FALSE; /* 樹中已有關鍵字相同的結點,再也不插入 */ } /* 從二叉排序樹中刪除結點p,並重接它的左或右子樹。 */ Status DeleteBST(BiTree *p) { BiTree q,s; if((*p)->rchild==NULL) /* 右子樹空則只需重接它的左子樹(待刪結點是葉子也走此分支) */ { q=*p; *p=(*p)->lchild; free(q); } else if((*p)->lchild==NULL) /* 只需重接它的右子樹 */ { q=*p; *p=(*p)->rchild; free(q); } else /* 左右子樹均不空 */ { q=*p; s=(*p)->lchild; while(s->rchild) /* 轉左,而後向右到盡頭(找待刪結點的前驅) */ { q=s; s=s->rchild; } (*p)->data=s->data; /* s指向被刪結點的直接前驅(將被刪結點前驅的值取代被刪結點的值) */ if(q!=*p) q->rchild=s->lchild; /* 重接q的右子樹 */ else q->lchild=s->lchild; /* 重接q的左子樹 */ free(s); } return TRUE; } /* 若二叉排序樹T中存在關鍵字等於key的數據元素時,則刪除該數據元素結點, */ /* 並返回TRUE;不然返回FALSE。 */ Status Delete(BiTree *T,int key) { if(!*T) /* 不存在關鍵字等於key的數據元素 */ return FALSE; else { if (key==(*T)->data) /* 找到關鍵字等於key的數據元素 */ return DeleteBST(T); else if (key<(*T)->data) return Delete(&(*T)->lchild,key); else return Delete(&(*T)->rchild,key); } } /*二叉樹中序遍歷*/ void LDR(BiTree T) { if (T!=NULL) { LDR(T->lchild); printf("%d ",T->data); LDR(T->rchild); } } #define N 10 void main() { int i,j; BiTree T=NULL; //定義數組和初始化SeqList int d[N]={62,88,58,47,35,73,51,99,37,93}; for (i=0;i<N;i++) { Insert(&T,d[i]); } printf("***************二叉排序樹查找(C版)***************\n"); printf("初始化二叉排序樹\n中序遍歷數據:"); LDR(T); printf("\n***************刪除節點1***************\n"); Delete(&T,93); printf("刪除葉節點93\n中序遍歷後:"); LDR(T); printf("\n***************刪除節點2***************\n"); Delete(&T,47); printf("刪除雙孩子節點47\n中序遍歷後:"); LDR(T); getchar(); }
程序輸出結果如圖: