c语言二叉树函数,二叉树C语言算法,急!!!!

清华大学 严蔚敏 的都有完整的代码,解释的也很清楚

#include

#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;ileft);

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);

}

}

//在二叉树中查找给定关键字

b_tree lookfor(b_tree root,int e)

{

b_tree p1,p2;

if(root!=NULL)

{

if(root->date==e)

return root;

else

p1=lookfor(root->left,e);

p2=lookfor(root->right,e);

if(p1!=NULL)

return p1;

else

if(p2!=NULL)

return p2;

else

return NULL;

}

else return NULL;

}

///测试函数//

void main()

{

b_tree root=NULL;

int i,index;

int value;

int nodelist[20];

cout>value;

while(value!=0)

{

nodelist[index]=value;

index=index 1;

cin>>value;

}

root=creat(nodelist,index);

printf("

中序打印

");

print1(root);

printf("

后序打印

");

print2(root);

printf("

前序打印

");

print3(root);

printf("

查找的词:

");

int a;

scanf("%d",&a);

b_tree p3=lookfor(root,a);

if(p3!=NULL)

printf("%d

",p3->date);

else

printf("没你要找的词"); }。

全部

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值