二叉搜索树的一些操作的C++实现

头文件:

#include<iostream>
using namespace std;
typedef struct BiTNode
{
    int data;
    int key;
    struct BiTNode *parent, *lchild, *rchild;
}BiTNode, *BiTree;
BiTree Tree_Search(BiTree x, int k);
void Tree_Walk(BiTree x);
BiTree Tree_Minimun(BiTree x);
BiTree Tree_Maximun(BiTree x);
void Tree_Insert(BiTree &T, BiTree z);
void Tree_Delete(BiTree &T, BiTree z);
void Transplant(BiTree &T, BiTree u, BiTree v);

实现部分:

#include "searchTree.h"
BiTree Tree_Search(BiTree x, int k)  //查找二叉搜索树。Tree-Search返回一个指向关键字为key的节点的指针;否则返回NULL
{
    if ((x == NULL) || (k == x->key))
    {
        return x;
    }
    if (k < x->key)
    {
        return Tree_Search(x->lchild, k);
    }
    else
    {
        return Tree_Search(x->rchild, k);
    }
}
void Tree_Walk(BiTree x)  //遍历
{
    if (x != NULL)
    {
        Tree_Walk(x->lchild);
        cout << x->key << endl;
        Tree_Walk(x->rchild);
    }
}
BiTree Tree_Minimun(BiTree x)
{
    while (x->lchild != NULL)
    {
        x = x->lchild;
    }
    return x;
}
BiTree Tree_Maximun(BiTree x)
{
    while (x->rchild != NULL)
    {
        x = x->rchild;
    }
    return x;
}
void Tree_Insert(BiTree &T,BiTree z)
{
    BiTree y = NULL;
    BiTree x = T;    
    while (x != NULL)
    {
        y = x;
        if (z->key < x->key)
        {
            x = x->lchild;
        }
        else
        {
            x = x->rchild;
        }
    }
    z->parent = y;
    if (y == NULL)
    {
        T = z;  
    }
    else if (z->key < y->key)
    {
        y->lchild = z;
    }
    else
    {
        y->rchild = z;
    }
}
void Transplant(BiTree &T, BiTree u, BiTree v)
{
    if (u->parent == NULL)
    {
        T = v;
    }
    else if (u == u->parent->lchild)
    {
        u->parent->lchild = v;
    }
    else
    {
        u->parent->rchild = v;
    }
    if (v != NULL)
    {
        v->parent = u->parent;
    }
}
void Tree_Delete(BiTree &T, BiTree z)
{
    if (z->lchild == NULL)
    {
        Transplant(T, z, z->rchild);
    }
    else if (z->rchild == NULL)
    {
        Transplant(T, z, z->lchild);
    }
    else
    {
        BiTree y = Tree_Minimun(z->rchild);
        if (y->parent != z)
        {
            Transplant(T, y, y->rchild);
            y->rchild = z->rchild;
            y->rchild->parent = y;
        }
        Transplant(T, z, y);
        y->lchild = z->lchild;
        y->lchild->parent = y;
    }
}
int main()
{
    BiTree oldTree = NULL;
    for (int i = 0; i < 10; ++i)
    {
        BiTree testNode = new BiTNode();
        testNode->key = rand()%100;
        Tree_Insert(oldTree, testNode);
    }
    Tree_Walk(oldTree);
    cout << "The Max is   " << Tree_Maximun(oldTree)->key << endl;
    cout << "The Max is   " << Tree_Minimun(oldTree)->key << endl;
    Tree_Delete(oldTree, Tree_Maximun(oldTree));
    Tree_Delete(oldTree, Tree_Minimun(oldTree));
    Tree_Walk(oldTree);
    return 0;
}

 

转载于:https://www.cnblogs.com/pentium-neverstop/p/5349824.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉搜索树的删除操作可以分为以下几步: 1. 首先找到要删除的节点,可以使用二叉搜索树的查找方法,从根节点开始递归查找。 2. 如果要删除的节点是叶子节点,直接删除即可。 3. 如果要删除的节点只有一个子节点,将该子节点接替要删除的节点即可。 4. 如果要删除的节点有两个子节点,需要找到要删除节点的中序遍历后继节点(也就是比要删除的节点大的最小节点),将其值复制到要删除的节点中,然后再将中序遍历后继节点删除。 以下是一个示例代码实现: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right class Solution: def deleteNode(self, root: TreeNode, key: int) -> TreeNode: if not root: return None # 找到要删除的节点 if key < root.val: root.left = self.deleteNode(root.left, key) elif key > root.val: root.right = self.deleteNode(root.right, key) else: # 叶子节点或只有一个子节点 if not root.left: return root.right elif not root.right: return root.left # 有两个子节点 else: successor = root.right while successor.left: successor = successor.left root.val = successor.val root.right = self.deleteNode(root.right, successor.val) return root ``` 在这个实现中,我们首先使用递归找到要删除的节点,然后根据节点的情况进行删除操作。如果节点是叶子节点或只有一个子节点,直接将其删除或将其子节点接到父节点上。如果节点有两个子节点,找到中序遍历后继节点,将其值复制到要删除的节点上,然后再将中序遍历后继节点删除。最后返回删除操作后的根节点即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值