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

    这只是作为我学习的笔记

      1 #ifndef BINARYTREE_H_
      2 #define BINARYTREE_H_
      3 #include<iostream>
      4 typedef int Item;
      5 struct Btreenode
      6     {
      7         Item data;
      8         Btreenode *left,*right;
      9     };
     10 class BinaryTree
     11 {
     12 public:
     13     Btreenode * BST;//树的根节点
     14     BinaryTree(){BST=NULL;}
     15     bool Insert(const Item & x);//合适的位置插入节点x
     16     bool Delete(const Item & x);//删除并调整树
     17     void create(const Item * x, int n);//创建n个树节点,调用insert
     18     bool find(Btreenode & p, const Item & x)const;//查找等于x的树节点并赋给p
     19 };
     20 #endif // !BinaryTree_H_
     21 
     22 
     23 #include"BinaryTree.h"
     24 
     25 void BinaryTree::create(const Item * x, int n)
     26 {
     27     for(int i=0;i<=n-1;i++)
     28     {
     29         Insert(x[i]);
     30     }
     31 }
     32 bool BinaryTree::Insert(const Item & x)//肯定是要放在合适的叶子节点位置
     33 {
     34     if(BST==NULL)//根指针
     35     {
     36         Btreenode * p=new Btreenode;
     37         p->data=x;
     38         p->left=p->right=0;
     39         BST=p;
     40         return true;
     41     }
     42     Btreenode *p=BST,*q=BST; 
     43     int f=0;
     44     while(p!=NULL)//非递归查找叶子节点位置
     45     {
     46         if(p->data>x)
     47             {q=p;f=1;p=p->left;}
     48         else if(p->data<x)
     49             {q=p;f=2;p=p->right;}
     50         else
     51             return false;   //如果插入相等的数返回false
     52     }
     53     if(p==NULL)
     54     {
     55         Btreenode * temp=new Btreenode;
     56         temp->data=x;
     57         temp->left=temp->right=NULL;//把叶子节点的后继孩子置为NULL指针
     58         if(f==1)q->left=temp;
     59         else if(f==2)q->right=temp;
     60         return true;
     61     }
     62     else return false;    
     63 }
     64 
     65 bool BinaryTree::find(Btreenode & p,const Item & x)const
     66 {
     67     if(BST==NULL)
     68         return false;
     69     else
     70     {
     71         Btreenode * temp=BST;
     72         while(temp!=NULL)//非递归实现,递归实现
     73         {
     74             if(temp->data==x)
     75             {
     76                 p=*temp;   //返回所指向的节点内容
     77                 return true;
     78             }
     79             else if(temp->data>x)
     80                 temp=temp->left;
     81             else 
     82                 temp=temp->right;
     83         }
     84         return false;    
     85     }
     86 }
     87 
     88 
     89 
     90 bool BinaryTree::Delete(const Item & x)
     91 {
     92     if(!BST)
     93         return false;
     94     Btreenode * s=NULL;
     95     Btreenode * t= BST;
     96     while(t!=NULL)   //定位 x的位置,其父节点
     97     {
     98         if(t->data==x)
     99             break;
    100         else if(t->data>x)
    101         {s=t;t=t->left;}//s指向t的父节点
    102         else 
    103         {s=t;t=t->right;}
    104     }
    105     if(t==NULL) return false;//不存在该节点,删不掉
    106     if(t->left==NULL && t->right==NULL)//t是叶节点,可以直接删掉置父节点孩子为NULL
    107     {
    108         if(t==BST)  //若t是根节点,单独考虑 s=NULL
    109             BST=NULL;
    110         else if(t==s->left)
    111             s->left=NULL;
    112         else
    113             s->right=NULL;
    114         delete t;
    115     }
    116     
    117     else if(t->left==NULL || t->right==NULL)//左孩子或右孩子为空,把非空的后继节点支脉给其父节点s
    118     {
    119         if(t==BST)
    120         {
    121             if(t->left==NULL)BST=BST->right;
    122             else BST=BST->left;
    123             delete t;
    124         }
    125         else
    126         {
    127             if(t==s->left && t->left!=NULL)
    128                 s->left=t->left;
    129             else if(t==s->left && t->right!=NULL)
    130                 s->left=t->right;
    131             else if(t==s->right && t->left!=NULL)
    132                 s->right=t->left;
    133             else if(t==s->right && t->right!=NULL)
    134                 s->right=t->right;
    135         }
    136     }
    137     else if(t->left!=NULL && t->right!=NULL)//如果都非空
    138     //法1:把左支脉给s节点相应缺失的孩子,右支脉放在t->left最大的位置(一直找右孩子,直至右孩子)
    139     //法2:用t->left后续最大节点代替t
    140     {
    141         if(s->left==t)//t指向的是s的左孩子
    142             s->left=t->left;
    143         else
    144             s->right=t->left;
    145         Btreenode * q=t->left;
    146         Btreenode * p=t;
    147         while(q!=NULL)
    148         {
    149             p=q;q=q->right; //q是t->left最大的位置.p没有右孩子
    150         }
    151         p->right=t->right;
    152         delete t;
    153     }
    154     return false;
    155 }
    156 
    157 
    158 #include<iostream>
    159 #include"BinaryTree.h"
    160 typedef BinaryTree T;
    161 
    162 void InOrder(Btreenode * r)//二叉查找树排序
    163 {
    164     if(r!=NULL)
    165     {
    166         InOrder(r->left);
    167         std::cout<<r->data<<" ";
    168         InOrder(r->right);
    169     }
    170     return;
    171 }
    172 
    173 int  main()
    174 {
    175     using namespace std;
    176     T mr;
    177     int n = 11;
    178     Item  a[]={66,80,3,10,88,98,15,77,25,65,35};
    179     mr.create(a, n);
    180     InOrder(mr.BST);
    181     Btreenode p; 
    182     mr.find(p, 10);
    183     cout<< p.data<<endl;
    184     mr.Delete(65);
    185     InOrder(mr.BST);
    186 
    187     system("pause");
    188     return 0;
    189 }
    View Code
  • 相关阅读:
    广域网(ppp协议、HDLC协议)
    0120. Triangle (M)
    0589. N-ary Tree Preorder Traversal (E)
    0377. Combination Sum IV (M)
    1074. Number of Submatrices That Sum to Target (H)
    1209. Remove All Adjacent Duplicates in String II (M)
    0509. Fibonacci Number (E)
    0086. Partition List (M)
    0667. Beautiful Arrangement II (M)
    1302. Deepest Leaves Sum (M)
  • 原文地址:https://www.cnblogs.com/fkissx/p/4555737.html
Copyright © 2011-2022 走看看