数据结构---二叉排序树

//
//  排序二叉树
//



# include<iostream>
# include<stdio.h>
# include<math.h>
using namespace std;
# define numm 9
typedef int Elemtype;

typedef struct BSTNode{
    Elemtype data;
    BSTNode *lchild,*rchild;

}BSTNode,*BiTree;

int n = numm;
int num[25] = {8,3,6,7,2,1,4,9,5};

void printTree(BSTNode *p){
    printf("%d  ",p->data);
}

void inOrder(BiTree &T){
    if(T!=NULL){
        printTree(T);
        inOrder(T->lchild);
        inOrder(T->rchild);
    }
}


BSTNode *BST_Search(BiTree T,Elemtype key){     //查找操作
    while(T!=NULL && T->data!=key){
        if(T->data > key)
            T = T->lchild;
        else
            T = T->rchild;
    }

    return T;

}


int BST_Insert(BiTree &T,Elemtype k){    //插入操作
    if(T==NULL){
        T = (BSTNode*)malloc(sizeof(BSTNode));
        T->data = k;
        T->lchild = T->rchild = NULL;
        return 1;
    }
    else if(T->data == k)  //如果有相同的节点,则插入失败
        return 0;
    else if(k < T->data)
        return BST_Insert(T->lchild,k);
    else if(k > T->data)
        return BST_Insert(T->rchild,k);
}


void create_BST(BiTree &T)
{
    T = NULL;
    int i = 0;
    while(i<n){
        BST_Insert(T,num[i]);
        i++;
    }
}


int main(void){
    BiTree T;
    BSTNode *p,*q;
    create_BST(T);
    inOrder(T);
    p = BST_Search(T,6);
    q = p->rchild;
    p = p->lchild;

    printf("\n%d %d",p->data,q->data);


    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值