【问题标题】:Node is not being inserted after random number of times, Binary Search Tree (C++)随机次数后未插入节点,二叉搜索树(C++)
【发布时间】:2021-05-11 07:20:48
【问题描述】:

正如您将在下面的输出中看到的那样,没有添加值为 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 并未添加到树中。我已经尝试了每一种方法。 如果有人有解决方案,请提供它。谢谢

【问题讨论】:

    标签: insert binary-search-tree


    【解决方案1】:

    make temp=temp->left in first else if block and remove second else if block and do the same for 3rd and 4 th if block

    【讨论】:

    • 是的,我会试试这个。非常感谢,但我已修复错误。我只需要删除“休息”;来自第二个和第四个 else if 块的语句。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多