二叉排序树-动态查找表

来源:互联网 发布:网络摄影比赛 编辑:程序博客网 时间:2024/04/29 23:05
#include<stdio.h>
typedef struct treenode
{
    int data;
    struct treenode *lchild;
    struct treenode *rchild;
}tnode;

void insert(tnode **node,int value)
{
    if(!*node)
    {
        (*node) =(tnode *)malloc(sizeof(tnode));
        (*node)->data=value;
        (*node)->lchild=NULL;
        (*node)->rchild=NULL;
    }
    else
        value>((*node)->data)?insert(&(*node)->rchild,value):insert(&(*node)->lchild,value);
}

tnode * createTree(int arr[],int n)
{
    tnode * root=NULL;
    int temp;
    int i=0;
    while(i<n)
    {
        temp=arr[i++];
        insert(&root,temp);
    }
    return root;
}

void preprintTree(tnode * root)
{
    if(root)
    {
     preprintTree(root->lchild);
     printf("%5d",root->data);
     preprintTree(root->rchild);
     }
}

int main()
{
    tnode *root;
    int arr[6]={7,1,23,55,9,2};
    root=createTree(arr,6);
    preprintTree(root);
    getch();
    return 0;
}
0 0