二叉搜尋樹的性質是:對樹中的每個結點x,它的左子樹的值小於x,它的右子樹的值大於x。
binarytree.h
#include "utility.h"
//typedef struct treenode *ptrtonode;
typedef struct treenode *position;
typedef struct treenode *searchtree;
struct treenode
;searchtree makeempty( searchtree t )
return null;
}position find( int x, searchtree t )
position findmin( searchtree t )
position findmax( searchtree t )
searchtree insert( int x, searchtree t )
}else if( x < t->data )else if( x > t->data )
return t;
}searchtree delete( int x, searchtree t )
else if( x < t->data )else if( x > t->data )else if( t->left && t->right )else
return t;
}void inorderprint( searchtree t )
}void preorderprint( searchtree t )
}
binarytree.cpp
#include "binarytree.h"
void main()
//for( int i=0; i < n; i++ )
cout << "最大元素:" << findmax( t )->data << endl;
cout << "最小元素:" << findmin( t )->data << endl;
cout << "中序遍歷:";
inorderprint( t );
cout << endl;
cout << "前序遍歷:";
preorderprint( t );
cout << endl;
/* cout << "輸入要刪除的結點:"<< endl;
cin >> x;
//delete( x, t );
t = delete( x, t );
inorderprint( t );
*/ system("pause");
}
BST 二叉搜尋樹原始碼筆記
tree.h typedef int elementtype start fig4 16.txt ifndef tree h define tree h struct treenode 定義結構體節點 typedef struct treenode position 指向節點的指標 typedef ...
c 實現二叉搜尋樹
h部分 ifndef binaryserchtree bst h define binaryserchtree bst h include template class bst 宣告 template class element template class bstnode template cla...
c 實現二叉搜尋樹
c 實現二叉搜尋樹 include include using namespace std struct node class tree tree tree void tree creattree node rt,int n if rt data n else else void tree leve...