C 模板實現平衡二叉樹基本操作

2021-10-10 11:54:00 字數 4830 閱讀 5189

#include #include #include #include using namespace std;

templatestruct bintreenode

;templateclass bintree

bintreenode* getroot();

bool init(vectorvecdata);

bool insertnode(bintreenode*& root, datatype data);

void beforeordertrv(bintreenode* root);

void midordertrv(bintreenode* root);

void afterordertrv(bintreenode* root);

void layerordertrv();

void layerordertrv_queue(); //層序遍歷_佇列實現

void beforeordertrv_stack();

void midordertrv_stack();

void afterordertrv_stack_from_before();

void destory(bintreenode* root);

// 翻轉--遞迴/非遞迴(棧/佇列)

bintreenode* revbintree(bintreenode* root);

bintreenode* revbintree();

// 高度--遞迴/非遞迴(先續/層序)

uint32_t gettreeheight(bintreenode* root);

uint32_t gettreeheight();

bintreenode* findnode(datatype data);

// 鋸齒遍歷

vector> zigzaglevelorder(bintreenode* root);

private:

bintreenode* root_;

uint32_t size_;

};templatebintreenode* bintree::findnode(datatype data)

bintreenode* proot = root_;

while (proot)

return nullptr;

}templatebintreenode* bintree::getroot()

templatebool bintree::init(vectorvecdata) }}

templatebool bintree::insertnode(bintreenode*& root, datatype data)

root->data = data;

root->left = root->right = nullptr;

size_++;

return true; }

if (data == root->data)

else if (data > root->data)

else }

templatevoid bintree::beforeordertrv(bintreenode* root)

}templatevoid bintree::midordertrv(bintreenode* root)

}templatevoid bintree::afterordertrv(bintreenode* root)

}// 層序遍歷_陣列

templatevoid bintree::layerordertrv()

uint32_t in = 0;

uint32_t out = 0;

temp[in++] = root_; //儲存根節點

while (in > out)

out++;

} if (temp) }

// 層序遍歷_佇列

templatevoid bintree::layerordertrv_queue()

while (q.empty() == false)

if (q.front()->right != nullptr)

q.pop(); }}

templatevoid bintree::beforeordertrv_stack()

bintreenode* proot = root_;

stack*> s;

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

else

}}templatevoid bintree::midordertrv_stack()

bintreenode* proot = root_;

stack*> s;

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

else

}} // 思路:先根特殊處理再倒序

templatevoid bintree::afterordertrv_stack_from_before()

stackres;

bintreenode* proot = root_;

stack*> s;

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

else

}while (!res.empty())

cout << endl;

}templatevoid bintree::destory(bintreenode* root) // 後根遍歷

destory(root->left);

destory(root->right);

delete root;

root = nullptr;

}templateuint32_t bintree::gettreeheight(bintreenode* root)

uint32_t leftheight = gettreeheight(root->left);

uint32_t rightheight = gettreeheight(root->right);

return (leftheight > rightheight ? leftheight : rightheight) + 1;

}templatebintreenode* bintree::revbintree(bintreenode* root)

bintreenode* ptmp = root->left;

root->left = root->right;

root->right = ptmp;

revbintree(root->left);

revbintree(root->right);

}templateuint32_t bintree::gettreeheight() // 層次

while (q.empty() == false)

if (q.front()->right != nullptr)

q.pop();

} return left_height > right_height ? left_height : right_height;

}templatebintreenode* bintree::revbintree() //反轉非遞迴_佇列

return root_;

}templatevector> bintree::zigzaglevelorder(bintreenode* root) // 鋸齒遍歷

++flag;

if (1 == flag % 2)

if (0 == flag % 2)

}return res;

}int main()

; b.init(v);

/* b.beforeordertrv(b.getroot());

cout << endl;

b.midordertrv(b.getroot());

cout << endl;

b.afterordertrv(b.getroot());

cout << endl;

bintreenode* node = b.findnode(11);

if (node)

else

b.layerordertrv();

cout << endl;

b.layerordertrv_queue();

cout << endl;

cout << b.gettreeheight(b.getroot()) << endl;

b.revbintree(b.getroot());

b.layerordertrv();

cout << endl;

b.revbintree(b.getroot());

cout << endl;

b.midordertrv_stack();

cout << endl;

b.beforeordertrv_stack();

cout << endl;

b.afterordertrv_stack_from_before();

cout << b.gettreeheight() << endl;

b.revbintree();

b.layerordertrv();

cout << endl;

*/ b.layerordertrv_queue();

cout << endl;

vector> v1 = b.zigzaglevelorder(b.getroot());

for (int i = 0; i < v1.size(); ++i) }

cout << endl;

return 0;

}

二叉查詢樹 模板實現 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...

C 模板實現二叉搜尋樹

二叉搜尋樹的增刪該查操作實現 插入操作 借助輔助遞迴函式node insert node node,key key,value value 像根節點為node的二叉搜尋樹插入乙個資料,返回值為根節點,如果node null,那麼新建乙個節點返回,如果keykey,則插入到左子樹,當前返回的根節點為左...