首页 > 解决方案 > 删除节点案例

问题描述

如何在代码末尾添加第 5 个案例以删除节点?该程序接收数组并在我要求时对其进行排序。它还将打印所有节点忽略并搜索特定节点。如何在代码末尾删除特定节点作为第 5 种情况选项?下面的 C++ 代码:

#include<iostream>
#include<stdlib.h>
#include<conio.h>
#include <cstdlib>
#include <fstream>
#include <sstream>

using namespace std;

typedef struct BST{
    int data;
    struct BST *left, *right;
} node;

class Binary_Tree{

public:
    void insert(node *root, node *new_node){

        if (new_node->data < root->data){
            if (root->left == NULL)
                root->left = new_node;
            else
                insert(root->left, new_node);
        }
        if (new_node->data > root->data){
            if (root->right == NULL)
                root->right = new_node;
            else
                insert(root->right, new_node);
        }
    }

public:
    node *search(node *root, int key, node **parent){
        node *temp;
        temp = root;

        while (temp != NULL){
            if (temp->data == key){
                cout<<"\nThe Element "<<temp->data<<"is Present"<<endl;
                return temp;
            }

            *parent = temp;

            if (temp->data > key)
                temp = temp->left;

            else
                temp = temp->right;
        }
        return NULL;
    }

public:
    void inorder(node *temp){
        if (temp != NULL){
            inorder(temp->left);
            cout<<"\t"<<temp->data;
            inorder(temp->right);
        }
    }

public:
    node *get_node(){
        node *temp;
        temp = (node *) malloc(sizeof(node));
        temp->left = NULL;
        temp->right = NULL;
        return temp;
    }
};

int main(){
    int nums[160] = {7711, 6837, 2525, 5542, 230, 1674, 4456, 2742, 5492, 7456, 6626, 1998, 3139, 6167, 4371, 6540, 3420, 1068, 8863, 6438, 3429, 9465, 6147, 7448, 8781, 4959, 5797, 8730, 1883, 7676, 5751, 7481, 2979, 2759, 2278, 7200, 6876, 1916, 1701, 4467, 7730, 4154, 8826, 8495, 5765, 8701, 1894, 8450, 6157, 1419, 9909, 8512, 8848, 7141, 1197, 9604, 2512, 4328, 5373, 1150, 6500, 9624, 6202, 9642, 7172, 9625, 8344, 7655, 6199, 2946, 8144, 8227, 1573, 3627, 6875, 1275, 7355, 4870, 6713, 6684, 5696, 9814, 7867, 4839, 6296, 5122, 7378, 6176, 4146, 9877, 1565, 5054, 5605, 9464, 7271, 9756, 8268, 2947, 3044, 4106, 8089, 5876, 8077, 5616, 5397, 5811, 6688, 5097, 8402, 5457, 2583, 1789, 6357, 5271, 3411, 2536, 5244, 6853, 1326, 8597, 7529, 2714, 9728, 3717, 3509, 6593, 2293, 6366, 6960, 2886, 8608, 4274, 9268, 2497, 1631, 6638, 7557, 6517, 1257, 9924, 9365, 3030, 3760, 4841, 7669, 4646, 7367, 8757, 1108, 2884, 9486, 3926, 7775, 6860, 6996, 5330, 8655, 8036, 4176, 6221};
    Binary_Tree obj;
    int option;
    //char ans = 'N';
    int key;
    node *new_node, *root, *tmp, *parent;
    node *get_node();
    root = NULL;
    int count = 0;
    int i = 0;
    while (count < 160){
        new_node = obj.get_node();
        new_node->data = nums[count];

        if (root == NULL)
            root = new_node;
        else
            obj.insert(root, new_node);
        cout << "*****" << (long)nums[count] << "\n";
        count++;
        i++;
    }

    //input from number file
    ifstream inputFile;
    string line, n1;
    int num;
    inputFile.open("nums.txt");

    if (!inputFile.fail()){
        getline(inputFile, line);
        istringstream ss(line);

        while (getline(ss, n1, ',')){
            num = stoi(n1);
            new_node = obj.get_node();
            new_node->data = num;

            if (root == NULL)
                root = new_node;
            else
                obj.insert(root, new_node);
            cout << "num: " << num << endl;
        }
    }
    //Binary_Tree obj;
    //int option;
    char ans = 'N';
    //int key;
    //node *new_node, *root, *tmp, *parent;
    //node *get_node();
    //root = NULL;
    cout<<"-------------------------------------------\n"<<endl;
    cout<<"\n **** Program For Binary Search Tree ****\n"<<endl;

    while(1){
        cout<<"\n1.Insert The Element";
        cout<<"\n2.Search The Element";
        cout<<"\n3.Print Tree in inorder:";
        cout<<"\n4.Exit";
        cout<<"\nSelect your choice :";
        cin>>option;
        switch (option){
        case 1:
            do{
                new_node = obj.get_node();
                cout<<"\nEnter The Element";
                cin>>new_node->data;

                if (root == NULL)
                    root = new_node;
                else
                    obj.insert(root, new_node);
                cout<<"\nDo you Want To insert More Elements?(y/n)";
                ans = getch();
            } while (ans == 'y');
            break;

        case 2:
            cout<<"\nEnter Element to be searched :";
                cin>>key;
                tmp=obj.search(root, key, &parent);
                cout<<"\nParent of node "<<tmp->data<<" is"<<parent->data<<endl;
                break;

            case 3:
                if (root == NULL){
                    cout<<"\nTree Is Empty"<<endl;
                }
                else{
                    cout<<"\nThe Inorder display : ";
                    obj.inorder(root);
                }
                break;
            case 4:
                exit(0);
            }
        }
    }

标签: c++

解决方案


推荐阅读