zoukankan      html  css  js  c++  java
  • 二叉排序树

    searchBST.h

      1 #ifndef SEARCHBST_H
      2 #define SERACHBST_H
      3 
      4 #include <stdio.h>
      5 #include <stdlib.h>
      6 #include <malloc.h>
      7 #include <queue>
      8 
      9 typedef int ElemType;
     10 typedef enum{ERROR,OK}Status;
     11 typedef int KeyType;
     12 
     13 typedef struct BiNode
     14 {
     15     ElemType data;
     16     struct BiNode *left,*right;
     17 }BiNode, *BiTree;
     18 
     19 Status LevelTraverseBiTree(BiTree T)//二叉排序树层序遍历
     20 {
     21     std::queue<BiTree> qbi;
     22     BiTree p;
     23     if(T)//逻辑要判断准确
     24     {
     25         qbi.push(T);
     26         while(!qbi.empty())
     27         {
     28             p = qbi.front();
     29             printf("%d ",p->data);
     30             qbi.pop();
     31             if(p->left)
     32             {
     33                 qbi.push(p->left);
     34             }
     35             if(p->right)
     36             {
     37                 qbi.push(p->right);
     38             }
     39         }
     40         printf("\n");
     41     }
     42     return OK;
     43 }
     44 
     45 bool EQ(ElemType t1,ElemType t2)
     46 {
     47     return t1 == t2;
     48 }
     49 
     50 bool LT(ElemType t1,ElemType t2)
     51 {
     52     return t1 < t2;
     53 }
     54 
     55 Status SearchBST(BiTree T,KeyType key,BiTree f,BiTree &p)//查找二叉排序树
     56 {
     57     if(!T)
     58     {
     59         p = f;
     60         return ERROR;//查找失败
     61     }
     62     else if(EQ(T->data,key))
     63     {
     64         p = T;
     65         return OK;
     66     }
     67     else if(LT(T->data,key))
     68     {
     69         return SearchBST(T->right,key,T,p);
     70     }
     71     else
     72     {
     73         return SearchBST(T->left,key,T,p);
     74     }
     75 }
     76 
     77 Status InsertBST(BiTree &T,KeyType key)//这里一定要加&号,在二叉排序树中插入节点
     78 {
     79     BiTree p;
     80     if(!SearchBST(T,key,NULL,p))
     81     {
     82         BiTree s = (BiNode *)malloc(sizeof(BiNode));
     83         s->data = key;
     84         s->left = NULL;
     85         s->right = NULL;
     86         if(!p)
     87         {
     88             T = s;
     89         }
     90         else if(LT(p->data,key))
     91         {
     92             p->right = s;
     93         }
     94         else
     95         {
     96             p->left = s;
     97         }
     98         return OK;
     99     }
    100     return OK;
    101 }
    102 
    103 Status Delete(BiTree p)//执行具体删除节点操作
    104 {
    105     BiTree q;
    106     if(!p->right)
    107     {
    108         q = p;
    109         p = p->left;
    110         free(q);
    111     }
    112     else if(!p->left)
    113     {
    114         q = p;
    115         p = p->right;
    116         free(q);
    117     }
    118     else
    119     {
    120         BiTree s = p->left;//首先s指向p的做孩子,寻找p左孩子中最右边的位置
    121         q = p;
    122         while(s->right)
    123         {
    124             q = s;
    125             s = s->right;
    126         }
    127         p->data = s->data;
    128         if(q != p)
    129         {
    130             q->right = s->left;
    131         }
    132         else
    133         {
    134             p->left = s->left;
    135         }
    136         free(s);
    137     }
    138     return OK;
    139 }
    140 
    141 Status DeletBST(BiTree &T,KeyType key)//删除二叉排序树中值为key的节点
    142 {
    143     if(!T)
    144     {
    145         return ERROR;
    146     }
    147     if(EQ(T->data,key))
    148     {
    149         return Delete(T);
    150     }
    151     else if(LT(T->data,key))
    152     {
    153         return DeletBST(T->right,key);
    154     }
    155     else
    156     {
    157         return DeletBST(T->left,key);
    158     }
    159 }
    160 #endif

    main.cpp

     1 #include "searchBST.h"
     2 
     3 int main()
     4 {
     5     BiTree T = NULL;
     6     BiTree p;
     7     int a[5] = {45,24,53,12,90};
     8     for(int i = 0;i < 5;i++)
     9     {
    10         InsertBST(T,a[i]);
    11     }
    12     LevelTraverseBiTree(T);
    13 
    14     InsertBST(T,37);
    15     LevelTraverseBiTree(T);
    16 
    17     printf("before delete 24:\n");
    18     if(SearchBST(T,24,NULL,p))
    19     {
    20         printf("24 has been found!\n");
    21     }
    22 
    23     printf("after delete 24:\n");
    24     DeletBST(T,24);
    25     if(!SearchBST(T,24,NULL,p))
    26     {
    27         printf("24 has not been found!\n");
    28     }
    29 
    30     LevelTraverseBiTree(T);
    31     
    32     
    33 
    34     system("pause");
    35     return 0;
    36 }
  • 相关阅读:
    PHP (20140519)
    PHP (20140516)
    js(20140517)在JS方法中返回多个值的三种方法
    PHP (20140515)
    PHP (20140514)
    Java内网发送邮件
    每日一“酷”之Cookie
    每日一“酷”之Queue
    每日一“酷”之pprint
    每日一“酷”之copy
  • 原文地址:https://www.cnblogs.com/maowang1991/p/2806304.html
Copyright © 2011-2022 走看看