二叉搜索树实现与测试

/******************************************
 * 文件名:BSTree.cpp
 * 功能:1,定义二叉查找树结点
        2,定义二叉查找树
        3,测试二叉查找树
*****************************************
*/

#include 
< iostream >
using   namespace  std;

template 
< typename T >
class  BSTnode
{
public:
    BSTnode():pare(
0),left(0),right(0)
    
{ }
    BSTnode(
const T &v, BSTnode<T> *= 0, BSTnode<T> *lef = 0, BSTnode<T> *rig = 0):value(v),pare(p),left(lef),right(rig)
    
{ }

public:
    T value;
    BSTnode
<T> *pare, *left, *right;
}
;

//  以上是二叉查找树结点的定义
//  以下是二叉查找树的定义

template 
< typename T >
class  BSTree
{
public:
    BSTnode
<T> *root;

public:
    BSTree():root(
0)
    
{}
    BSTree(
const BSTnode<T> &r):root(&r)
    
{}
    BSTree(
const T &v)
    
{
        root 
= new BSTnode<T>(v);
    }


    
void Insert_BST(const T &v)
    
{
        
if( root == 0 )
            root 
= new BSTnode<T>(v);
        
else
        
{
            BSTnode
<T> *curr = root;
            BSTnode
<T> *= 0;

            
while(curr != 0)
            
{
                p 
= curr;
                curr 
= (v > curr->value) ? curr->right : curr->left;
            }


            curr 
= new BSTnode<T>(v);
            curr
->pare = p;
            
if( curr->value > p->value )
                p
->right = curr;
            
else
                p
->left = curr;
        }
 // else
    }
 // Insert_BST

    BSTnode
<T> *search_BST(const T &v)
    
{
        BSTnode
<T> *curr = root;
        
while( (curr != 0&& (v != curr->value) )
        
{
            curr 
= (v > curr->value) ? curr->right : curr->left;
        }


        
return curr;
    }
 // search_BST

    T mini_BST(BSTnode
<T> *r) const
    
{
        BSTnode
<T> *curr = r;
        
while( curr->left != 0 )
            curr 
= curr->left;

        
return curr->value;
    }


    T max_BST(BSTnode
<T> *r) const
    
{
        BSTnode
<T> *curr = r;
        
while(curr->right != 0)
            curr 
= curr->right;

        
return curr->value;
    }


    T next_BST(
const T &v)
    
{
        BSTnode
<T> *curr;
        curr 
= search_BST(v);
        
if(curr->right != 0)
            
return mini_BST(curr->right);

        BSTnode
<T> *= curr->pare;
        
while((p != 0&& (curr == p->right))
        
{
            curr 
= p;
            p 
= p->pare;
        }


        
return p->value;
    }


    T prev_BST(
const T &v)
    
{
        BSTnode
<T> *curr;
        curr 
= search_BST(v);
        
if(curr->left != 0)
            
return max_BST(curr->left);

        BSTnode
<T> *= curr->pare;
        
while((p != 0&& (curr == p->left))
        
{
            curr 
= p;
            p 
= curr->pare;
        }


        
return p->value;
    }


    BSTnode
<T> *del_BST(BSTnode<T> *r, const T &v)
    
{
        BSTnode
<T> *curr, *y, *x;
        curr 
= 0;
        y 
= 0;
        x 
= 0;

        curr 
= search_BST(v);
        
if( curr == 0 )
            
return 0;

        
if((curr->left == 0|| (curr->right == 0))
            y 
= curr;
        
else
            y 
= search_BST(next_BST(v));

        
if( y->left != 0 )
            x 
= y->left;
        
else
            x 
= y->right;

        
if(x != 0)
            x
->pare = y->pare;
        
        
if(y->pare == 0)
            r 
= x;
        
else if( y == y->pare->left )
            y
->pare->left = x;
        
else
            y
->pare->right = x;

        
if( y != curr )
            curr
->value = y->value;
    
        
return y;
    }


    
// 中序遍历BSTree 输出每个结点值
    void InOrder_BST(BSTnode<T> *r)
    
{
        
if(r != 0)
        
{
            InOrder_BST(r
->left);
            cout 
<< r->value << ' ';
            InOrder_BST(r
->right);
        }

    }

}
;

/***************************************************
                        14
                    /        
                   3        17
                  /           /  
                 2     5      15   18
                    /                 
                   4   7

**************************************************
*/

int  main()
{
    BSTree
<int> a(14);
    
    a.Insert_BST(
3);
    a.Insert_BST(
2);
    a.Insert_BST(
5);
    a.Insert_BST(
4);
    a.Insert_BST(
7);
    a.Insert_BST(
17);
    a.Insert_BST(
15);
    a.Insert_BST(
18);

    a.InOrder_BST(a.root);
    cout 
<< endl;
    
// 2 3 4 5 7 14 15 17 18

    cout 
<< a.prev_BST(5<< " <- 5 -> " << a.next_BST(5<< endl;
    
// 4 <- 5 -> 7

    cout 
<< a.search_BST(5)->value << endl;
    
// 5

    cout 
<< a.del_BST(a.root, 3)->value << endl;
    
// 7

    a.InOrder_BST(a.root);
    
// 2 3 4 7 14 15 17 18

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值