与在二叉搜索树中插入元素相关

Related to insertion of an element in a binary search tree

提问人:VishIsHere26 提问时间:10/7/2023 更新时间:10/7/2023 访问量:13

问:

这是我用来插入 BST 的方法。

 class BinarySearchTree:Tree{
public:
void add(int data){
    if(root==nullptr){
        createRoot(data);
    }
    else{
        if(data>=root->data){
            insertRight(data);
            root=root->right;
        }
        if(data<root->data){
            insertLeft(data);
            root=root->left;
        }
    }
}
 };  

这些是树类的方法

   public:
    Node* root;
    Tree(){
        root=nullptr;
    }
    void createRoot(int val){
        Node* root=new Node(val);
        root->left=root->right=nullptr;
    }

内部树类:

 void insertLeft(int val){
        if(root==nullptr){
            createRoot(val);
        }else{
        
        Node* n=new Node(val);
        n->left=root->left;
        root->left=n;
       }}
       
    void insertRight(int val){
        if(root == nullptr){
            createRoot(val);
        }else{
        Node* n=new Node(val);
        n->right=root->right;
        root->right=n;
        }     
    }
};

有人可以告诉我我可以在二叉树类中实现哪些其他方法

我没有在这个类中创建显示方法,我该如何检查我的实现

数据结构 binary-tree binary-search-tree 节点

评论

0赞 trincot 10/8/2023
“我还能实现哪些其他方法...”:对于 Stack Overflow 来说,这不是一个合适的问题。在互联网上搜索一下,找到成千上万的实现,看看其他人已经实现了什么。“我怎样才能检查我的实现”:像所有编程一样,创建单元测试,涵盖边界情况,随机输入。

答: 暂无答案