简体   繁体   中英

Basic Binary Tree Program c++

So I have been learning all about Binary Trees and decided to write a simple program to demonstrate to myself that I can implement my knowledge into working code. All I am trying to do with this code is add 4 numbers into a binary tree and output the numbers in order from least to greatest. Although, I did run into a problem with my code. When i run the code, Visual Studio breaks it at lines 29 and 59. I believe the problem has to do with the recursive function addLeaf but maybe its something else. Any advise, solutions, or input would be greatly appreciated.!

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

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

node* root = NULL;

node* createLeaf(int data)
{
    node* n = new node;
    n->data = data;
    n->left = NULL;
    n->right = NULL;

    return n;
}
void addLeaf(int data)
{
    node* curr = root;


    //If tree is empty, create first node
    if(root == NULL)
    {
        root = createLeaf(data);

    }

    //Left(Less than)
    else if(data < curr->data)
    {
        //Check for curr->left
        if(curr->left != NULL)
        {
            addLeaf(data);
        }
        else //Adds node to left if null
        {
            curr->left = createLeaf(data);
        }
    }
    //Right(greater than)
    else if(data > curr->data)
    {
        //Check for curr->right
        if(curr->right != NULL)
        {
            addLeaf(data);
        }
        else //Adds node if right is Null
        {
            curr->right = createLeaf(data);
        }
    }
    else
    {
        cout << "The data " << data << " has already been received\n";
    }


}

void printTree(node* Ptr)
{


    if(root != NULL)
    {
        if(Ptr->left != NULL)
        {
            printTree(Ptr->left);
        }
        cout << Ptr->data << " ";
        if(Ptr->right != NULL)
        {
            printTree(Ptr->right);
        }
        cout << Ptr->data << " ";
    }
    else
    {
        cout << "The Tree is empty\n";
    }


}

int main()
{
    int data[4] = {1, 7, 5, 4};
    node* Ptr = root;


    for(int i = 0; i < 4; i++)
    {
        addLeaf(data[i]); 
    }

    printTree(Ptr);



    system("PAUSE");
    return 0;
}

one problem I can spot:

void addLeaf(int data)
{
    node* curr = root;
.....
        //Check for curr->left
        if(curr->left != NULL)
        {
            addLeaf(data);
        }

your so-called recursion did nothing. It only keep on calling addLeaf function, and the function keep on checking if root's left is not null and in turn call the addLeaf again.

Refactor all your code. Don't use any global variable. Make sure you passed correct parameters (eg you should pass the next level node to addLeaf)

The addleaf function is going to run infinitely. You only keep adding to the root without any check. You assign Ptr to root , but then using new , assign it to some new address in memory, which the root does not point to. You have to pass Ptr by reference to addLeaf , otherwise changes will be made to its copy which is destroyed as addLeaf terminates. printTree prints the current node value twice (a copy paste error?)

Here is the complete code :

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

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

node* root = NULL;

node* createLeaf(int data)
{
  node* n = new node;
  n->data = data;
  n->left = NULL;
  n->right = NULL;

  return n;
}
void addLeaf(node* &curr, int data)
{
  //If tree is empty, create first node
  if(curr == NULL)
    {
      curr = createLeaf(data);
    }

  //Left(Less than)
  else if(data < curr->data)
    {
      addLeaf (curr->left, data);
    }
  //Right(greater than)
  else if(data > curr->data)
    {
      addLeaf(curr->right, data);
    }
  else
    {
      cout << "The data " << data << " has already been received\n";
    }
}

void printTree(node* Ptr)
{


  if(root != NULL)
    {
      if(Ptr->left != NULL)
        {
      printTree(Ptr->left);
        }
      cout << Ptr->data << " ";
      if(Ptr->right != NULL)
        {
      printTree(Ptr->right);
        }
    }
  else
    {
      cout << "The Tree is empty\n";
    }


}

int main()
{
  int data[4] = {1, 7, 5, 4};

  for(int i = 0; i < 4; i++)
    {
      addLeaf(root, data[i]);
    }

  printTree(root);

  system("PAUSE");
  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