匿名用户
20级
2011-06-23 回答
#include #include int counter; /*计数器*/ struct tree /*声明书的结构*/ {struct tree *left; int data; struct tree *right; }; typedef struct tree treenode; /*声明新类型树的结构*/ typedef treenode *b_tree; /*声明二叉树的链表*/ b_tree insert_node(b_tree root,int node) /*插入二叉树的结点*/ {b_tree newnode; b_tree currentnode; b_tree parentnode; newnode=(b_tree)malloc(sizeof(treenode)); /*分配新结点空间*/ newnode->data=node; newnode->right=NULL; newnode->left=NULL; if(root==NULL)return newnode; else{currentnode=root; while(currentnode!=NULL) {parentnode=currentnode; if(currentnode->data>node) currentnode=currentnode->left; else currentnode=currentnode->right; } if(parentnode->data>node) parentnode->left=newnode; else parentnode->right=newnode; } return root; /*返回根结点的指针*/ } b_tree create_btree(int *data, int len) /*建立二叉树*/ { b_tree root=NULL; int i; for(i=0;ileft); printf("\n%d\n",point->data); /*处理结点内容*/ inorder(point->right); } } int tree_search(b_tree root,int key) /*查找二叉树*/ { b_tree pointer; pointer=root; counter=0; while(pointer!=NULL) { counter++; if(pointer->dataright; else if(pointer->data>key) pointer=pointer->left; else if(pointer->data==key) return 1; } return 0; } void main() { b_tree root=NULL; int m,i,index; /*读入输入值所用的暂存变量*/ float max=0; int nodelist[20]; /*声明存储输入数据之数组*/ int value; printf("\nPlease input the elements of binary tree(exit for 0):\n"); index=0; scanf("%d",&value); /*读取输入值放到变量value*/ while(value!=0) {nodelist[index]=value; index=index+1; scanf("%d",&value); } printf("%d",index); root=create_btree(nodelist,index); printf(" \n the inorder traversal result is :"); inorder(root); printf("\n"); for(m=0;m