二叉查詢數的C 實現

2021-06-13 05:11:02 字數 2309 閱讀 2511

///標頭檔案定義

#include

using namespace std;

template

class binarysearchtree

};binarynode *root;

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

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

binarynode * findmin( binarynode *t) const;

binarynode * findmax( binarynode *t) const;

bool contains( const type & x, binarynode *t ) const;

void makeempty( binarynode  * & t) ;

void printtree( binarynode *t ) const;

binarynode* clone( binarynode *t )const;

/函式實現

#include

#include

template

binarysearchtree::binarysearchtree()

template

binarysearchtree::binarysearchtree(const binarysearchtree& rhs)

template

binarysearchtree::~binarysearchtree()

template

bool binarysearchtree::isempty( ) const

template

const binarysearchtree& binarysearchtree:: operator=( const binarysearchtree& rhs)

template

const type & binarysearchtree::findmax() const

template

typename binarysearchtree::binarynode*

binarysearchtree::findmax(binarynode *t) const

template

const type & binarysearchtree::findmin() const

template

typename binarysearchtree::binarynode*

binarysearchtree::findmin(binarynode *t) const

template

bool binarysearchtree::contains(const type & x) const

template

bool binarysearchtree::contains(const type & x, binarynode* t) const

template

void binarysearchtree::printtree() const

}template

void binarysearchtree::insert(const type &x)

template

void binarysearchtree::insert(const type &x, binarynode *&t) const

template

void binarysearchtree::remove(const type &x)

template

void binarysearchtree::remove(const type &x, binarynode *&t) const

else

}template

void binarysearchtree::makeempty()

template

void binarysearchtree::makeempty(binarynode *&t)

t=null;

}template

typename binarysearchtree::binarynode*

binarysearchtree::clone(binarynode *t) const

//測試函式實現

#include "stdafx.h"

#include "stdafx.cpp"

int _tmain(int argc, _tchar* argv)

二叉查詢數

二叉查詢樹,或者是一顆空樹,具備以下性質得二叉樹 1,若它的左子樹不空,則其左子樹上的所有結點的值均小於它根結點的值 2,若它的右子樹不空,則其右子樹上的所有結點的值均大於它根結點的值 3,它的左 右子樹也分別為二叉查詢樹 具體如下圖 查詢操作 在二叉查詢樹中查詢x的過程如下 1 若二叉樹是空樹,則...

C 實現二叉查詢樹

樹是一種非線性結構。樹的本質是將一些節點由邊連線起來,形成層級的結構。而二叉樹是一種特殊的樹,使得樹每個子節點必須小於等於2.而二叉查詢樹又是一類特殊的二叉樹。使得每乙個節點的左節點或左子樹的所有節點必須小於這個節點,右節點必須大於這個節點。從而方便高效搜尋。下面來看如何使用c 實現二叉查詢樹。二叉...

二叉查詢樹的實現C

struct node class bstelse else 插入到root的左子樹 else return root node else node insert2tree node root node,int val 刪除節點base,並返回根節點 node deletenode node con...