二叉查詢樹的類模板實現

2021-06-26 17:38:04 字數 2090 閱讀 6649

二叉查詢樹的性質是,對於樹中的每乙個結點x,它的左子樹中所有的項的值小於x中的項,而它的右子樹中所有項的值大於x中的項。

#include using namespace std;

template class binarysearch

};binarynode *root; //頭結點

void insert ( const t & x, binarynode * &t ) ;

void remove ( const t & x, binarynode * &t ) ;

const t & findmin ( binarynode * t ) const;

const t & findmax ( binarynode * t ) const;

bool contain ( const t &x, binarynode *t ) const;

void printtree ( binarynode *t ) const;

void makeempty ( binarynode * &t );

binarynode *clone ( binarynode *t ) const

};templatebinarysearch::binarysearch()

templatebinarysearch::binarysearch ( const binarysearch &rhs )

templateconst binarysearch& binarysearch::operator= ( const binarysearch &rhs )

return *this;

}templatebinarysearch::~binarysearch()

templatevoid binarysearch::makeempty()

templatevoid binarysearch::makeempty ( binarynode * &t )

t = null;

}template void binarysearch::insert ( const t &x )

template void binarysearch::insert ( const t &x, binarynode * &t )

template bool binarysearch::contains ( const t &x ) const

template bool binarysearch::contain ( const t &x, binarynode *t ) const

template void binarysearch::remove ( const t &x )

templatevoid binarysearch::remove ( const t &x, binarynode * &t )

else

}templateconst t & binarysearch::findmax() const

templateconst t & binarysearch::findmax ( binarynode *t ) const

templateconst t & binarysearch::findmin() const

templateconst t & binarysearch::findmin ( binarynode *t ) const

templatebool binarysearch::isempty() const

templatevoid binarysearch::printtree() const

templatevoid binarysearch::printtree ( binarynode *t ) const

cout << t->element << " ";

if ( t->right != null )

printtree ( t->right );

/*後序輸出*/

//if(t->left!=null)

// printtree(t->left);

//if(t->right!=null)

// printtree(t->right);

//cout}

類模板實現二叉查詢樹

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

二叉查詢樹 模板實現 C

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

c 模板實現 二叉查詢樹

搗鼓了乙個晚上,最後還是照著書本把這bst弄出來了。悲催的娃娃啊,不動手寫這個還真的很難啊!1 ifndef btree h 2 define btree h 34 include 5 using std ostream 67 template 8class bstree917 bstnode tr...