在树中打印某些元素

时间:2014-04-24 13:54:43

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

我需要制作一个程序,接受用户的号码,数字被解释为游戏的评级。这个计划的目的是接受数字并使质量参差不齐的团队不是数量。

例如: 用户输入数字:25,50,63,80。

糟糕的球队将是:25,50。

优秀的团队将是:63,90。

我使用的是二叉搜索树,所以时间是O(nlogn)

我的代码到目前为止接受数字,将它们放入树中,确定团队数量是否相等,但是不能正确打印。我怀疑当我调用打印功能或实际的打印功能时问题出现在我的int main()中。

#include <iostream>
using namespace std;

struct node
{
    int data;
    node *left;
    node *right;
};

node *createNode(int data) // creates nodes
{
    node *newNode = NULL;
    newNode = new node;
    newNode->data=data;
    newNode->left=NULL;
    newNode->right=NULL;
    return newNode;
}

node *insert(int data, node **tree) // inserts them into a tree
{
    node *newNode=NULL;
    if (*tree==NULL)
    {
        newNode = createNode(data);
        *tree = newNode;
    }
    else if (data<(*tree)->data)
    {
        if ((*tree)->left==NULL)
        {
            newNode = createNode(data);
            (*tree)->left=newNode;
        }
        else
        {
            newNode = insert(data, &((*tree)->left));
        }
    }
    else
    {
        if ((*tree)->right==NULL)
        {
            newNode = createNode(data);
            (*tree)->right = newNode;
        }
        else
            newNode = insert(data, &((*tree)->right));
    }
    return newNode;
}

void destroy(node *tree) // destroy tree at the end
{
    if (tree!=NULL)
    {
        if (tree->left!=NULL)
            destroy(tree->left);
        if (tree->right!=NULL)
            destroy(tree->right);
        delete tree;
    }
}

int treeHeight(node *tree, int counter) // finds the height of the tree
{
    if (tree==NULL) // if there is nothing in the tree, return 0
        return counter;

    int leftcount, rightcount; // leftcount counts the left side of the tree and right count counts the right side of the tree
    counter++; // counter should go up by 1 for every node
    rightcount = treeHeight(tree->right, counter); // searches the right side
    leftcount = treeHeight(tree->left, counter); // searches the left side

    if (rightcount > leftcount) // return the height
        return rightcount;
    else
        return leftcount;
}

void printNode(node *Node)
{
    if (Node!=NULL)
    {
        cout << Node->data << ", ";
    }
}

void printLeft(node *tree)
{
    if (tree!=NULL)
    {
        printLeft(tree->left);
        printNode(tree);
    }
}

void printRight(node *tree)
{
    if (tree!=NULL)
    {
        printRight(tree->right);
        printNode(tree);
    }
}


int main()
{
    node *root = NULL;
    node *current = NULL;
    int value;

    while (true)
    {
        cout << "Enter a rating where the bigger the number, the better (zero to quit): ";
        cin >> value;
        if (value==0)
            break;
        current = insert(value, &root);
    }

    int height = treeHeight(root, 0); // call the function and display the height of the tree

    if (height==0)
    {
        cout << "\nYou did not enter any players, the game cannot happen now. Thanks a lot." << endl;
        return 0;
    }

    else if (height%2!=0)
    {
        cout << "Uneven amount of player, add one more player to make the teams equal in quantity" << endl;
        cout << "Enter a rating: ";
        cin >> value;
        current = insert(value, &root);
    }

        cout << "\nThe bad team is: " ;
        printLeft(root); // ???
        cout << "\nThe good team is: ";
        printRight(root); // ???

    destroy(root);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

首先,你必须误解某些东西。带有此输入的二进制搜索树:25, 50, 63, 80不会以您编写的方式进行划分。看。它将被构造成:

25
 \
  50
   \
    63
     \
      80

那样:

 25
 / \
50 63
     \
      80

就像那样

 50
 / \
25 63
     \
      80

我建议你去模仿BST的插入:BST simulator online

现在,关于你的代码。函数printLeftprintRight仅打印树的最左侧和最右侧分支中的元素(printLeft执行顶部底部而printRight执行底部顶部)。这就是他们给出结果2580, 63, 50, 25的原因。我不确切地知道你的BST应该分为“坏”和“好”团队,但是我们假设“坏”团队由根节点和左子树组成,“好”团队只包含正确的子树(wihtour root)节点)。然后你可以使用这个功能:

void printSubTree(node *tree) {
    if (tree) {
        printSubTree(tree->left);
        printNode(tree);
        printSubTree(tree->right);
    }
}

并将您的代码修改为:

if (root) {
    cout << "\nThe bad team is: " ;
    printNode(root);
    printSubTree(root->left);
    cout << "\nThe good team is: ";
    printSubTree(root->right);
}