输入树的节点,输入0结束
1 2 3 4 5 6 7 8 9 0
中序打印
1->2->3->4->5->6->7->8->9->
后序打印
9->8->7->6->5->4->3->2->1->
前序打印
1->2->3->4->5->6->7->8->9->
//////////////////////////////////////////////////////////////////////////////////////////
#include
#include
typedef struct tree
{
struct tree *left;
int date;
struct tree *right;
}treenode,*b_tree;
///////按顺序插入节点/////////////////////
b_tree insert(b_tree root,int node)
{
b_tree newnode;
b_tree currentnode;
b_tree parentnode;
newnode=(b_tree)malloc(sizeof(treenode));
newnode->date=node;
newnode->right=NULL;
newnode->left=NULL;
if(root==NULL)
return newnode;
else
{
currentnode=root;
while(currentnode!=NULL)
{
parentnode=currentnode;
if(currentnode->date>node)
currentnode=currentnode->left;
else
currentnode=currentnode->right;
}
if(parentnode->date>node)
parentnode->left=newnode;
else
parentnode->right=newnode;
}
return root;
}
//////建立树///////////////////
b_tree creat(int *date,int len)
{
b_tree root=NULL;
int i;
for(i=0;i
return root;
}
//////中序打印////////////////
void print1(b_tree root)
{if(root!=NULL)
{
print1(root->left);
printf("%d->",root->date);
print1(root->right);
}
}
//////后序打印////////////////
void print2(b_tree root)
{if(root!=NULL)
{
print2(root->left);
print2(root->right);
printf("%d->",root->date);
}
}
//////前序打印////////////////
void print3(b_tree root)
{if(root!=NULL)
{ printf("%d->",root->date);
print3(root->left);
print3(root->right);
}
}
///////测试函数//////////////////
void main()
{
b_tree root=NULL;
int i,index;
int value;
int nodelist[20];
printf("输入树的节点,输入0结束\n");
index=0;
scanf("%d",value);
while(value!=0)
{
nodelist[index]=value;
index=index+1;
scanf("%d",value);
}
root=creat(nodelist,index);
printf("\n中序打印\n");
print1(root);
printf("\n后序打印\n");
print2(root);
printf("\n前序打印\n");
print3(root);}
#include
using namespace std;
struct BiNode
{
char data;
BiNode *lchild, *rchild;
};
typedef BiNode *BiTree;
int CreateBiTree(BiTree &T, const char *s1, const char *s2, int len)
{
if (len<=0)
{
T = NULL;
return 1;
}
else
{
T = new BiNode;
T->data = *s1;
int i;
for ( i=0; i
CreateBiTree(T->rchild, s1+i+1, s2+i+1, len-(i+1));
}
return 1;
}
int DestroyBiTree(BiTree &T)
{
if (T==NULL) return 1;
DestroyBiTree(T->lchild);
DestroyBiTree(T->rchild);
delete T;
T = NULL;
return 1;
}
int ATraverse(BiTree &T)
{
if (T==NULL) return 1;
ATraverse(T->lchild);
ATraverse(T->rchild);
cout<
return 1;
}
main()
{
char a[2000],b[2000];
while(cin>>a>>b)
{
BiTree T;
int count=0;
int n;
for(n=0;a[n]!='\0';n++);
CreateBiTree(T,a,b,n);
ATraverse(T);
cout<<" ";
cout<
}
}
//用c++写的,主要因为没有分就不给你改成c了!