二叉樹的幾種遍歷方式-創(chuàng)新互聯(lián)

#include
#include
#include
using namespace std;
template
class BinaryTreeNode {
?private:
??? ?T element;
??? ?BinaryTreeNode*leftChild;
??? ?BinaryTreeNode*rightChild;
?public:
??? ?BinaryTreeNode() {};
??? ?BinaryTreeNode(const T& ele):element(ele),leftChild(NULL),rightChild(NULL) {};
??? ?BinaryTreeNode(const T& ele,BinaryTreeNode*l,BinaryTreeNode*r);
??? ?BinaryTreeNode* getLeftChild()const {
??? ??? ?return leftChild;
??? ?};
??? ?BinaryTreeNode* getRightChild()const {
??? ??? ?return rightChild;
??? ?};
??? ?void setLeftChild(BinaryTreeNode*l) {
??? ??? ?leftChild=l;
??? ?};
??? ?void setRightChild(BinaryTreeNode*r) {
??? ??? ?rightChild=r;
??? ?};
??? ?void createLeftChild();
??? ?void createRightChild();
??? ?T getvalue() const {
??? ??? ?return element;
??? ?};
??? ?void setvalue(const T& val) {
??? ??? ?element=val;
??? ?};
??? ?bool isLeaf()const;
};
template
class BinaryTree {

創(chuàng)新互聯(lián)的客戶來自各行各業(yè),為了共同目標(biāo),我們在工作上密切配合,從創(chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對我們的要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。專業(yè)領(lǐng)域包括成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、電商網(wǎng)站開發(fā)、微信營銷、系統(tǒng)平臺(tái)開發(fā)。

?public:
??? ?BinaryTreeNode* root;
??? ?BinaryTree();
??? ?BinaryTree(BinaryTreeNode*a):root(a) {
??? ?};
??? ?bool isEmpty() const;
??? ?BinaryTreeNode* getRoot() {
??? ??? ?return root;
??? ?};
??? ?BinaryTreeNode* getparent(BinaryTreeNode* current)const;
??? ?BinaryTreeNode* getLeftsibling(BinaryTreeNode* current)const;
??? ?void levelOrder(BinaryTreeNode*root);
??? ?void preOrder(BinaryTreeNode*root);
??? ?void PreOrderWithoutRecursion(BinaryTreeNode*root);
??? ?void inOrder(BinaryTreeNode*root);
??? ?void InOrderWithoutRecursion(BinaryTreeNode*root);
??? ?void postorder(BinaryTreeNode*root);
??? ?void PostOrderWithoutRecursion(BinaryTreeNode*root);
??? ?void visit(BinaryTreeNode* t);
??? ?void PreInBuild(T* a,T* b,int num);
??? ?void Getroot(BinaryTreeNode*a);
??? ?void maketree(BinaryTree* b1,BinaryTree* b2);
};
template
void BinaryTree::Getroot(BinaryTreeNode*a) {
?root=a;
}
template
void BinaryTree::maketree(BinaryTree* b1,BinaryTree* b2) {
?this->root->setLeftChild(b1->getRoot());
?this->root->setRightChild(b2->getRoot());
}
template
void BinaryTree::levelOrder(BinaryTreeNode*root) {
?queue*>nodeQueue;
?BinaryTreeNode*pointer=root;
?if(pointer)
??? ?nodeQueue.push(pointer);
?while(!nodeQueue.empty() ) {
??? ?pointer=nodeQueue.front();
??? ?visit(pointer);
??? ?nodeQueue.pop();
??? ?if(pointer->getLeftChild())
??? ??? ?nodeQueue.push(pointer->getLeftChild());
??? ?if(pointer->getRightChild())
??? ??? ?nodeQueue.push(pointer->getRightChild());
?}
}
template
void BinaryTree::preOrder(BinaryTreeNode*root) {
?if(root!=NULL) {
??? ?visit(root);
??? ?preOrder(root->getLeftChild());
??? ?preOrder(root->getRightChild());
?}
}
template
void BinaryTree::PreOrderWithoutRecursion(BinaryTreeNode*root) {
?stack*>nodeStack;
?BinaryTreeNode*pointer=root;
?while(!nodeStack.empty()||pointer) {
??? ?if(pointer) {
??? ??? ?visit(pointer);
??? ??? ?if(pointer->getRightChild()!=NULL)
??? ??? ??? ?nodeStack.push(pointer->getRightChild());
??? ??? ?pointer=pointer->getLeftChild();
??? ?} else {
??? ??? ?pointer=nodeStack.top();
??? ??? ?nodeStack.pop();
??? ?}
?}
}
template
void BinaryTree::inOrder(BinaryTreeNode*root) {
?if(root!=NULL) {
??? ?inOrder(root->getLeftChild());
??? ?visit(root);
??? ?inOrder(root->getRightChild());
?}
}
template
void BinaryTree::InOrderWithoutRecursion(BinaryTreeNode*root) {
?stack*>nodeStack;
?BinaryTreeNode*pointer = root;
?while(!nodeStack.empty() ||pointer) {
??? ?if(pointer) {
??? ??? ?nodeStack.push(pointer);
??? ??? ?pointer = pointer->getLeftChild();
??? ?} else {
??? ??? ?pointer= nodeStack.top();
??? ??? ?visit(pointer);
??? ??? ?pointer= pointer->getRightChild();
??? ??? ?nodeStack.pop();
??? ?}
?}
}
template
void BinaryTree::postorder(BinaryTreeNode*root) {
?if(root!=NULL) {
??? ?postorder(root->getLeftChild());
??? ?postorder(root->getRightChild());
??? ?visit(root);
?}
}
template
void BinaryTree::PostOrderWithoutRecursion(BinaryTreeNode*root) {
?stack* >nodeStack;
?BinaryTreeNode*pointer =root;
?BinaryTreeNode*pre =root;
?while(pointer) {
??? ?for(; pointer->getLeftChild() != NULL; pointer = pointer->getLeftChild())
??? ??? ?nodeStack.push (pointer);
??? ?while(pointer != NULL && (pointer->getRightChild() == NULL || pointer->getRightChild() == pre)) {
??? ??? ?visit(pointer);
??? ??? ?pre = pointer;
??? ??? ?if(nodeStack.empty())
??? ??? ??? ?return;
??? ??? ?pointer = nodeStack.top();
??? ??? ?nodeStack.pop();
??? ?}
??? ?nodeStack.push(pointer);
??? ?pointer = pointer->getRightChild();
?}
}
template
void BinaryTree::visit(BinaryTreeNode* t) {
?cout<getvalue()<<" ";
}
template
void BinaryTree::PreInBuild(T* pre,T* in,int num) {
}
int main() {
?BinaryTreeNode*x=new ?BinaryTreeNode(1);
?BinaryTreeNode*s=new ?BinaryTreeNode(4);
?BinaryTreeNode*t=new ?BinaryTreeNode(5);
?BinaryTreeNode*r=new ?BinaryTreeNode(2);
?r->setRightChild(t);
?r->setLeftChild(s);
?BinaryTreeNode*u=new ?BinaryTreeNode(3);
?BinaryTreeNode*v=new ?BinaryTreeNode(6);
?u->setRightChild(v);
?BinaryTree* b1=new BinaryTree(r);
?BinaryTree* b2=new BinaryTree(u);
?BinaryTree* b3=new BinaryTree(x);
?b3->maketree(b1,b2);
?cout<<"廣度優(yōu)先 ?:";?
?b3->levelOrder(x);
?cout< ?cout<<"前序遞歸 ?:";
?b3->preOrder(x);
?cout< ?cout<<"前序非遞歸:";
?b3->PreOrderWithoutRecursion(x);
?cout< ?cout<<"中序遞歸 ?:";?
?b3->inOrder(x);
?cout< ?cout<<"中序非遞歸:";
?b3->InOrderWithoutRecursion(x);
?cout< ?cout<<"后序遞歸 ?:";
?b3->postorder(x);
?cout< ?cout<<"后序非遞歸:";
?b3->PostOrderWithoutRecursion(x);
}

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁題目:二叉樹的幾種遍歷方式-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://bm7419.com/article32/gohpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站建設(shè)外貿(mào)網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都app開發(fā)公司