节点未在随机次数后插入,二进制搜索树(C++)



正如您将在下面的输出中看到的,没有添加值为10和15的节点。整个代码如下。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。………….

#include<iostream>
using namespace std;
#define SPACE 10
class TreeNode{
public:
int value;
TreeNode *left;
TreeNode *right;
TreeNode(){
value = 0;
left = NULL;
right = NULL;
}
TreeNode(int v){
value = v;
left = NULL;
right = NULL;
}
};
class BST{
public:
TreeNode *root;
BST(){
root = NULL;
}
bool isEmpty(){
if(root == NULL)
return true;
else
return false;
};
void insertNode(TreeNode *new_node){
if(root == NULL){
root = new_node;
cout<<"Node entered at root"<<endl;
}
else{
TreeNode *temp = root;
while(temp!=NULL){

if(new_node->value == temp->value){
cout<<"Node already exist"<<endl;
break;
}
else if((new_node->value < temp->value) && (temp ->left == NULL)){
temp->left = new_node;
cout<<"Inserted at right"<<endl;
break;
}
else if (new_node->value < temp->value){
temp = temp->left;
break;
}
else if((new_node->value > temp->value) && (temp ->right == NULL)){
temp->right = new_node;
cout<<"Inserted at left"<<endl;
break;
}
else if (new_node->value > temp->value){
temp = temp->right;
break;
}
}
}
}

void print2D(TreeNode * r, int space) {
if (r == NULL) // Base case  1
return;
space += SPACE; // Increase distance between levels   2
print2D(r -> right, space); // Process right child first 3 
cout << endl;
for (int i = SPACE; i < space; i++) // 5 
cout << " "; // 5.1  
cout << r -> value << "n"; // 6
print2D(r -> left, space); // Process left child  7
}


};
int main(){
BST obj;
int option, val;
do {
cout << "What operation do you want to perform? " <<
" Select Option number. Enter 0 to exit." << endl;
cout << "1. Insert Node" << endl;
//cout << "2. Search Node" << endl;
//cout << "3. Delete Node" << endl;
cout << "2. Print/Traversal BST values" << endl;
//cout << "5. Height of Tree" << endl;
//cout << "6. Clear Screen" << endl;
cout << "0. Exit Program" << endl;
cin >> option;
//Node n1;
TreeNode *newNode = new TreeNode();
switch (option){
case 0:
break;
case 1:
cout<<"Insert Node"<<endl;
cout<<"Enter the value of the node"<<endl;
cin>>val;
newNode->value = val;
obj.insertNode(newNode);
break;
case 2:
cout<<"Print value"<<endl;
obj.print2D(obj.root, 5);
break;
default:
cout<<"Enter appropriate option"<<endl;
} //switch
}while(option!=0);
}

OUTPUT:
What operation do you want to perform?  Select Option number. Enter 0 to exit.
1. Insert Node
2. Print/Traversal BST values
0. Exit Program
1
Insert Node
Enter the value of the node 
30
Node entered at root
What operation do you want to perform?  Select Option number. Enter 0 to exit.
1. Insert Node
2. Print/Traversal BST values
0. Exit Program 
1
Insert Node
Enter the value of the node
18
Inserted at right
What operation do you want to perform?  Select Option number. Enter 0 to exit.
1. Insert Node
2. Print/Traversal BST values
0. Exit Program
1    
Insert Node
Enter the value of the node
45
Inserted at left
What operation do you want to perform?  Select Option number. Enter 0 to exit.
1. Insert Node
2. Print/Traversal BST values
0. Exit Program
1
Insert Node
Enter the value of the node
10
What operation do you want to perform?  Select Option number. Enter 0 to exit.
1. Insert Node
2. Print/Traversal BST values
0. Exit Program
1
Insert Node
Enter the value of the node
15
What operation do you want to perform?  Select Option number. Enter 0 to exit.
1. Insert Node
2. Print/Traversal BST values
0. Exit Program
2
Print value
45
30
18

正如您所看到的,节点10和15没有被添加到树中。我试过每一种方法。Plz如果有人有解决方案,请提供。谢谢

使temp=temp->留在第一个else if块中,删除第二个else if块,并对第三个和第四个else if块执行相同操作

最新更新