C 類模板實踐(二叉樹類)1

2021-08-31 13:36:36 字數 1475 閱讀 1165

先貼上我**(未成品,我就先寫了構造和析構,先序遍歷**),需要用的人可以直接用。

造輪子方便我去了解stl,了解**。

我寫**時踩過的一些小坑,作為一些小知識:

發生了幾個語法,編譯,鏈結bug。

第乙個:模板的typedef,得宣告型別:typedef  binarytreenodenode;

第二個:struct binarytreenode,構造時採用匿名物件構造解決了初版**的一些小問題

第三個:inline函式還是不要分離(我自己一開始不規範),一開始宣告放在標頭檔案,實現放在下面的cpp實現,會出現鏈結錯誤,源自inline的特性:c++在編譯inline(內聯)函式時,會直接展開,避免壓棧,宣告實現分離就會....找不到函式了。

第四個:stack容器得把型別定好,我實現先序遍歷的迭代版本(非遞迴)時,一開始沒使用stack* >,導致了一些stack型別轉換問題,恩,沒錯,借鑑我**的朋友要時刻注意模板型別的問題,模板還是蠻酸爽的,和用c語言寫資料結構完全不一樣。

#pragma once

#include #include #includeusing namespace std;

template struct binarytreenode

binarytreenode* _left;

binarytreenode* _right;

t _data;

};template class binarytree

;templateinline binarytree::binarytree(const t* a, size_t size, int index, const t& invalid)

templateinline binarytree::~binarytree()

templateinline void binarytree::printpre()

templateinline binarytreenode* binarytree::_maketree(const t * a, size_t size, int & index, const t & invalid)

return root;

}templateinline void binarytree::destroy(node * _root)

templateinline void binarytree::_printpre(node * _root)

cout << _root->_data << endl;

_printpre(_root->_left);

_printpre(_root->_right);

}templateinline void binarytree:: printpreiteration()

while(cur||!s.empty())

top=s.top();

s.pop();

cur=top->_right;

}}

類模板實現二叉查詢樹

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

二叉查詢樹的類模板實現

二叉查詢樹的性質是,對於樹中的每乙個結點x,它的左子樹中所有的項的值小於x中的項,而它的右子樹中所有項的值大於x中的項。include using namespace std template class binarysearch binarynode root 頭結點 void insert co...

二叉樹的模板類

同樣的問題,當時忘了衛星資料的模板了 二叉樹的模板類 include using namespace std template struct node template class tree class template tree class tree class template 這裡用的是指標的...