#include <iostream>
#include <stack>
using namespace std;
template <class T>
class BinNode{
public:
T data;
BinNode<T>* leftChild;
BinNode<T>* rightChild;
public:
BinNode(T data, BinNode<T>* leftChild=nullptr, BinNode<T>* rightChild=nullptr){
this->data = data;
this->leftChild = leftChild;
this->rightChild = rightChild;
}
};
template <class T>
class BinaryTree{
public:
BinNode<T>* root;
public:
//构造函数,创建一个空的二叉树
BinaryTree(){
root = nullptr;
}
BinaryTree(T data){
root = new BinNode<T>(data);
}
~BinaryTree(){
delete root;
}
bool isEmpty(){
return root == nullptr;
}
void makeTree(T data, BinaryTree<T>& left, BinaryTree<T>& right){
root = new BinNode<T>(data, left.root, right.root);
}
//输入为扩充二叉树前序遍历序列,空树时输入为0
void createBinTree(BinNode<T>*& root){
T data;
cin >> data;
if(data){
root = new BinNode<T>(data);
createBinTree(root->leftChild);
createBinTree(root->rightChild);
}
else{
root = nullptr;
}
}
//前序
void preOrder(BinNode<T>*& root){
BinNode<T>* cur = root;
if(cur){
cout << cur->data << endl;
preOrder(cur->leftChild);
preOrder(cur->rightChild);
}
}
//前序非递归
void preOrder_2(BinNode<T>*& root){
BinNode<T>* cur = root;
stack<BinNode<T>*> stack;
while(cur || !stack.empty()){
if(cur){
cout << cur->data << endl;
stack.push(cur->rightChild);//沿路将右子树记录下来表示未曾访问
cur = cur->leftChild;
}
else{//遇到空树则回溯处理右子树
cur = stack.top();
stack.pop();
}
}
}
//中序
void inOrder(BinNode<T>*& root){
BinNode<T>* cur = root;
if(cur){
inOrder(cur->leftChild);
cout << cur->data << endl;
inOrder(cur->rightChild);
}
}
//中序非递归
void inOrder_2(BinNode<T>*& root){
BinNode<T>* cur = root;
stack<BinNode<T>*> stack;
while(cur || !stack.empty()){
if(cur){
stack.push(cur);
cur = cur->leftChild;
}
else{
cur = stack.top();
stack.pop();
cout << cur->data << endl;
cur = cur->rightChild;
}
}
}
//后序
void postOrder(BinNode<T>*& root){
BinNode<T>* cur = root;
if(cur){
postOrder(cur->leftChild);
postOrder(cur->rightChild);
cout << cur->data << endl;
}
}
//后序非递归
void postOrder_2(BinNode<T>* root){
BinNode<T>* cur = root;
stack<BinNode<T>*> stack;
while(cur || !stack.empty()){
if(cur){
stack.push(cur);
//能左就左,否则向右
if(cur->leftChild)
cur = cur->leftChild;
else
cur = cur->rightChild;
}
else{
cur = stack.top();
stack.pop();
cout << cur->data << endl;
if(!stack.empty() && stack.top()->leftChild == cur)
//如果被访问的结点是其父的左结点,直接转到其右兄弟结点继续
cur = stack.top()->rightChild;
else
//如果被访问的结点是其父的右结点,设cur为空,强制访问更上一层结点
cur = nullptr;
}
}
}
int height(BinNode<T>*& root){
if(root){
int hl = height(root->leftChild);
int hr = height(root->rightChild);
return hl>hr?hl+1:hr+1;
}
else{
return 0;
}
}
int size(BinNode<T>*& root){
if(root){
int sl = size(root->leftChild);
int sr = size(root->rightChild);
return sl+sr+1;
}
else{
return 0;
}
}
};
int main(){
// BinaryTree<int> tree1, tree2(2), tree3(3), tree4, tree5;
// tree1.makeTree(1, tree2, tree3);
// tree4.makeTree(4, tree1, tree5);
// tree4.postOrder(tree4.root);
BinaryTree<int> tree;
tree.createBinTree(tree.root);
tree.postOrder(tree.root);
tree.postOrder_2(tree.root);
return 0;
}
二叉树C++版
猜你喜欢
转载自blog.csdn.net/vaemusicsky/article/details/81490274
今日推荐
周排行