二叉排序树的创建、插入、删除

二叉排序树的创建

首先定义树节点treeNode:包含节点的值value 以及其左右孩子指针left right 

1 class treeNode
2 {
3 public:
4     int value;
5     treeNode *right,*left;
6     treeNode():right(NULL),left(NULL){}
7 };

定义二叉排序树Tree:包含根节点Root,节点个数num,以及构造函数(创建树)、中序遍历(输出排序结果)、插入和删除函数 

#include<bits/stdc++.h>
using namespace std;
class Node{
    public:
        int value;
        Node *lchild,*rchild;
        Node():lchild(NULL),rchild(NULL){};
        ~Node(){};
};
class Tree{
    int num;
    Node *root;
    void Inorder(Node *t);
    void CreateTree(int a,Node *&t);
    void Search(int a,Node *&t);
    void Delete(int a,Node *&t);
    void setnum();
    public:
    Tree(){
        root=NULL;num=0;
    } 
    ~Tree(){};
    void Inorder();
    void Create(int a);
    void Search(int a);
    void Delete(int a);
};
void Tree::setnum(){
    num=0;
}
void Tree::Create(int a){
    CreateTree(a,root);
} 
void Tree::CreateTree(int a,Node *&t){
    if(t==NULL){
        t=new Node;
        t->value=a;
        t->lchild=NULL;t->rchild=NULL;
    }
    else{
        if(t->value>a){
            CreateTree(a,t->lchild);
        }
        else{
            CreateTree(a,t->rchild);
        }
    }
}
void Tree::Inorder(){
    Inorder(root);
    cout<<endl;
}
void Tree::Inorder(Node *t){
    if(t){
        Inorder(t->lchild);
        cout<<t->value<<" ";
        Inorder(t->rchild);
    }
}
void Tree::Search(int a){
    setnum();
    Search(a,root);
}
void Tree::Search(int a,Node *&t){
    if(t==NULL){
        cout<<"-1"<<endl;return;
    }
    else{
        if(a==t->value){
            num++;cout<<num<<endl;return;
        }
        else if(a<t->value){
            num++;Search(a,t->lchild);
        }
        else if(a>t->value){
            num++;Search(a,t->rchild);
        }
    } 
}
void Tree::Delete(int a){
    Delete(a,root);
}
void Tree::Delete(int a,Node *&t){
    if(t){
        if(a==t->value){
            if(t->lchild==NULL){
                Node *n=t;
                t=t->rchild;
                free(n);
            }
            else if(t->rchild==NULL){
                Node *n=t;
                t=t->lchild;
                free(n);
            }
            else{
                Node *m=t->lchild;
                Node *n=t; 
                while(m->rchild){
                    n=m;
                    m=m->rchild;
                }
                t->value=m->value;
                if(n!=t){
                    n->rchild=m->lchild;
                }
                else{
                    n->lchild=m->lchild;
                }
                free(m);
            }
        }
        else if(a<t->value){
            Delete(a,t->lchild);
        }
        else if(a>t->value){
            Delete(a,t->rchild);
        }
    } 
}
int main()
{
    int t;cin>>t;
    while(t--){
        int n;cin>>n;
        int *a=new int[n];
        Tree tree;
        for(int i=0;i<n;i++){
            cin>>a[i];
            tree.Create(a[i]);
        }
        tree.Inorder();
        int m;cin>>m;
        while(m--){
            int b;cin>>b;
            tree.Delete(b);
            tree.Inorder();
        }
    }
    return 0;
}

 

18.设二叉排序树中关键字由1至1000的整数构成,现要查找关键字为363的结点,下述关键字序列(  )不可能是在二叉排序树上查找到的序列?  

A)2,252,401,398,330, 344,397,363

B)924, 220, 911, 244, 898, 258, 362, 363

C)2, 399, 387, 219, 266, 382, 381, 278, 363

D)925, 202, 911, 240, 912, 245, 363

【答案】D

转载于:https://www.cnblogs.com/Liu269393/p/10212528.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
二叉排序树(Binary Search Tree,BST)是一种特殊的二叉树,它满足以下性质: 1. 左子树上所有节点的值均小于它的根节点的值; 2. 右子树上所有节点的值均大于它的根节点的值; 3. 左右子树也分别为二叉排序树。 基于这些性质,我们可以利用二叉排序树来快速地进行查找、插入删除等操作。 二叉排序树创建插入操作如下: 创建二叉排序树: ```c++ struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; // 创建二叉排序树 TreeNode* createBST(vector<int>& nums) { if (nums.empty()) return NULL; TreeNode* root = new TreeNode(nums[0]); for (int i = 1; i < nums.size(); i++) { insertBST(root, nums[i]); } return root; } ``` 插入节点到二叉排序树: ```c++ // 插入节点到二叉排序树 void insertBST(TreeNode* &root, int val) { if (!root) { root = new TreeNode(val); return; } if (val < root->val) { insertBST(root->left, val); } else { insertBST(root->right, val); } } ``` 其中,createBST 函数接收一个整数数组 nums,表示二叉排序树的节点值。我们先以 nums 的第一个元素作为根节点,然后依次将剩余的元素插入到树中。 insertBST 函数则是插入一个节点到二叉排序树中。如果根节点为空,直接创建一个新的节点作为根节点。否则,根据节点值的大小,递归地插入到左子树或右子树中。 需要注意的是,为了能够在 insertBST 函数中修改树结构,我们使用了指向指针的引用,即 TreeNode* &root。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值