简体   繁体   中英

Troubles with printing functions in Binary Search Tree

My exercise is to Insert Elements in BSTree and Find the Max element of it. After I did that, It is not printing anything and always return 0. I tried with Print Function but it was same i don't know the reason. Please help. Thanks in advance.............................................................................................................................................................

#include <iostream>

using namespace std;

struct BstNode {
    int data;
    BstNode *left;
    BstNode *right;

    BstNode(int data1, BstNode *left1 = nullptr, BstNode *right1 = nullptr) : data(data1), left(left1), right(right1) {}

    ~BstNode() {}

};

class BinTree {
private:
    BstNode *root;
public:
    BinTree() : root(NULL) {}

    ~BinTree() {}

    BstNode *Insert(int data, BstNode *root) {
        if (root == NULL) {
            root = new BstNode(data);
            return root;
        }
        while (data <= root->data) {
            if (root->left == NULL) {
                root->left = new BstNode(data);
            }
            Insert(data, root->left);
        }
        while (data <= root->data) {
            if (root->right == NULL) {
                root->right = new BstNode(data);
            }
            Insert(data, root->right);
        }
    }

    int FindMax() {
        MaxHelper(root);
    }

    int MaxHelper(BstNode *root) {
        if (root == NULL) {
            cout << "List is empty  ";
            return -1;
        } else if (root->right != NULL) {
            root = root->right;
        }
        return root->data;
    }

    void Preorder(BstNode *root) { //DLR
        if (root == NULL) {
            return;
        }
        cout << root->data;
        Preorder(root->left);
        Preorder(root->right);
    }


};

BstNode *root = new BstNode(10);

int main() {

    BinTree b;
    cout << b.Insert(5, root);
    cout<<b.Insert(6, root);
    cout << b.Insert(7, root);
    cout << b.FindMax();

    return 0;
}

First, you have to include the comments in your code then others can understand. And I found a lot of bugs in your code.

The first thing to note is if you have a non-void function it should return something.

And using namespace std; pollute the namespace. Hence, use

using std::cout;
using std::endl;

And it is no need to have a separate structure if you are using classes in your file. Try to do implement it inside the class.

And root == NULL can be changed as !root .

And your Insert function has logical errors.

And if you return something in the if statement no need to use else if, just use if.

And better to use two separate files for the main and the implementation and use it as a header file.

And protect your header files from multiple inclusion.

And bla bla bla...;)

Here I have corrected some of your mistakes. but not all, try to correct them all yourself.

#include <iostream>

using std::cout;
using std::endl;

struct BstNode
{
    int data;
    BstNode *left;
    BstNode *right;

    BstNode(int data1) : data(data1), left(NULL), right(NULL){};

    ~BstNode(){};
};

class BinTree
{
private:
    BstNode *root;

public:
    BinTree() : root(NULL) {}

    ~BinTree() {}

    BstNode *Insert(int data, BstNode *root)
    {
        if (!root)
        {
            return new BstNode(data);
        }
        if (data > root->data)
        {
            root->right = Insert(data, root->right);
        }
        else
        {
            root->left = Insert(data, root->left);
        }

        return root;
    }

    int FindMax(BstNode *root)
    {
        return MaxHelper(root);
    }

    int MaxHelper(BstNode *root)
    {
        if (!root)
        {
            return -1;
        }
        if (root->right)
        {
            root = root->right;
        }
        return root->data;
    }

    void Preorder(BstNode *root)
    {
        if (!root)
            return;
        Preorder(root->left);
        cout << root->data << " ";
        Preorder(root->right);
    }
};

int main()
{

    BstNode *root = new BstNode(10);
    BinTree b;
    b.Insert(5, root);
    b.Insert(6, root);
    b.Insert(7, root);
    b.Insert(11, root);
    cout << b.FindMax(root) << endl;
    b.Preorder(root);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM