From 78e41abe7fb30b30bdd0a36c382cfdff5d248886 Mon Sep 17 00:00:00 2001 From: Didnelpsun <2675350965@qq.com> Date: Tue, 27 Apr 2021 23:26:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=A0=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Code/Code.vcxproj | 1 + Code/link_tree.h | 69 ++++++++++++++++++++++++ Code/sequence_tree.cpp | 6 --- Data-Structrue/tree.md | 117 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 187 insertions(+), 6 deletions(-) delete mode 100644 Code/sequence_tree.cpp diff --git a/Code/Code.vcxproj b/Code/Code.vcxproj index 7d07a56..ec632d7 100644 --- a/Code/Code.vcxproj +++ b/Code/Code.vcxproj @@ -141,6 +141,7 @@ + diff --git a/Code/link_tree.h b/Code/link_tree.h index 46d25ec..ca57f2e 100644 --- a/Code/link_tree.h +++ b/Code/link_tree.h @@ -118,4 +118,73 @@ int LevelorderTraversalLinkTree(LinkTree tree, int(*visit)(LinkTree elem)) { EnterSequenceQueue(&queue, p->rchild); }*/ } +} + +// 二叉排序树遍历查找 +LinkTreeNode* TraversalSearchBST(LinkTree tree, element_type elem) { + while (tree != NULL && elem != tree->data) { + if (elem < tree->data) { + tree = tree->lchild; + } + else { + tree = tree->rchild; + } + } + return tree; +} + +// 二叉排序树递归查找 +LinkTreeNode* RecursiveSearchBST(LinkTree tree, element_type elem) { + if (tree == NULL) { + // 为空树 + return NULL; + } + if (elem = tree->data) { + return tree; + } + else if (elem < tree->data) { + return RecursiveSearchBST(tree->lchild, elem); + } + else { + return RecursiveSearchBST(tree->rchild, elem); + } +} + +// 二叉排序树递归插入 +int InsertBST(LinkTree tree, element_type elem) { + if (tree == NULL) { + tree = (LinkTree)malloc(sizeof(LinkTree)); + if (tree) { + tree->data = elem; + tree->lchild = tree->rchild = NULL; + return 0; + } + else { + printf("BSTInsert:插入分配空间失败!"); + return 1; + } + } + // 关键词重复 + else if (elem == tree->data) { + printf("BSTInsert:关键字重复!"); + return 1; + } + else if (elem < tree->data) { + return InsertBST(tree->lchild, elem); + } + else { + return InsertBST(tree->rchild, elem); + } +} + +// 根据关键字建立二叉排序树 +int CreateBST(LinkTree tree, element_type elem[], int n) { + tree = NULL; + int i = 0; + while (true) + { + InsertBST(tree, elem[i]); + i++; + } + return 0; } \ No newline at end of file diff --git a/Code/sequence_tree.cpp b/Code/sequence_tree.cpp deleted file mode 100644 index f2e4dba..0000000 --- a/Code/sequence_tree.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include -#include -#include "head.h" - -// 定义一个顺序树数组 -typedef int SequenceTree[MAXSIZE]; diff --git a/Data-Structrue/tree.md b/Data-Structrue/tree.md index 6b70adb..8f0b8c0 100644 --- a/Data-Structrue/tree.md +++ b/Data-Structrue/tree.md @@ -178,6 +178,123 @@ 3. 鑻ュ皬浜庢牴缁撶偣锛屽垯鍦ㄥ乏瀛愭爲涓婃煡鎵撅紝鍚﹀垯鍦ㄥ彸瀛愭爲涓婃煡鎵俱 4. 閬嶅巻缁撴潫鍚庝粛娌℃湁鎵惧埌鍒欒繑鍥濶ULL銆 +閬嶅巻鏌ユ壘鐨勬椂闂村鏉傚害鏄$O(\log_2n)$锛屽垯閫掑綊鏌ユ壘鐨勬椂闂村鏉傚害鏄$O(\log_2n+1)$锛屽叾涓$\log_2n+1$浠h〃浜屽弶鏍戠殑楂樺害銆 + +鏌ユ壘鎴愬姛鐨勫钩鍧囨煡鎵鹃暱搴SL锛屼簩鍙夋爲鐨勫钩鍧囨煡鎵鹃暱搴︿负$O(\log_2n)$锛屾渶鍧忔儏鍐垫槸姣忎釜缁撶偣鍙湁涓涓垎鏀紝骞冲潎鏌ユ壘闀垮害涓$O(n)$銆 + +#### 浜屽弶鎺掑簭鏍戠殑鎻掑叆 + ++ 鑻ュ師浜屽弶鎺掑簭鏍戜负绌猴紝灏辩洿鎺ユ彃鍏ョ粨鐐广 ++ 鍚﹀垯锛岃嫢鍏抽敭瀛楀皬浜庢牴缁撶偣鍊硷紝鎻掑叆宸︾粨鐐规爲銆 ++ 鑻ュ叧閿瓧澶т簬鏍圭粨鐐瑰硷紝鎻掑叆鍙崇粨鐐规爲銆 + +#### 浜屽弶鎺掑簭鏍戠殑鍒犻櫎 + ++ 鎼滅储鍒板搴斿肩殑鐩爣缁撶偣銆 ++ 鑻ヨ鍒犻櫎缁撶偣1p鏄彾瀛愮粨鐐癸紝鍒欑洿鎺ュ垹闄わ紝涓嶄細鐮村潖浜屽弶鎺掑簭鏍戠殑缁撴瀯銆 ++ 鑻ヨ鍒犻櫎缁撶偣鍙湁涓妫靛乏瀛愭爲鎴栧彸瀛愭爲锛屽垯璁╄缁撶偣鐨勫瓙鏍戠О涓鸿缁撶偣鐖剁粨鐐圭殑瀛愭爲锛屾潵浠f浛鍏剁殑浣嶇疆銆 ++ 鑻ヨ鍒犻櫎缁撶偣鏈夊乏瀛愭爲鍜屽彸瀛愭爲锛屽垯璁╁叾缁撶偣鐨勭洿鎺ュ悗缁э紙涓簭鎺掑簭璇ョ粨鐐瑰悗涓涓粨鐐癸紝鍏跺彸瀛愭爲鐨勬渶宸︿笅瑙掔粨鐐癸級鎴栫洿鎺ュ墠椹憋紙涓簭鎺掑簭璇ョ粨鐐瑰墠涓涓粨鐐癸紝鍏跺乏瀛愭爲鐨勬渶鍙充笅瑙掔粨鐐癸級鏇夸唬璇ョ粨鐐癸紝骞朵粠浜屽弶鎺掑簭鏍戜腑鍒犻櫎璇ョ殑缁撶偣鐩存帴鍚庣户鍚庣洿鎺ュ墠椹憋紝杩欏氨鍙樻垚浜嗙涓绉嶆垨绗簩绉嶆儏鍐点 + +### 骞宠 浜屽弶鏍 + +鍗矨VL鏍戯紝鏍戜笂浠绘剰涓缁撶偣鐨勫乏瀛愭爲鍜屽彸瀛愭爲鐨勯珮搴︿箣宸笉瓒呰繃1銆 + +缁撶偣鐨勫钩琛″洜瀛=宸﹀瓙鏍戦珮-鍙冲瓙鏍戦珮銆 + +鍦ㄦ彃鍏ヤ竴涓粨鐐规椂锛屾煡鎵捐矾寰勪笂鐨勬墍鏈夌粨鐐归兘鍙兘鏀跺埌褰卞搷銆 + +浠庢彃鍏ョ偣寰鍥烇紙浠庝笅寰涓婏級鎵惧埌绗竴涓笉骞宠 鐨勭粨鐐癸紝璋冩暣浠ヨ缁撶偣涓烘牴鐨勫瓙鏍戙傛瘡娆¤皟鏁寸殑瀵硅薄閮芥槸鏈灏忎笉骞宠 鏍戙 + +### 璋冩暣鏈灏忎笉骞宠 鏍 + +#### 浠庣粨鐐圭殑宸﹀瀛愮殑宸﹀瓙鏍戜腑鎻掑叆瀵艰嚧涓嶅钩琛 + +```terminal + A(2) + | + ----------- + | | + B(1) AR(H) + | + ------------ + | | +BL(H+1) BR(H) +``` + +鐢变簬鍦ㄧ粨鐐笰鐨勫乏瀛╁瓙B鐨勭殑宸﹀瓙鏍態L涓婃彃鍏ヤ簡鏂扮粨鐐癸紝A鐨勫钩琛″洜瀛愮敱1鍙樻垚浜2锛屽鑷翠互A涓烘牴鐨勫瓙鏍戝け鍘讳簡骞宠 锛岄渶瑕佽繘琛屼竴娆″悜鍙崇殑鏃嬭浆鎿嶄綔銆 + +灏咥鐨勫乏瀛╁瓙B鍚戝彸涓婃棆杞唬鏇緼鎴愪负鏍硅妭鐐癸紝灏咥缁撶偣鍚戝彸涓嬮夋嫨涓烘垚B鐨勫彸瀛愭爲鐨勬牴缁撶偣锛岃孊鐨勫師鍙冲瓙鏍戝垯浣滀负A缁撶偣鐨勫乏瀛愭爲銆 + +```terminal + B(0) + | + ----------- + | | +BL(H+1) A(0) + | + ------------ + | | + BR(H) AR(H) +``` + +#### 浠庣粨鐐圭殑鍙冲瀛愮殑鍙冲瓙鏍戜腑鎻掑叆瀵艰嚧涓嶅钩琛 + +```terminal + A(-2) + | + ----------- + | | +AL(H) B(-1) + | + ------------ + | | + BL(H) BR(H+1) +``` + +鐢变簬鍦ㄧ粨鐐笰鐨勫彸瀛╁瓙(R)鐨勫彸瀛愭爲(R)涓婃彃鍏ヤ簡鏂扮粨鐐癸紝A鐨勫钩琛″洜瀛愮敱-1鍑忚嚦-2锛屽鑷翠互A涓烘牴鐨勫瓙鏍戝け鍘诲钩琛★紝闇瑕佷竴-娆″悜宸︾殑鏃嬭浆鎿嶄綔銆 + +灏咥鐨勫彸瀛╁瓙B鍚戝乏涓婃棆杞唬鏇緼鎴愪负鏍圭粨鐐癸紝灏咥缁撶偣鍚戝乏涓嬫棆杞垚涓築鐨勫乏瀛愭爲鐨勬牴缁撶偣锛岃孊鐨勫師宸﹀瓙鏍戝垯浣滀负A缁撶偣鐨勫彸瀛愭爲銆 + +```terminal + B(0) + | + ----------- + | | + A(0) BR(H+1) + | + ------------ + | | +AL(H) BR(H) +``` + +#### 浠庣粨鐐圭殑宸﹀瀛愮殑鍙冲瓙鏍戜腑鎻掑叆瀵艰嚧涓嶅钩琛 + +```terminal + A(2) + | + ----------- + | | + B(1) AR(H) + | + ------------ + | | +BL(H) BR(H+1) +``` + +```terminal + B(0) + | + ----------- + | | +BL(H+1) A(0) + | + ------------ + | | + BR(H) AR(H) +``` + +#### 浠庣粨鐐圭殑鍙冲瀛愮殑宸﹀瓙鏍戜腑鎻掑叆瀵艰嚧涓嶅钩琛 + ## 鏍戜笌妫灄 ### 鏍戠殑瀛樺偍缁撴瀯