在C ++中将节点插入二进制搜索树

时间:2014-10-13 19:53:56

标签: c++ tree

我正在尝试构建一个二叉搜索树,然后以最左边的节点作为显示的第一个节点进行水平顺序打印。此外,在每个节点之前是它的深度(距离根的距离)以及波浪形以帮助可视化树本身。从概念上讲,我的代码似乎是正确的,但无论出于何种原因,我似乎无法正确地构建树。我认为错误最有可能出现在我的插入函数中,但我似乎无法找到它。

任何建议或想法都会非常有用!

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <algorithm>
using namespace std;

typedef struct treeNode {

    treeNode *leftChild;
    treeNode *rightChild;
    int data;
} treeNode;


void printTree(treeNode*);
int getNodeDepth(treeNode*);
treeNode* insert(treeNode*, int);
treeNode* createNewNode(int);

int main()
{


    //read in file here



    treeNode *root = NULL;

    root = insert(root, 8);
    root = insert(root, 1);
    root = insert(root, 90);
    root = insert(root, 3);
    root = insert(root, 80);
    root = insert(root, 6);
    root = insert(root, 83);

    printTree(root);

    return 0;
}


/*
 Purpose: Constructs a new node for the tree.
 Inputs:  The data for the node.
 Outputs: returns the new node
 */
treeNode* createNewNode(int data)
{
    treeNode *newNode = new treeNode;
    newNode->data = data;
    newNode->leftChild = NULL;
    newNode->rightChild = NULL;
    return newNode;
}

/*
 Purpose: Calculates the depth of a given node using recursion.
 Inputs:  The node to check the depth on.
 Outputs: returns the depth
 */
int getNodeDepth(treeNode *node)
{
    if (node == NULL)  // tree doesn't exist
        return(0);

    return(1 + max(getNodeDepth(node->leftChild), getNodeDepth(node->rightChild)));
}


/*
 Purpose: Inserts a node into the tree.
 Inputs:  The node to be inserted and the data for the node.
 Outputs: returns the inserted node
 */
treeNode* insert(treeNode *node, int data)
{
    if (node == NULL)
        return createNewNode(data);
    else
    {
        if (data <= node->data)
        {
            node->leftChild = insert(node->leftChild, data);
        }
        else
        {
            node->rightChild = insert(node->rightChild, data);
        }
        return node;
    }
}


/*
 Purpose: Prints the BST in a horizontal inorder format.
 Inputs:  The root node.
 Outputs: nothing
 */
void printTree(treeNode *node)
{
    if (node == NULL)
        return;
    printTree(node->leftChild);
    cout << "(" << (getNodeDepth(node)-1) << ") ";
    for (int i=0; i<(getNodeDepth(node)-1); i++)
        cout << "~";
    cout << node->data << endl;
    printTree(node->rightChild);
}

当前输出如下:

(2) ~~1
(1) ~3
(0) 6
(3) ~~~8
(1) ~80
(0) 83
(2) ~~90

显然它不能有两个根(即6和83)。谢谢!

2 个答案:

答案 0 :(得分:1)

对于那些希望正确实现我原来问题答案的人来说,这里是我提出的重构代码。我决定采用OOP方法并修改insert和getNodeDepth函数以使其正常工作。

//
//  Binary Search Tree
//

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <algorithm>
using namespace std;

// binary search tree
class BST {

private:
    typedef struct treeNode {
        treeNode *leftChild;
        treeNode *rightChild;
        int data;
    } treeNode;

    treeNode *root;

public:
    //Constructor
    BST() { root = NULL; }

    /*
     Purpose: Constructs a new node for the tree.
     Inputs:  The data for the node.
     Outputs: returns the new node
     */
    treeNode* createNewNode(int data)
    {
        treeNode *newNode = new treeNode;
        newNode->data = data;
        newNode->leftChild = NULL;
        newNode->rightChild = NULL;
        return newNode;
    }

    //Check if the tree is empty
    bool isEmpty() const { return root==NULL; }

    /*
     Purpose: Calculates the depth of a given node using recursion.
     Inputs:  The node to check the depth on and the node to check the depth from.
     Outputs: returns the depth
     */
    int getNodeDepth(treeNode *node, treeNode *from)
    {
        if (node == from)
            return 0;
        else if (node->data < from->data)
            return getNodeDepth(node, from->leftChild) + 1;
        else
            return getNodeDepth(node, from->rightChild) + 1;
    }


    /*
     Purpose: Inserts a node into the tree.
     Inputs:  The data for the node.
     Outputs: none
     */
    void insert(int newData)
    {
        treeNode* t = createNewNode(newData);
        treeNode* parent;
        parent = NULL;


        if(isEmpty())   //check if tree exists or not
            root = t;
        else {
            //Note: ALL insertions are as leaf nodes
            treeNode* curr;
            curr = root;
            // Find the Node's parent
            while(curr)
            {
                parent = curr;
                if (t->data > curr->data)
                    curr = curr->rightChild;
                else
                    curr = curr->leftChild;
            }

            if ((t->data) < (parent->data))
                parent->leftChild = t;
            else
                parent->rightChild = t;
        }

    }


    /*
     Purpose: Prints the BST in a horizontal inorder format.
     Inputs:  The root node.
     Outputs: nothing
     */
    void printTree(treeNode *node)
    {
        if (node == NULL)
            return;
        printTree(node->leftChild);
        cout << "(" << getNodeDepth(node, root) << ") ";
        for (int i=0; i<getNodeDepth(node, root); i++)
            cout << "~";
        cout << node->data << endl;
        printTree(node->rightChild);
    }

    //Getter for private member variable root
    void printInorder()
    {
        printTree(root);
    }

};

int main()
{
    // read file in here

    BST temp;

    temp.insert(8);
    temp.insert(1);
    temp.insert(90);
    temp.insert(3);
    temp.insert(80);
    temp.insert(6);
    temp.insert(83);

    temp.printInorder();

    return 0;
}

正确的输出如下所示,其中8为根:

(1) ~1
(2) ~~3
(3) ~~~6
(0) 8
(2) ~~80
(3) ~~~83
(1) ~90

希望这有帮助!

答案 1 :(得分:0)

首先,你不应该写两次treeNode

typedef struct {
    treeNode *leftChild;
    treeNode *rightChild;
    int data;
} treeNode;

在第二个中你创建了一个内存泄漏:

treeNode *root = new treeNode;
root = NULL;

你应该写:

treeNode *root = NULL;
  

显然它不能有两个根(即6和83)。谢谢!

6和83不是根。 8是根。所以你的程序给出了正确答案。