#include #include#define elementtype int //節點資料型別
//typedef struct treenode *position;
//typedef struct treenode *searchtree;
struct
treenode
;typedef
struct treenode *position;
typedef
struct treenode *searchtree;
searchtree insert(elementtype x, searchtree t);
searchtree delete(elementtype x, searchtree t);
searchtree makeempty(searchtree t);
searchtree printtree(searchtree t); //使用前序遍歷列印樹
position find(elementtype x, searchtree t);
position findmax(searchtree t);
position findmin(searchtree t);
searchtree insert(elementtype x, searchtree t)
else
}else
if (x > t->element)
else
returnt;}
//刪除的效率並不高,因為它沿該樹進行了兩趟搜尋以查詢和刪除右子樹中最小的節點.
//經過大量刪除操作後,將會導致左子樹比右子樹深,原因是我們總是用右子樹的乙個節點來代替刪除節點
searchtree delete(elementtype x, searchtree t)
else
if (x > t->element)
else
if (x < t->element)
else
if (t->right && t->left)
else
else
if (null == t->left)
else
{}
free
(temp);
temp =null;
}returnt;}
searchtree makeempty(searchtree t)
return
null;
}searchtree printtree(searchtree t)
if (null != t->right)
}return
null;
}position find(elementtype x, searchtree t)
else
if (x < t->element)
else
if (x > t->element)
else
}position findmax(searchtree t)
}returnt;}
position findmin(searchtree t)
else
if (null == t->left)
else
}int
main()
ptmp =findmax(t);
if (null !=ptmp)
ptmp = find(3
, t);
if (null !=ptmp)
printtree(t);
printf("\n
");
t = delete(3
, t);
printtree(t);
printf("\n
");
ptmp = find(3
, t);
if (null !=ptmp)
t =makeempty(t);
return
0;
}
資料結構與演算法分析 二叉查詢樹
class binarysearchtree comparable super anytype 節點初始化 建立乙個有兩個子樹的節點 binarynode anytype theelement,binarynode left,binarynode right 宣告根節點 private binary...
資料結構與演算法 二叉查詢樹
1.建立bst樹 往bst樹中新增節點 公開的介面函式 param node 需要新增到bst樹的那個節點 public void add treenode node else 隱藏的函式,add方法的具體實現 param parent bst樹 或者子樹 的根節點 param child 要插入b...
《資料結構與演算法 二叉查詢樹》
二叉查詢樹 binary search tree 也被稱作二叉搜尋樹。設x是二叉查詢樹中的任意乙個結點,則x的鍵值大於等於它的左子樹中任意乙個結點的鍵值,小於等於它的右子樹中任意乙個結點的鍵值。1 結點的前驅和後繼 結點的前驅 二叉樹中鍵值小於該結點的最大結點。結點的後繼 二叉樹中鍵值大於該結點的最...