查找二叉树节点

#include<stdio.h>
#include<stdlib.h>
#define NULL 0

typedef struct BTNode
{
    struct BTNode *lchild,*rchild;
    int data;
}BTNode,*btnode;

void createBtree(btnode &T)
{
    int t;
    scanf("%d",&t);
    if(t==0)
        T=NULL;
    else
    {
        T=(btnode)malloc(sizeof(BTNode));
        T->data=t;
        createBtree(T->lchild);
        createBtree(T->rchild);
    }
}

void search(btnode p,btnode &q,int key)
{
    if(p!=NULL)
    {
        if(p->data==key)
            q=p;
        else
        {
            search(p->lchild,q,key);
            search(p->rchild,q,key);
        }
    }
}

void main()
{
    int key;
    btnode T,q=NULL;
    createBtree(T);
    printf("input key:");
    scanf("%d",&key);
    search(T,q,key);
    if(q==NULL)
        printf("can not find!\n");
    else
        printf("find q = %d\n",q->data);

}






    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/zxiang248/article/details/50927082
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞