二叉查找树的遍历和插入
#include<stdio.h> #include<stdlib.h> struct tree{ int ele; struct tree* left; struct tree* right; }; typedef struct tree* pointer; typedef struct tree trees; void insert(pointer *T1,int interag){ pointer prev; pointer T=*T1; if(T==NULL) { (*T1)=(pointer)malloc(sizeof(trees)); (*T1)->ele=interag; (*T1)->left=(*T1)->right=NULL; } else { while(T!=NULL) { if(interag<T->ele) { prev=T; T=T->left;} else { prev=T; T=T->right;}} if(interag<prev->ele) { prev->left=(pointer)malloc(sizeof(trees)); prev->left->ele=interag; prev->left->left=prev->left->right=NULL; } else { prev->right=(pointer)malloc(sizeof(trees)); prev->right->ele=interag; prev->right->left=prev->right->right=NULL; }} } void printftree(pointer T) { if(T!=NULL){ printftree(T->left); printf("%d",T->ele); printftree(T->right); } } int main(void) { pointer T=NULL; insert(&T,3); insert(&T,2); insert(&T,4); insert(&T,1); insert(&T,5); printftree(T); }