提问人:VishIsHere26 提问时间:10/7/2023 更新时间:10/7/2023 访问量:13
与在二叉搜索树中插入元素相关
Related to insertion of an element in a binary search tree
问:
这是我用来插入 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;
}
}
};
有人可以告诉我我可以在二叉树类中实现哪些其他方法
我没有在这个类中创建显示方法,我该如何检查我的实现
答: 暂无答案
评论