//二叉树中序遍历
void inorder(b_tree point)
{
if(point!=NULL)
{
inorder(point->left);
printf("%d",point->data);
inorder(point->right);
}
}
//主程序
void main( )
{
b_tree root=NULL;
int i,index;
int value;
int nodelist[20];
printf("\n pleaase input the elements of binary tree(exit for 0 ):\n");
index=0;
//读取数值存到数组中
scanf("%d",&value);
while(value!=0)
{
nodelist[index]=value];
index=index+1;
scanf("%d",&value);
}
//建立二叉树
root=create_btree(nodelist,index);
//中序遍历二叉树
printf("\nThe inorder traversal result is :");
inorder(root);
printf("\n");
}
[/CODE]
include <stdlib.h>
#define Null 0
struct tree_node \\声明二差树节点结构
{
int data;
struct tree_node *left;
struct tree_node *right;
};
typedef struct tree_node tree_list;
typedef tree_list *btree;
struct s_node \\声明堆栈结构
{
btree data;
struct s_node *next;
};
typedef struct s_node s_list;
typedef s_list *link;
btree insert_btree(btree root,int node) \\插入二叉树节点
{
btree new,parent,current;
new=(btree)malloc(sizeof(tree_list));
new->data=node;
new->left=Null;
new->right=Null;
if(root==Null)
{
return new;
}
else
{
current=root;
while(current!=Null)
{
parent=current;
if(new->data<current->data)
{
current=current->left;
}
else
{
current=current->right;
}
}
if(new->data<parent->data)
{
parent->left=new;
}
else
{
parent->right=new;
}
return root;
}
}
btree create_btree(int *nodlist,int len) \\建立二叉树
{
btree root=Null;
int i;
for(i=0;i<len;i++)
{
root=insert_btree(root,nodlist[i]);
}
return root;
}
link push(link stack,btree value) \\入栈函数
{
link new;
new=(link)malloc(sizeof(s_list));
new->data=value;
new->next=Null;
if(stack==Null)
{
stack=new;
return stack;
}
else
{
new->next=stack;
stack=new;
return stack;
}
}
link pop(link stack,btree *value) \\出栈函数
{
link pointer;
pointer=stack;
if(pointer==Null)
{
return Null;
}
else
{
stack=stack->next;
*value=pointer->data;
free(pointer);
return stack;
}
}
void search_btree(btree root) \\非递归层次遍历
{
link stack=Null;
btree value1;
int i=0;
value1=root;
printf("%d\n",value1->data);
while(stack!=Null||value1->left!=Null||value1->right!=Null)
{
if(value1==Null)
{
stack=pop(stack,&value1);
printf("%d\n",value1->data);
}
else if(value1->left!=Null&&value1->right!=Null)
{
printf("%d\n",value1->right->data);
stack=push(stack,value1->left);
value1=value1->right;
}
else if(value1->left==Null&&value1->right==Null)
{
stack=pop(stack,&value1);
if(value1!=Null)
{
printf("%d\n",value1->data);
}
}
else if(value1->left!=Null&&value1->right==Null)
{
stack=push(stack,value1->left);
value1=Null;
}
else if(value1->left==Null&&value1->right!=Null)
{
printf("%d\n",value1->right->data);
value1=value1->right;
}
}
}
void inorder_btree(btree root) \\递归法中序遍历
{
if(root!=Null)
{
inorder_btree(root->left);
printf("%d\n",root->data);
inorder_btree(root->right);
}
}
main()
{
int i,index,data;
int nodlist[16]={6,9,7,4,5,2,1,8,12};
btree root,root1;
link stack;
index=9;
root=create_btree(nodlist,index);
search_btree(root);
getch();
}
void invertorder_btree(btree root) \\递归法后序遍历
{
if(root!=Null)
{
invertorder_btree(root->left);
invertorder_btree(root->right);
printf("%d\n",root->data);
}
}
这个非递归层次遍历还挺难写,靠,想了好一阵才写出来。。。
在下对列位的景仰犹如滔滔江水,连绵不绝,俩字儿:谢谢