#include<stdio.h>
#include<stdlib.h>
#include<queue>
#include <iostream>
#define MAXSIZE 10010
#define ElemType int
using namespace std;
typedef struct BTNode{
ElemType data;
BTNode *lchild,*rchild;
}*BTree;
BTree CreateTree(){
ElemType ch;
printf("输入结点元素值:");
scanf("%d",&ch);
if(ch == 0)
return NULL;
else{
BTree tree=(BTree)malloc(sizeof(BTree));
tree->data=ch;
printf("%d 结点左子树\n",ch);
tree->lchild=CreateTree();
printf("%d 结点右子树\n",ch);
tree->rchild=CreateTree();
return tree;
}
}
void LayeredOrderTravel(BTree tree){
queue<BTree> queue1;
BTree p;
if(tree){
queue1.push(tree);
while(!queue1.empty()){
p=queue1.front();
queue1.pop();
printf("%d ",p->data);
if(p->lchild)
queue1.push(p->lchild);
if(p->rchild)
queue1.push(p->rchild);
}
}
}
void ChangeChildTree(BTree tree){
queue<BTree> queue1;
BTree p,temp;
if(tree){
queue1.push(tree);
while(!queue1.empty()){
p=queue1.front();
queue1.pop();
temp=p->rchild;
p->rchild=p->lchild;
p->lchild=temp;
if(p->lchild)
queue1.push(p->lchild);
if(p->rchild)
queue1.push(p->rchild);
}
}
}
int main(){
printf("二叉树的建立\n");
BTree tree;
tree=CreateTree();
printf("二叉树的层序遍历\n");
LayeredOrderTravel(tree);
printf("\n交换左右子树\n");
ChangeChildTree(tree);
printf("二叉树的层序遍历\n");
LayeredOrderTravel(tree);
return 0;
}