算法导论 二叉搜索树

二叉搜索树的结构不算复杂,就是删除结点有点复杂,需要注意

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
class Node
{
public:
    int key;
    Node* left;
    Node* right;
    Node* parent;
};
class BST
{
public:
    Node* root;

    BST()
    {
        root = NULL;
    }

private:
    //中序遍历
    void InorderTree(Node* x)
    {
        if(x != NULL)
        {
            InorderTree(x->left);
            printf("%d ",x->key);
            InorderTree(x->right);
        }
    }

    //查找
    bool TreeSearch(Node* x,int key)
    {
        if(x==NULL)
            return false;
        if(x->key == key)
        {
            cout<<"key is found"<<endl;
            return true;
        }
        if(x->key>key)
            TreeSearch(x->left,key);
        else
            TreeSearch(x->right,key);
    }


    //返回以x为根节点树的最小值
    Node* minimumTree(Node* x)
    {
        while(x->left!=NULL)
            x=x->left;
        return x;
    }

    //返回以x为根节点树的最大值
    Node* maximumTree(Node* x)
    {
        while(x->right!=NULL)
            x=x->right;
        return x;
    }

    //按中序遍历查找x节点的后继
    Node* successorTree(Node* x)
    {
        if(NULL == x)
            return NULL;
        if(x->right!=NULL)
        {
            return minimumTree(x->right);
        }
        Node* y = x->parent;
        while(y!=NULL&&x==y->right)
        {
            x=y;
            y=y->parent;
        }
        return y;
    }

    //按中序遍历查找x节点的前驱
    Node* processorTree(Node* x)
    {
        if(NULL == x)
            return NULL;
        if(x->left!=NULL)
        {
            return maximumTree(x->left);
        }
        Node* y = x->parent;
        while(y!=NULL&&x==y->left)
        {
            x=y;
            y=y->parent;
        }
        return y;
    }

    //基于迭代查找
    Node* IterativeTreeSearch(Node* x,int key)
    {
        while(x!=NULL&&x->key!=key)
        {
            if(key > x->key)
                x = x->right;
            else
                x = x->left;                
        }
        return x;
    }

    //用另一颗子树替换一颗子树并成为其双亲的孩子结点
    void transplantTree(Node* u,Node* v)
    {
        if(u->parent == NULL)
            root = v;
        else if(u==u->parent->left)
            u->parent->left = v;
        else
            u->parent->right = v;
        if(v!=NULL)
            v->parent = u->parent;
    }

    //删除某个结点
    void deleteTree(Node* z)
    {
        Node* y;
        if(z->left == NULL)
            transplantTree(z,z->right);
        else if(z->right == NULL)
            transplantTree(z,z->left);
        else
        {
            y = minimumTree(z->right);
            if(y->parent != z)
            {
                transplantTree(y,y->right);
                y->right = z->right;
                y->right->parent = y;
            }
            transplantTree(z,y);
            y->left = z->left;
            y->left->parent = y;
        }
    }

public:
    //中序遍历二叉搜索树
    void InorderTree()
    {
        InorderTree(root);
    }

    //基于键值插入
    void insertTree(int key)
    {
        Node* z = new Node();
        z->key = key;
        Node* y = NULL;
        Node* x = root;
        while(x!=NULL)
        {
            y=x;
            if(z->key < x->key)
                x = x->left;
            else
                x = x->right;
        }
        z->parent = y;
        if(y==NULL)
            root = z;
        else if (z->key < y->key)
        {
            y->left = z;
        }
        else
        {
            y->right = z;
        }
    }

    //基于键值删除
    void deleteTree(int key)
    {
        Node* z=IterativeTreeSearch(root,key);
        deleteTree(z);
    }

    //基于键值进行迭代查找
    Node* IterativeTreeSearch(int key)
    {
        Node* x = root;
        IterativeTreeSearch(x,key);
    }

    //基于键值按中序遍历查找x节点的后继
    Node* successorTree(int key)
    {
        Node* z=IterativeTreeSearch(root,key);
        return successorTree(z);
    }

    //基于键值按中序遍历查找x节点的前驱
    Node* processorTree(int key)
    {
        Node* z=IterativeTreeSearch(root,key);
        return processorTree(z);
    }

    //返回树的最小值
    int minimumTree()
    {
        Node* x = root;
        while(x->left!=NULL)
            x=x->left;
        return x->key;
    }

    //返回以树的最大值
    int maximumTree()
    {
        Node* x = root;
        while(x->right!=NULL)
            x=x->right;
        return x->key;
    }
};

int main(array<System::String ^> ^args)
{
    BST temp;
    temp.insertTree(4);
    temp.insertTree(1);
    temp.insertTree(7);
    temp.insertTree(3);
    temp.insertTree(12);
    printf("%d %d",temp.maximumTree(),temp.minimumTree());
    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值