C ++二进制树,具有3个Struct值

时间:2018-05-02 03:41:54

标签: c++ struct binary-tree binary-search-tree nodes

所以我正在尝试创建一个二叉搜索树,该树存储位于树中的每个“气泡”的ID (T value)年龄(int age)和名称(string),并按以下顺序排序: ID。

出于某种原因,我无法让我的代码正确地将所有3个值存储为每个节点1个结构。有什么我做错了吗?

这是我的代码。

#ifndef _TREE_GUARD 
#define _TREE_GUARD 1

#include <iostream>
#include <math.h>
using namespace std;

template <class T>
struct Node {
    T value;
    int age;
    string name;
    Node *left;
    Node *right;

    Node(T val) {
        this->value = val;
    }

    Node(T val, int age, string name, Node<T> left, Node<T> right) {
        this->value = val;
        this->age = age;
        this->name = name;

        this->left = left;
        this->right = right;
    }
};

template <class T>
class BST {

private:
    Node<T> *root;

    void addHelper(Node<T> *root, T val) {
        if (root->value > val) {
            if (!root->left) {
                root->left = new Node<T>(val);
            }
            else {
                addHelper(root->left, val);
            }
        }
        else {
            if (!root->right) {
                root->right = new Node<T>(val);
            }
            else {
                addHelper(root->right, val);
            }
        }
    }

    void printHelper(Node<T> *root) {
        if (!root) return;
        printHelper(root->left);
        cout << root->value << ' ';
        cout << root->age << ' '; // ADDED
        cout << root->name << ' '; //ADDED
        printHelper(root->right);
    }

    int nodesCountHelper(Node<T> *root) {
        if (!root) return 0;
        else return 1 + nodesCountHelper(root->left) + nodesCountHelper(root->right);
    }

    int heightHelper(Node<T> *root) {
        if (!root) return 0;
        else return 1 + max(heightHelper(root->left), heightHelper(root->right));
    }

    void printMaxPathHelper(Node<T> *root) {
        if (!root) return;
        cout << root->value << ' ';
        if (heightHelper(root->left) > heightHelper(root->right)) {
            printMaxPathHelper(root->left);
        }
        else {
            printMaxPathHelper(root->right);
        }
    }

    bool deleteValueHelper(Node<T>* parent, Node<T>* current, T value) {
        if (!current) return false;
        if (current->value == value) {
            if (current->left == NULL || current->right == NULL) {
                Node<T>* temp = current->left;
                if (current->right) temp = current->right;
                if (parent) {
                    if (parent->left == current) {
                        parent->left = temp;
                    }
                    else {
                        parent->right = temp;
                    }
                }
                else {
                    this->root = temp;
                }
            }
            else {
                Node<T>* validSubs = current->right;
                while (validSubs->left) {
                    validSubs = validSubs->left;
                }
                T temp = current->value;
                current->value = validSubs->value;
                validSubs->value = temp;
                return deleteValueHelper(current, current->right, temp);
            }
            delete current;
            return true;
        }
        return deleteValueHelper(current, current->left, value) ||
            deleteValueHelper(current, current->right, value);
    }

public:

    void insert(T val) {
        if (root) {
            this->addHelper(root, val);
        }
        else {
            root = new Node<T>(val);
        }
    }

    void print() {
        printHelper(this->root);
    }

    int nodesCount() {
        return nodesCountHelper(root);
    }

    int height() {
        return heightHelper(this->root);
    }

    void printMaxPath() {
        printMaxPathHelper(this->root);
    }

    bool Delete(T value) {
        return this->deleteValueHelper(NULL, this->root, value);
    }
};
#endif

我使用这3个值创建了结构,并且指向了一个左右节点,但是,我的所有其他函数似乎都不适用于年龄和名称值,只有ID。这是addprint函数中最具问题的。

我对C ++比较陌生,所以任何帮助都会受到赞赏。

编辑:我假设我的问题出在某处。代码运行得很好,但它没有将年龄和名称添加到字符串中,我无法正确打印这些值,只是ID

 Node<T> *root;

    void addHelper(Node<T> *root, T val) {
        if (root->value > val) {
            if (!root->left) {
                root->left = new Node<T>(val);
            }
            else {
                addHelper(root->left, val);**
            }
        }
        else {
            if (!root->right) {
                root->right = new Node<T>(val);
            }
            else {
                addHelper(root->right, val);
            }
        }
    }

在这里

void printHelper(Node<T> *root) {
        if (!root) return;
        printHelper(root->left);
        cout << root->value << ' ';
        cout << root->age << ' '; // ADDED
        cout << root->name << ' '; //ADDED
        printHelper(root->right);
    }

提前致谢!

1 个答案:

答案 0 :(得分:1)

  

root-&gt; left = new Node(val);

     

root-&gt; right = new Node(val);

您似乎创建了一个新节点,只给它一个“val”,没有年龄,没有名称等,所以它只有值。

编辑:我之前从未使用过“模板”,所以我花了一段时间来弄清楚它。反正...

解决方案: 插入节点时,需要复制所有信息,而不仅仅是“值”,否则只能获得“值”。

void addHelper(Node<T> *root, Node<T>* n) {
    if (root->value > n->value) {
        if (!root->left) {
            root->left = new Node<T>(n->value,n->age,n->name,NULL,NULL);
        }
        else {
            addHelper(root->left, n);
        }
    }
    else {
        if (!root->right) {
            root->right = new Node<T>(n->value,n->age,n->name,NULL,NULL);
        }
        else {
            addHelper(root->right, n);
        }
    }
}

同样在“插入”中:

void insert(Node<T>* n) {
    if (root) {
        this->addHelper(root, n);
    }
    else {
        root = new Node<T>(n->value,n->age,n->name,NULL,NULL);
    }
}

您可以尝试其他几个方面来看看差异:

我会将* root初始化为NULL,否则它可能是一些随机的东西,有时我因此而遇到了seg错误。

class BST {

private:
    Node<T> *root=NULL;

如果您要插入的节点与现有节点具有相同的“值”,您会怎么做?您的程序只会将其添加到右侧。我不知道这是不是你想要的,但这就是它的作用。

我测试了这个并且它有效。希望它也适合你。

int main(){
    BST<int> tree;
    Node<int> node1(1,10,"test1",NULL,NULL);
    Node<int> node2(5,50,"test2",NULL,NULL);
    Node<int> node3(3,30,"test3",NULL,NULL);
    Node<int> node4(3,30,"test4",NULL,NULL);
    Node<int> node5(3,30,"test5",NULL,NULL);
    Node<int> node6(8,80,"test5",NULL,NULL);
    tree.insert(&node1);
    tree.insert(&node2);
    tree.insert(&node3);
    tree.insert(&node4);
    tree.insert(&node5);
    tree.insert(&node6);
    tree.print();
    cout<<endl;
    return 0;
}

输出:

1 10 test1 3 30 test3 3 30 test4 3 30 test5 5 50 test2 8 80 test5