真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

二叉樹(shù)的幾種遍歷方式-創(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)的客戶來(lái)自各行各業(yè),為了共同目標(biāo),我們?cè)诠ぷ魃厦芮信浜?,從?chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對(duì)我們的要求,感謝他們從不同領(lǐng)域給我們帶來(lái)的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來(lái)驚喜。專業(yè)領(lǐng)域包括成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)、電商網(wǎng)站開(kāi)發(fā)、微信營(yíng)銷、系統(tǒng)平臺(tái)開(kā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級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧


名稱欄目:二叉樹(shù)的幾種遍歷方式-創(chuàng)新互聯(lián)
URL鏈接:http://weahome.cn/article/gohpc.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部