c 模板實現 二叉查詢樹

2021-09-06 05:46:03 字數 3675 閱讀 3400

搗鼓了乙個晚上,最後還是照著書本把這bst弄出來了。悲催的娃娃啊,不動手寫這個還真的很難啊!

1

#ifndef btree_h_

2#define btree_h_

34 #include 5

using

std::ostream;

67 template

8class

bstree917

bstnode(treedatatype a_data):data(a_data),left(null),right(null) {}

18 };//

節點宣告

19 typedef bstnode*bstnodepointer;

20bstnodepointer m_root;

2122

public:23

bstree():m_root(null) {}

24 ~bstree()

25bool isempty() const

26bool find(const treedatatype& a_data) const;27

void insert(const treedatatype&a_data)

28void remove(const treedatatype&a_data);

29void inorder(ostream& out) const

30void graph(ostream& out) const

3132

protected:33

void deletenode(bstnodepointer a_node);//

刪除節點和所有到子節點

34void insertaux(bstnodepointer& a_subroot, const treedatatype&a_data);

35void inorderaux(ostream& out, bstnodepointer a_subroot) const;36

void graphaux(ostream& out, int a_indent, bstnodepointer a_subroot) const;37

void find2(const treedatatype& a_data, bool& found, bstnodepointer& a_locptr, bstnodepointer& a_parent) const

; 38 };//

類模板宣告結束

39#endif

4041 template

42 inline void bstree::deletenode(bstree::bstnodepointer a_node)

4348

else

if (a_node->right !=null)

4952

else

if (a_node !=null)

5357}58

59 template

60 inline void bstree::insertaux(bstree::bstnodepointer& a_subroot, const treedatatype&a_data)

6166

else

if (a_data < a_subroot->data)

6770

else

if (a_subroot->data

7174

else

7578}79

80 template

81 inline void bstree::inorderaux(ostream& out, bstree::bstnodepointer a_subroot) const

8289}90

91 #include 92

using

std::setw;

93using

std::endl;

94 template

95 inline void bstree::graphaux(ostream& out, int a_indent, bstree::bstnodepointer a_subroot) const

96103

}104

105 template

106 inline bool bstree::find(const treedatatype& a_data) const

107116

else

if (locptr->data

117120

else

121124

}125

return

found;

126}

127128 template

129 inline void bstree::find2(const treedatatype& a_data, bool&found,

130 bstree::bstnodepointer&a_locptr,

131 bstree::bstnodepointer& a_parent) const

132143

else

if (a_locptr->data

144148

else

149152

}153

}154

155 template

156 inline void bstree::remove(const treedatatype&a_data)

157167

168if (x->left != null && x->right != null)//

節點有兩個子女

169178 x->data = xsucc->data;

179 x =xsucc;

180}

181 bstree::bstnodepointer subtree = x->left;

182if (subtree ==null)

183186

if (parent ==null)

187190

else

if (parent->left ==x)

191194

else

195198

delete x;

199 }

測試下結果

1 #include "

bstree.h

"2 #include 3 #include 4

using

namespace

std;56

intmain()720

intbst.inorder(cout);

21 cout <

22intbst.graph(cout);

2324

//測試find

25for

(;;)

2633

34//

測試remove

35for

(;;)

3645

intbst.inorder(cout);

46return0;

47 }

二叉查詢樹 模板實現 C

二叉查詢樹 模板實現 c 1 二叉查詢樹的性質 對於樹中每個結點x,它的左子樹中所有項的值小於x中的值,而它的右子樹中所有項的值大於x中的值。2 二叉樹的操作主要是 插入,刪除,查詢。2.1 查詢 contains 實現思路 如果待查詢的項x在樹中,返回true 否則返回false。若當前比較的結點...

類模板實現二叉查詢樹

二叉查詢樹是一種特殊的二叉樹,這種樹上不存在重複的結點,而且它上面的左子樹都比其父結點小,它上面的右子樹都比其父結點大。值得注意的是 具有相同資料的二叉查詢樹會隨著資料插入順序不同而不同。在treenode.h中 在tree.h中 在tree.cpp中 在main.cpp中 值得注意的幾點 1.te...

C 模板實現二叉搜尋樹

二叉搜尋樹的增刪該查操作實現 插入操作 借助輔助遞迴函式node insert node node,key key,value value 像根節點為node的二叉搜尋樹插入乙個資料,返回值為根節點,如果node null,那麼新建乙個節點返回,如果keykey,則插入到左子樹,當前返回的根節點為左...