您的位置:首页 > 其它

二叉树的实现

2015-10-26 14:14 288 查看
功能

1. 前、中、后序方法建立二叉树。

2. 实现二叉树前、中、后序遍历(包括递归与非递归算法)。

3. 实现二叉树的层次遍历。

4. 交换二叉树的左右子数。

5. 求二叉树的叶子总数,高度。

#include <iostream>
using namespace std;
#include <stack>
#include <queue>
struct BinTreeNode{
int data;
BinTreeNode *leftChild,*rightChild;
};

void preOrder(BinTreeNode *t){
if(t!=0){cout<<t->data<<" ";
preOrder(t->leftChild);
preOrder(t->rightChild);
}
}

void preOrderWithoutRecursion(BinTreeNode *t){
/*:如果二叉树非空,则
(1)将二叉树的根结点作为当前结点。
(2)若当前结点非空,则先输出该结点,并将该结点进栈,再将其左孩子结点作为当前结点,重复步骤(2),直到当前结点为空为止。
(3)若栈非空,则将栈顶结点出栈,并将该结点的右孩子结点作为当前结点。
(4)重复步骤(2), (3),直到栈为空且当前结点为空为止。*/
stack<BinTreeNode*> s;
while(!s.empty()||t!=NULL){
while(t!=NULL){cout<<t->data<<" ";s.push(t);t=t->leftChild;}
if(!s.empty()){t=s.top();s.pop();t=t->rightChild;}
}
}

void inOrder(BinTreeNode *t){
if(t!=NULL){
inOrder(t->leftChild);
cout<<t->data<<" ";
inOrder(t->rightChild);
}
}

void inOrderWithoutRecursion(BinTreeNode *t){
/*如果二叉树非空,则
(1)将二叉树的根结点作为当前结点;
(2)若当前结点非空,则该结点进栈并将其左孩子结点作为当前结点,重复步骤(2),直到当前结点为空为止;
(3)若栈非空,则将栈顶结点出栈并作为当前结点,接着访问当前结点,再将当前结点的右孩子结点作为当前结点;
(4)重复步骤(2)、(3),直到栈为空且当前结点为空为止。*/
stack<BinTreeNode*> s;
while(!s.empty()||t!=NULL){
while(t!=NULL){s.push(t);t=t->leftChild;}
if(!s.empty()){t=s.top();s.pop();cout<<t->data<<" ";t=t->rightChild;}
}
}
void postOrder(BinTreeNode *t){
if(t!=NULL){
postOrder(t->leftChild);
postOrder(t->rightChild);
cout<<t->data<<" ";
}
}

void postOrderWithoutRecursion(BinTreeNode *t){
//用非递归算法进行后序遍历二叉树时,一个结点需要进栈,出栈各两次。为了区别同一个结点的两次进栈,需要设置一个标志flag, flag=0为第一次进栈,表示该结点出栈之后不能访问;flag=1为第二次进栈,表示该结点出栈之后可以访问
stack<BinTreeNode*> s;
stack<int> num;int flag;
while(!s.empty()||t!=NULL){
while(t!=NULL){s.push(t);num.push(0);t=t->leftChild;}
if(!s.empty()){
t=s.top();s.pop();flag=num.top();num.pop();
if(flag==1){cout<<t->data<<" ";t=0;}
else{s.push(t);num.push(1);t=t->rightChild;}
}
}
}

void levelOrder(BinTreeNode *t){
/*层次遍历的过程:
(1)遍历进行之前先将二叉树的根结点存入队列中。
(2)然后依次从队列中取出队头结点,每取出一个结点,都先访问该结点。
(3)接着分别检查该结点是否存在左、右孩子,若存在则先后入列。
(4)如此反复,直到队列为空为止。*/
queue<BinTreeNode* > q; q.push(t);
while (!q.empty()){
t=q.front();
q.pop();
cout<<t->data<<" ";
if(t->leftChild!=NULL) q.push(t->leftChild);
if(t->rightChild!=NULL) q.push(t->rightChild);
}
}

int size(BinTreeNode *t){
if(t==0) return 0;
else if(t->leftChild==0&&t->rightChild==0) return 1;
else return size(t->rightChild)+size(t->leftChild);
}

int height(BinTreeNode *t){
if(t==0) return 0;
else{
int LeftHeight = height(t->leftChild);
int RightHeight = height(t->rightChild);
return LeftHeight>RightHeight?LeftHeight+1:RightHeight+1;
}
}

BinTreeNode * preOrderCreate(int value){
BinTreeNode *current;
int i;cin>>i;
if(i==value) return NULL;
else{
current = new BinTreeNode;
current->data=i;
current->leftChild=preOrderCreate(value);
current->rightChild=preOrderCreate(value);
return current;
}
}

BinTreeNode * inOrderCreate(int value){
BinTreeNode *current,*q;
int i;cin>>i;
if(i==value) return NULL;
else{
q=inOrderCreate(value);
current = new BinTreeNode;
current->data=i;
current->leftChild=q;
current->rightChild=inOrderCreate(value);
return current;
}
}

BinTreeNode * postOrderCreate(int value){
BinTreeNode *current,*l,*r;
int i;cin>>i;
if(i==value) return NULL;
else{
l=postOrderCreate(value);
r=postOrderCreate(value);
current = new BinTreeNode;
current->data=i;
current->leftChild=l;
current->rightChild=r;
return current;
}
}

void swop(BinTreeNode *t){
BinTreeNode *temp;
if(t->leftChild!=NULL&&t->rightChild!=NULL){
temp=t->leftChild;t->leftChild=t->rightChild;t->rightChild=temp;
swop(t->leftChild);swop(t->rightChild);
}
}

void nodePrint(BinTreeNode *t){
if(t){
cout<<t->data<<" ";
nodePrint(t->leftChild);
nodePrint(t->rightChild);
}
}

int main(){
cout<<"**************输入数字实现对应功能**************"<<endl;
cout<<" 1. 前序建立一棵二叉树"<<endl;
cout<<" 2. 前序建立一棵二叉树"<<endl;
cout<<" 3. 前序建立一棵二叉树"<<endl;
cout<<" 4. 前序遍历递归算法"<<endl;
cout<<" 5. 前序遍历非递归算法"<<endl;
cout<<" 6. 中序遍历递归算法"<<endl;
cout<<" 7. 中序遍历非递归算法"<<endl;
cout<<" 8. 后序遍历递归算法"<<endl;
cout<<" 9. 后序遍历非递归算法"<<endl;
cout<<" 10. 层次遍历算法"<<endl;
cout<<" 11. 求树高"<<endl;
cout<<" 12. 求叶子总数"<<endl;
cout<<" 13. 输出二叉树"<<endl;
cout<<" 14. 交换左右子数"<<endl;
cout<<" 15. 退出"<<endl;
BinTreeNode *root;
int i;
while(cin>>i){
switch (i) {
case 1:root = preOrderCreate(-1);break;
case 2:root = inOrderCreate(-1);break;
case 3:root = postOrderCreate(-1);break;
case 4: preOrder(root);break;
case 5: preOrderWithoutRecursion(root);break;
case 6: inOrder(root);break;
case 7: inOrderWithoutRecursion(root);break;
case 8: postOrder(root);break;
case 9: postOrderWithoutRecursion(root);break;
case 10:levelOrder(root);break;
case 11:cout<<"Height: "<<height(root)<<endl;break;
case 12:cout<<"Size: "<<size(root)<<endl;break;
case 13:nodePrint(root);break;
case 14:swop(root);break;
case 15:exit(0);break;

}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: