二叉搜索树的构建、插入、删除、查找

#include <bits/stdc++.h>
using namespace std;
typedef int KeyType;
struct TreeNode
{
        KeyType val;
        TreeNode* left;
        TreeNode* right;
        TreeNode(int value):val(value),left(NULL),right(NULL){}
};
class BinarySortTree
{
        public:
                BinarySortTree(KeyType a[],int n);//建立集合a[n]的二叉排序树
                ~BinarySortTree();
                TreeNode* GetRoot();//获取指向根节点的指针
                TreeNode* InsertBST(TreeNode* root,TreeNode* node);//在二叉排序树中插入一个节点node
                void DeleteBST(TreeNode* parent,TreeNode* node);//删除节点node,其父节点为parent
                TreeNode* SearchBST(TreeNode* root,KeyType k);//查找值为k的节点
        private:
                TreeNode* root;
                void Release(TreeNode* root);//析构函数
};

BinarySortTree::BinarySortTree(KeyType a[],int n)
{
        root = NULL;
        for(int i = 0;i < n;i++)
        {
                TreeNode* tmp = new TreeNode(a[i]);
                root = InsertBST(root,tmp);
        }
}

TreeNode* BinarySortTree::InsertBST(TreeNode* root,TreeNode* node)
{
        //递归形式
        /*
        if(root == NULL) 
        {
                return node;
        }
        if(node->val < root->val)
        {
                root->left = InsertBST(root->left,node);
        }
        else
        {
                root->right = InsertBST(root->right,node);
        }
        return root;
        */
        //非递归形式
        if(root == NULL) return node;
        TreeNode* tmp = root;
        TreeNode* upnode = NULL;//要插入节点位置
        while(tmp != NULL)
        {
                upnode = tmp;
                if(node->val < tmp->val)
                {
                        tmp = tmp->left;
                }
                else
                {
                        tmp = tmp->right;
                }
        }
        if(node->val < upnode->val)
        {
                upnode->left = node;
        }
        else
        {
                upnode->right = node;
        }
        return root;
}

TreeNode* BinarySortTree::GetRoot()
{
        return root;
}
TreeNode* BinarySortTree::SearchBST(TreeNode* root,KeyType k)
{
        //递归形式
        /*
        if(root == NULL) return NULL;
        if(root->val == k) return root;
        if(root->val > k) return SearchBST(root->left,k);
        if(root->val <= k) return SearchBST(root->right,k);
        */
        //非递归形式
        if(root == NULL) return NULL;
        TreeNode* t = root;
        while(t != NULL)
        {
                if(t->val == k) return t;
                else if(t->val > k) t = t->left;
                else t = t->right;
        }
        return NULL;
}
void BinarySortTree::DeleteBST(TreeNode* p,TreeNode* node)
{
        if(p->left == node)
        {
                if(node->left == NULL && node->right == NULL)//node是叶子节点
                {
                        p->left = NULL;
                }
                else if(node->left != NULL && node->right == NULL)//node只有一边子树
                {
                        p->left = node->left;
                }
                else if(node->right != NULL && node->right == NULL)
                {
                        p->right = node->right;
                }
                else//左右子树都不是空
                {
                        TreeNode* t = node->left;
                        TreeNode* upnode = t;
                        while(t->right != NULL)
                        {
                                upnode = t;
                                t = t->right;
                        }
                        node->val = t->val;
                        if(t == node->left)
                        {
                                node->left = t->left;
                        }
                        else
                        {
                                upnode->right = t->left;
                        }
                }
        }
                          else if(p->right == node)
        {
                if(node->left == NULL && node->right == NULL)
                {
                        p->right = NULL;
                }
                else if(node->left != NULL && node->right == NULL)
                {
                        p->left = node->left;
                }
                else if(node->right != NULL && node->left == NULL)
                {
                        p->right = node->right;
                }
                else
                {
                        TreeNode* t = node->right;
                        TreeNode* upnode = t;
                        while(t->left != NULL)
                        {
                                upnode = t;
                                t = t->left;
                        }
                        node->val = t->val;
                        if(t != node->right)  upnode->left = t->right;
                        else node->right = t->right;
                }
        }

}
BinarySortTree::~BinarySortTree()
{
        Release(root);
}
void BinarySortTree::Release(TreeNode* root)
{
        if(root != NULL)
        {
                Release(root->left);
                Release(root->right);
                delete root;
        }
}

int main()
{
        int a[10] = {63,55,90,42,58,70,10,45,67,83};
        BinarySortTree bst = BinarySortTree(a,10);//初始化
        TreeNode* root = bst.GetRoot();
        cout<<root->left->val<<endl;
        TreeNode* searchnode1 = bst.SearchBST(root,63);
        TreeNode* searchnode2 = bst.SearchBST(root,55);
        bst.DeleteBST(searchnode1,searchnode2);
        cout<<searchnode1->left->val<<endl;
        return 0;
}

 

    原文作者:二叉查找树
    原文地址: https://blog.csdn.net/N1neDing/article/details/83903365
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞