zoukankan      html  css  js  c++  java
  • 搜索——二叉搜索树

    //二t叉?搜?索??树???的??搜?索??
    #include <stdio.h>
    #include <stdlib.h>
    typedef int KeyType;
    typedef struct node{
                    KeyType key;
                     struct node *lchild,*rchild;
    }BSTNode;
    
    int Compare(int index1,int index2)
    {
                     if(index1>index2)
                                     return 1;
                     if(index1<index2)
                                     return -1;
                     return 0;
    }
    BSTNode * bst_search(BSTNode* t,KeyType k)
    {
                     if(t==NULL)
                                     return NULL;
                     else
                    {
                                     switch(Compare(k,t->key))
                                    {
                                     case 0:
                                                     return (t);
                                     case 1:
                                                     return (bst_search(t->rchild,k));
                                     case -1:
                                                     return (bst_search(t->lchild,k));
                                    }
                    }
    }
     
    void main()
    {
                    BSTNode * ptrtemp = NULL;
                    ptrtemp = (BSTNode*)malloc( sizeof(BSTNode));
                    ptrtemp->key = 30;
                    
                    BSTNode* l1,*r1,*l2;
                    l1=(BSTNode*)malloc( sizeof(BSTNode));
                    r1=(BSTNode*)malloc( sizeof(BSTNode));
                    l2=(BSTNode*)malloc( sizeof(BSTNode));
                    ptrtemp->lchild = l1;
                    ptrtemp->rchild = r1;
                    l1->lchild = l2;
                    l1->key = 5;
                    l1->rchild = NULL;
                    r1->key = 40;
                    r1->lchild = NULL;
                    r1->rchild = NULL;
                    l2->key = 2;
                    l2->lchild =NULL;
                    l2->rchild = NULL;
                    BSTNode* result;
                    KeyType searched =2;
                    result = bst_search(ptrtemp,searched);
                     if(result!=NULL)
                    printf( "%d",result->key);
    }
  • 相关阅读:
    everything is nothing
    基础算法
    OC 优化目录
    iOS 更改启动视图
    单例--iOS
    OC-Objection 学习笔记之一:简单的开始
    iOS 类库列表
    IOS 上线问题
    OC强弱引用的使用规则
    设置桌面图标
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3435848.html
Copyright © 2011-2022 走看看