C++二叉树
自学数据结构不久,看着课本上的意思自己写的一些二叉树基本操作,还有很多不足,希望看到的大神给些意见。 #include <iostream> #include <cstdio> #include <stack> #include <queue> using namespace std; struct tree { int x; tree * left; tree * right; tree * father; }; class Tree { private : tree *head; public : Tree() { head=nullptr; } bool isEmptyTree()//判断二叉树是否为空 { return (head==nullptr)?true:false; } tree * root()//找到这棵二叉树的根 { return head; } tree * parent (tree * p1)//找出指定二叉树的根节点 { return p1->father; } tree * leftChild(tree * p1) { return p1->left; } tree * rightChild(tree * p1) { return p1->right; } bool creatHead(int num) { if (head==nullptr) { tree * p1=new tree; p1->x=num; p1->left=nullptr; p1->right=nullptr; p1->father=nullptr; head=p1; return true; } return false; } tree * Find(int num1)//查找某个数字是否在二叉树当中 { tree *ff=head; if (head==nullptr) { return nullptr; } else if (ff->x==num1) { return ff; } while(1) { if (ff->x<num1) { ff=ff->left; if (ff==nullptr)return nullptr; if (ff->x==num1)return ff; } else if (ff->x>num1) { ff=ff->right; if (ff==nullptr)return nullptr; if (ff->x==num1)return ff; } } } bool addnum(int num1)//往二叉树中添加数字 { tree * pp=head; if (pp==nullptr) { tree * p=new tree; p->x=num1; p->father=nullptr; p->left=nullptr; p->right=nullptr; head=p; return true; } while(1) { if (num1<=pp->x) { if (pp->left==nullptr) { tree * p=new tree; p->x=num1; p->father=pp; p->left=nullptr; p->right=nullptr; pp->left=p; return true; } pp=pp->left; } else if (num1>pp->x) { if (pp->right==nullptr) { tree * p=new tree; p->x=num1; p->father=pp; p->left=nullptr; p->right=nullptr; pp->right=p; return true; } pp=pp->right; } } return false; } void qianXu()//二叉树的前序遍历 { tree * pp=head; stack<tree*>sta; if (pp!=nullptr) { sta.push(pp); } while(!sta.empty()) { pp=sta.top(); sta.pop(); cout<<pp->x<<endl; if (pp->right!=nullptr) { sta.push(pp->right); } if (pp->left!=nullptr) { sta.push(pp->left); } } } void Zhongxu()//二叉树的中序遍历 { tree * pp=head; if (pp==nullptr)return ; stack<tree*>sta; do { while(pp!=nullptr) { sta.push(pp); pp=pp->left; } pp=sta.top(); cout<<pp->x<<endl; sta.pop(); pp=pp->right; }while(pp!=nullptr||sta.size()!=0); } void Houxu()//二叉树的后序遍历 { struct node { tree * t; int x; }; stack<node>sta; tree * pp=head; if (pp==nullptr)return ; do{ while(pp!=nullptr) { node m; m.t=pp; m.x=1; pp=pp->left; sta.push(m); } while(sta.size()) { node m=sta.top(); sta.pop(); if (m.x==1) { m.x=2; sta.push(m); pp=m.t->right; break; } else cout<<((m.t)->x)<<endl; } }while(sta.size()); } void Houxu2()//相比于前一种后续遍历的另一种方法 { tree * pp=head; stack<tree*>sta; if (pp==nullptr)return ; while(pp!=nullptr||sta.size()) { while(pp!=nullptr) { sta.push(pp); pp=pp->left?(pp->left):(pp->right); } if(sta.size()) { pp=sta.top(); cout<<pp->x<<endl; sta.pop(); } if (!sta.empty()&&(((sta.top())->left)==pp))//最后弹出2,此时左右两侧都已执行完 { pp=(sta.top())->right; } else pp=nullptr; } } void zhouyou()//二叉树的周游 { tree * pp=head; if (pp==nullptr)return; queue<tree*>que; que.push(pp); while(que.size()) { pp=que.front();que.pop(); cout<<pp->x<<endl; if (pp->left!=nullptr)que.push(pp->left); if (pp->right!=nullptr)que.push(pp->right); } } ~Tree() { tree *pp=head; stack<tree*>sta; if (pp!=nullptr) sta.push(pp); while(sta.size()) { pp=sta.top(); sta.pop(); if (pp->right!=nullptr) { sta.push(pp->right); } if (pp->left!=nullptr) { sta.push(pp->left); } delete pp; } } }; int main() { Tree tre; tre.creatHead(5); for (int number=0;number<10;number++) { tre.addnum(number); } cout<<"前序遍历"<<endl; tre.qianXu(); cout<<"中序遍历"<<endl; tre.Zhongxu(); cout<<"后序遍历"<<endl; tre.Houxu(); cout<<"后续遍历2"<<endl; tre.Houxu2(); cout<<"二叉树的广度优先遍历"<<endl; tre.zhouyou(); }