#include
#include
#include
using namespace std;
template
class BinaryTreeNode {
?private:
??? ?T element;
??? ?BinaryTreeNode
??? ?BinaryTreeNode
?public:
??? ?BinaryTreeNode() {};
??? ?BinaryTreeNode(const T& ele):element(ele),leftChild(NULL),rightChild(NULL) {};
??? ?BinaryTreeNode(const T& ele,BinaryTreeNode
??? ?BinaryTreeNode
??? ??? ?return leftChild;
??? ?};
??? ?BinaryTreeNode
??? ??? ?return rightChild;
??? ?};
??? ?void setLeftChild(BinaryTreeNode
??? ??? ?leftChild=l;
??? ?};
??? ?void setRightChild(BinaryTreeNode
??? ??? ?rightChild=r;
??? ?};
??? ?void createLeftChild();
??? ?void createRightChild();
??? ?T getvalue() const {
??? ??? ?return element;
??? ?};
??? ?void setvalue(const T& val) {
??? ??? ?element=val;
??? ?};
??? ?bool isLeaf()const;
};
template
class BinaryTree {
?public:
??? ?BinaryTreeNode
??? ?BinaryTree();
??? ?BinaryTree(BinaryTreeNode
??? ?};
??? ?bool isEmpty() const;
??? ?BinaryTreeNode
??? ??? ?return root;
??? ?};
??? ?BinaryTreeNode
??? ?BinaryTreeNode
??? ?void levelOrder(BinaryTreeNode
??? ?void preOrder(BinaryTreeNode
??? ?void PreOrderWithoutRecursion(BinaryTreeNode
??? ?void inOrder(BinaryTreeNode
??? ?void InOrderWithoutRecursion(BinaryTreeNode
??? ?void postorder(BinaryTreeNode
??? ?void PostOrderWithoutRecursion(BinaryTreeNode
??? ?void visit(BinaryTreeNode
??? ?void PreInBuild(T* a,T* b,int num);
??? ?void Getroot(BinaryTreeNode
??? ?void maketree(BinaryTree* b1,BinaryTree* b2);
};
template
void BinaryTree
?root=a;
}
template
void BinaryTree
?this->root->setLeftChild(b1->getRoot());
?this->root->setRightChild(b2->getRoot());
}
template
void BinaryTree
?queue
?BinaryTreeNode
?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
?if(root!=NULL) {
??? ?visit(root);
??? ?preOrder(root->getLeftChild());
??? ?preOrder(root->getRightChild());
?}
}
template
void BinaryTree
?stack
?BinaryTreeNode
?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
?if(root!=NULL) {
??? ?inOrder(root->getLeftChild());
??? ?visit(root);
??? ?inOrder(root->getRightChild());
?}
}
template
void BinaryTree
?stack
?BinaryTreeNode
?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
?if(root!=NULL) {
??? ?postorder(root->getLeftChild());
??? ?postorder(root->getRightChild());
??? ?visit(root);
?}
}
template
void BinaryTree
?stack
?BinaryTreeNode
?BinaryTreeNode
?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
?cout<
}
template
void BinaryTree
}
int main() {
?BinaryTreeNode
?BinaryTreeNode
?BinaryTreeNode
?BinaryTreeNode
?r->setRightChild(t);
?r->setLeftChild(s);
?BinaryTreeNode
?BinaryTreeNode
?u->setRightChild(v);
?BinaryTree
?BinaryTree
?BinaryTree
?b3->maketree(b1,b2);
?cout<<"廣度優(yōu)先 ?:";?
?b3->levelOrder(x);
?cout<
?b3->preOrder(x);
?cout<
?b3->PreOrderWithoutRecursion(x);
?cout<
?b3->inOrder(x);
?cout<
?b3->InOrderWithoutRecursion(x);
?cout<
?b3->postorder(x);
?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)查看詳情吧