演算法導論P151 二叉查詢樹

2021-05-26 08:56:28 字數 1804 閱讀 8426

// 標頭檔案 bstree.h

// cpp檔案 bstree.cpp

// 測試檔案 mainentry.cpp

#include "bstree.h"

int main(int argc, char* argv)

cout << endl;

cout << "inorder bstree: " << endl;

inorderbstree(root);

// test func searchbstree

bstree* tobefound = null;

int valtobefound = 63;

//tobefound = searchbstree(root, valtobefound);

tobefound = searchbstree_v2(root, valtobefound);

if (tobefound != null)

else

// test func findmin

bstree* minnode = null;

//minnode = findmin(root);

minnode = findmin_v2(root);

cout << endl;

cout << "min val: " << minnode->m_nvalue << endl;

// test func findmax

bstree* maxnode = null;

//maxnode = findmax(root);

maxnode = findmax_v2(root);

cout << endl;

cout << "max val: " << maxnode->m_nvalue << endl;

// test func findsuccessor and findpredecessor

bstree* psuccessor = null;

bstree* ppredecessor = null;

int tobeinserted = 49;

insert2bstree_v2(root, tobeinserted);

cout << endl;

cout << "inorder bstree: " << endl;

inorderbstree(root);

psuccessor = findsuccessor(root, tobeinserted);

ppredecessor = findpredecessor(root, tobeinserted);

if (ppredecessor != null)

else

if (psuccessor != null)

else

// test func removefrombstree

bool isremoved = false;

int toberemoved = root->m_nvalue;

isremoved = removefrombstree(root, toberemoved);

if (isremoved)

else

// test func emptybstree

cout << endl;

cout << "empty bstree " << endl;

emptybstree(root);

cout << endl;

delete arr;

arr = null;

return 0;}

// 執行結果

演算法導論 二叉查詢樹

部分實現,用insert生成二叉查詢樹。暫無delete函式。建立查詢樹是用insert逐個插入。比較簡陋,希望有所幫助。includeusing namespace std struct node class searchtree void create void insert node newn...

演算法導論之二叉查詢樹

還要認真學習啊,功力不夠,要多向師兄師姐學習,自己也要努力。includeusing namespace std 首先進行二叉樹的一些操作的 補上之前欠下的東西 class bintree class treenode class treenode int element,treenode lchi...

二叉查詢樹(來自演算法導論)

include using namespace std typedef struct treenode treepoint 採用迴圈而非遞迴的插入樹節點。void tree insert treepoint root,int key else x x right if y null 樹為空時 els...