基本二进制树程序c++

Basic Binary Tree Program c++

本文关键字:程序 c++ 二进制      更新时间:2023-10-16

所以我一直在学习关于二进制树的所有知识,并决定写一个简单的程序来向自己证明我可以将我的知识实现到工作代码中。我对这段代码所做的就是将4个数字添加到一个二叉树中,并按从最小到最大的顺序输出这些数字。尽管如此,我的代码确实遇到了问题。当我运行代码时,VisualStudio会在第29行和第59行将其分解。我相信这个问题与递归函数addLeaf有关,但可能是其他问题。任何建议、解决方案或意见都将不胜感激。!

#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 receivedn";
    }

}
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 emptyn";
    }

}
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;
}

我能发现的一个问题:

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

你所谓的递归没有任何作用。它只继续调用addLeaf函数,该函数继续检查root的左边是否为空,然后再次调用addLeaf

重构所有代码。不要使用任何全局变量。确保您传递了正确的参数(例如,您应该将下一级节点传递给addLeaf)

addleaf函数将无限运行。您只不断添加到根目录,而不进行任何检查。将Ptr分配给root,然后使用new将其分配给内存中的某个新地址,而根并不指向该地址。您必须通过引用addLeaf来传递Ptr,否则将对其副本进行更改,该副本将在addLeaf终止时销毁。printTree打印当前节点值两次(复制粘贴错误?)

这是完整的代码:

#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 receivedn";
    }
}
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 emptyn";
    }

}
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;
}