二叉树的几种遍历方式-创新互联
#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<<"广度优先 :";
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);
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前文章:二叉树的几种遍历方式-创新互联
转载来源:http://ybzwz.com/article/gohpc.html