// BTree.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream"
#include "stack"
using namespace std;
typedef struct _node
{
int data;
struct _node* rch;
struct _node* lch;
}Node;
Node* InsertNode(Node* n,int d)
{
if(n)
{
if(d>n->data)
n->rch=InsertNode(n->rch,d);
else n->lch=InsertNode(n->lch,d);
}
else
{
n=(Node*)malloc(sizeof(Node));
n->data=d;
n->lch=NULL;
n->rch=NULL;
}
return n;
}
void CreateTree(Node* root,int arr[],int len)
{
for(int i=0;i<len;i++)
{
InsertNode(root,arr[i]);
}
}
void SwapChild(Node* root)
{
stack<Node*> s;
s.push(root);
while(!s.empty())
{
Node* p=s.top();
s.pop();
if(p->lch||p->rch)
{
Node* temp=p->lch;
p->lch=p->rch;
p->rch=temp;
}
if(p->lch)
s.push(p->lch);
if(p->rch)
s.push(p->rch);
}
}
void InorderTree(Node* root)
{
if(root)
{
InorderTree(root->lch);
cout<<root->data<<" ";
InorderTree(root->rch);
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[]={12,34,1,78,54,89};
Node* head=(Node*)malloc(sizeof(Node));
head->data=0;
head->lch=NULL;
head->rch=NULL;
CreateTree(head,a,6);
SwapChild(head);
InorderTree(head);
cin.get();
return 0;
}