#include <stdlib.h> #include <stdio.h> typedef struct btnode { int data; struct btnode *lchild,*rchild; }btnode; int b[100]; //在二叉树中插入元素 btnode *insertnode(btnode *root,int node) { btnode *newnode; btnode *currentnode; btnode *parentnode; newnode=(btnode*)malloc(sizeof(btnode)); newnode->lchild=NULL; newnode->rchild=NULL; newnode->data=node; if (root==NULL) return newnode; else { currentnode=root; while(currentnode!=NULL) { parentnode=currentnode; if (currentnode->data>node) currentnode=currentnode->lchild; else currentnode=currentnode->rchild; } if (parentnode->data>node) parentnode->lchild=newnode; else parentnode->rchild=newnode; } return root; } //建立二叉树 btnode* createtree(int *s,int len) { btnode* root=NULL; for (int i=0;i<len;++i) root=insertnode(root,s[i]); return root; } //先序遍历输出二叉树 void preorder(btnode *root) { if (root==NULL) return; else { printf ("%d",root->data); preorder(root->lchild); preorder(root->rchild); } } int main() { int s[20]; btnode* root; root=NULL; int len=0; int n; scanf("%d",&n); while(n!=0) { s[len]=n; len++; scanf("%d",&n); } root=createtree(s,len); printf ("the tree is created! "); preorder(root); return 0; }