PTA二叉搜索树——二叉搜索树的基本操作


Code:

//二叉排序树的创建,插入,判断操作以及其简单应用
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#define OK 1
#define ERROR 0
using namespace std;

//二叉排序树的存储结构定义
typedef int ElementType;
typedef int Status;
typedef struct TNode *Position;
typedef Position BSTree;
struct TNode{
    ElementType Data;
    BSTree lchild;
    BSTree rchild;
};

Status InsBST(BSTree T, ElementType e, BSTree &f) //判断是不是已经存在
                                                  //并且将需要插入位置的父节点通过引用带回,后续操作可以直接插入
{
   TNode *p = T;
   f = NULL;
   while(p != NULL && e != p->Data)
   {
       if(e > p->Data){
            f=p;  p=p->rchild;
       }
       else{
            f=p;  p=p->lchild;
       }
   }
   if(!p)return OK;
   else return  ERROR;
}


Status CreateBST(BSTree &T)
{
    T=NULL;
    int N;
    scanf("%d", &N);                           //节点的个数
    if(N == 0)                                 //空树
        return OK;
    else
    {
        int e;
        scanf("%d", &e); N--;                  //根节点
        T = (TNode*) malloc(sizeof(TNode)), T->Data = e, T ->lchild = NULL, T->rchild = NULL;
    }
    while(N--)
    {
        int e;
        scanf("%d", &e);
        TNode *f;
        if(InsBST(T,e, f) != ERROR)               //没找到,可以插入
        {
            TNode *p;
            p = (TNode*) malloc (sizeof(TNode));
            p->Data = e, p->lchild = NULL, p->rchild = NULL;
            if(f->Data > e)
                f->lchild = p;
            else
                f->rchild = p;
        }
    }
}

bool IsRoot(BSTree T, int e)
{
    if(T->Data == e)
        return true;
    else
        return false;
}

int IsFather(BSTree T, int a, int b)
{
    while(T != NULL)
    {
        if(T->Data == a)
            break;
        if(T->Data > a)
            T = T->lchild;
        else
            T = T->rchild;
    }
    if(T == NULL)
        return 0;
    bool flag1 = false, flag2 = false;
    if(T->lchild != NULL)
    {
        if((T->lchild)->Data == b)
            return 1;
    }
    if(T->rchild != NULL)
    {
        if((T->rchild)->Data == b)
            return 2;
    }
    return 0;
}

void FindFather(BSTree T, int a, BSTree &f)
{
    TNode *p = T;
    f = NULL;
    while(p != NULL && a != p->Data)
    {
       if(a > p->Data){
            f=p;  p=p->rchild;
       }
       else{
            f=p;  p=p->lchild;
       }
   }
   if(p == NULL)
      f = NULL;
    return ;
}

int Rank(BSTree T, int a)
{
    int ans = 1;
    while(T != NULL && T->Data != a)
    {
        ans++;
        if(T->Data > a)
            T = T->lchild;
        else
            T = T->rchild;
    }
    if(T == NULL)
        return -1;
    return ans;
}

int main()
{
    BSTree T;
    CreateBST(T); //用引用实现树的创建
    int N;
    scanf("%d", &N);
    while(N--)
    {
        int a, b;
        string str, str1, str2;
        cin >> a >> str;
        if(str[0] == 'i')
        {
            cin >> str1 >> str2;
            if(str2 == "root") {
                if(IsRoot(T, a)) cout << "Yes" << endl;
                else cout << "No" << endl;
            }
            else if(str2 == "right"){
                cin >> str1 >> str2 >> b;
                if(IsFather(T, b, a) == 2) cout << "Yes" << endl;
                else cout << "No" << endl;
            }
            else if(str2 == "left"){
                cin >> str1 >> str2 >> b;
                if(IsFather(T, b, a) == 1) cout << "Yes" << endl;
                else cout << "No" << endl;
            }
            else if(str2 == "parent"){
                cin >> str2 >> b;
                if(IsFather(T, a, b) != 0) cout << "Yes" << endl;
                else cout << "No" << endl;
            }
        }
        else
        {
            cin >> b >> str1 >> str2;
            if(str2 == "siblings"){
                BSTree t1, t2;
                FindFather(T, a, t1); FindFather(T, b, t2);
                if(t1 == NULL || t2 == NULL) cout << "No" << endl;
                else if(t1->Data == t2->Data) cout << "Yes" << endl;
                else cout << "No" << endl;
            }
            else {
                cin >> str >> str1 >> str2;
                int r1 = Rank(T, a), r2 = Rank(T, b);
                if(r1 != -1 && r2 != -1 && r1 == r2) cout << "Yes" << endl;
                else cout << "No" << endl;
            }
        }

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉搜索树的删除操作涉及到以下几种情况: 1. 要删除的节点是叶子节点:直接删除即可。 2. 要删除的节点只有一颗子树:将子树接到要删除的节点的父节点上。 3. 要删除的节点有两颗子树:需要找到该节点的中序遍历的前驱或后继节点进行替换。 具体的删除操作如下: 1. 如果要删除的节点是根节点,直接将根节点置为空。 2. 如果要删除的节点是叶子节点,直接删除即可。 3. 如果要删除的节点只有一颗子树,将该子树接到要删除的节点的父节点上。 4. 如果要删除的节点有两颗子树,需要找到该节点的中序遍历的前驱或后继节点进行替换,这里以找后继节点为例: a. 在该节点的右子树中找到最小的节点,即后继节点。 b. 将该节点的值赋值给要删除的节点。 c. 将后继节点删除。 下面是Java代码实现: ```java public TreeNode deleteNode(TreeNode root, int key) { if (root == null) { return null; } if (key < root.val) { root.left = deleteNode(root.left, key); } else if (key > root.val) { root.right = deleteNode(root.right, key); } else { if (root.left == null) { return root.right; } else if (root.right == null) { return root.left; } else { TreeNode successor = findSuccessor(root.right); root.val = successor.val; root.right = deleteNode(root.right, successor.val); } } return root; } private TreeNode findSuccessor(TreeNode node) { while (node.left != null) { node = node.left; } return node; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值