二叉树C语言算法,急!!!!

用函数实现如下算法:
(1) 插入新结点(创建二叉排序树)
(2) 前序、中序、后序遍历二叉树
(3) 中序遍历的非递归算法
(4) 层次遍历二叉树
(5) 在二叉树中查找给定关键字
==========================================
用C或C++
要求有注释(当然的越清楚越好啦!)
本人的实验,非常急!!!~`~~~~````~~~`~~`~~~~`
只有50分,全给你了~~````

清华大学 严蔚敏 的<数据结构里>都有完整的代码,解释的也很清楚

#include<iostream>
#include<stdlib.h>
#include<stdio.h>

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<len;i++)
root=insert(root,date[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);

}
}
//////////在二叉树中查找给定关键字 ////////////
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<<"输入树的节点,输入0结束\n";
index=0;
cin>>value;
while(value!=0)
{
nodelist[index]=value;
index=index+1;
cin>>value;
}
root=creat(nodelist,index);
printf("\n中序打印\n");
print1(root);
printf("\n后序打印\n");
print2(root);
printf("\n前序打印\n");
print3(root);

printf("\n查找的词:\n");
int a;
scanf("%d",&a);
b_tree p3=lookfor(root,a);
if(p3!=NULL)
printf("%d\n",p3->date);
else
printf("没你要找的词"); }
温馨提示:答案为网友推荐,仅供参考
第1个回答  2007-11-17
呵呵,楼主都“倾家荡产”了
估计这个程序都简单得没人愿意写了,而且网上到处都是,自己用百度google一下吧,很多的