c ++用数组和列表制作二叉树实现

时间:2016-02-20 23:34:07

标签: c++ arrays tree linked-list

我正在编写子二进制树实现列表。在我的代码中,我有一个列表数组。每个列表包含一个节点,后跟树上的子节点。我完成了编写代码和编译的所有内容,但我不断收到分段错误,我无法弄清楚原因。我一直在尝试调试并弄清楚我的代码在哪里搞砸了。我知道FIRST功能存在问题。它会导致分段错误。此外,当我尝试只打印一个数组列表时,它会打印所有内容。我现在已经坚持了很长时间,并希望得到一些帮助。任何人都可以提供有关FIRST和PRINT功能无效的建议吗?也许有一个我无法看到的大错误。

我的代码如下:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <array>
#include <string.h>

using namespace std;

struct node
{
   char element;
   struct node *next;
}*start;

class list
{
   public:
      void ADD(char n);
      node* CREATE(char n);
      void BEGIN(char n);
      char FIRST();
      char END();
      char NEXT(char n);
      char PREVIOUS(char n);
      int LOCATE(char n);
      void EMPTY();
      void PRINT();
      list()
      {
         start = NULL;
      }
};

char PARENT(const char n, list tree[], int length)
{
   int i=0;
   list l;
   for (i; i<length; i++)
   {
      l = tree[i];
      if (n != l.FIRST())
      {
         if (l.LOCATE(n)>0)
            return l.FIRST();
      }
   }
}

char LEFTMOST_CHILD(char n, list tree[], int length)
{
   int i;
   list l;
   for (i=0; i<length; i++)
   {
      l = tree[i];
      if (l.FIRST() == n)
         return l.NEXT(n);
   }
}

char RIGHT_SIBLING(char n, list tree[], int length)
{
   int i;
   list l;
   for (i=0; i<length; i++)
   {
      l = tree[i];
      if(n != l.FIRST())
      {
      if (l.LOCATE(n) > 0)
         {
            return l.NEXT(n);
         }
      }
   }
}

char ROOT(list tree[]) //assumes array is in order, root is first item
{
   list l;
   l = tree[0];
   cout << "Assigned tree to l" << endl;
   return l.FIRST();
}

void MAKENULL(list tree[], int length)
{
   int i;
   list l;
   for (i=0; i<length; i++)
   {
      l = tree[i];
      l.EMPTY();
   }
}

void list::PRINT()
{
   struct node *temp;
   if (start == NULL)
   {
      cout << "The list is empty" << endl;
      return;
   }
   temp = start;
   cout << "The list is: " << endl;
   while (temp != NULL)
   {
      cout << temp->element << "->" ;
      temp = temp->next;
   }
   cout << "NULL" << endl << endl;
}

void list::EMPTY()
{
   struct node *s, *n;
   s = start;
   while (s != NULL)
   {
      n = s->next;
      free(s);
      s = n;
   }  
   start = NULL;
}

int list::LOCATE(char n)
{
   int pos = 0;
   bool flag = false;
   struct node *s;
   s = start;
   while (s != NULL)
   {
      pos++;
      if (s->element == n)
      {
         flag == true;
         return pos;
      }
      s = s->next;
   }
   if (!flag)
      return -1;
}
void list::ADD(char n)
{
   struct node *temp, *s;
   temp = CREATE(n);
   s = start;
   while (s->next != NULL)
      s = s->next;
   temp->next = NULL;
   s->next = temp;
}

node *list::CREATE(char n)
{
   struct node *temp;
   temp = new(struct node);
   temp->element = n;
   temp->next = NULL;
   return temp;
}

void list::BEGIN(char n)
{
   struct node *temp, *p;
   temp = CREATE(n);
   if (start == NULL)
   {
      start = temp;
      start->next = NULL;
   }
}

char list::FIRST()
{
   char n;
   struct node *s;
   s = start;
   cout << "s = start" << endl;
   n = s->element;
   cout << "n" << endl;
   return n;
}
char list::END()
{
   struct node *s;
   s = start;
   int n;
   while (s != NULL)
   {
      n = s->element;
      s = s->next;
   }
   return n;
}

char list::NEXT(char n)
{
   char next;
   struct node *s;
   s = start;
   while (s != NULL)
   {
      if (s->element == n)
         break;
      s = s->next;
   }
   s = s->next;
   next = s->element;
   return next;
}
char list::PREVIOUS(char n)
{
   char previous;
   struct node *s;
   s = start;
   while (s != NULL)
   {
      previous = s->element;
      s = s->next;
      if (s->element == n)
         break;
   }
   return previous;
}
main()
{
   list a,b,c,d,e,f,g,h,i,j,k,l,m,n;
   a.BEGIN('A');
   b.BEGIN('B');
   c.BEGIN('C');
   d.BEGIN('D');
   e.BEGIN('E');
   f.BEGIN('F');
   g.BEGIN('G');
   h.BEGIN('H');
   i.BEGIN('I');
   j.BEGIN('J');
   k.BEGIN('K');
   l.BEGIN('L');
   m.BEGIN('M');
   n.BEGIN('N');
   a.ADD('B');
   a.ADD('C');
   b.ADD('D');
   b.ADD('E');
   e.ADD('I');
   i.ADD('M');
   i.ADD('N');
   c.ADD('F');
   c.ADD('G');
   c.ADD('H');
   g.ADD('J');
   g.ADD('K');
   h.ADD('L');
   a.PRINT();
   list tree[] = {a,b,c,d,e,f,g,h,i,j,k,l,m,n};
   int length = sizeof(tree)/sizeof(char);

   char root = ROOT(tree);
   cout << "Found root" << endl;
   char parent = PARENT('G', tree, length);
   cout << "Found Parent" << endl;
   char leftChild = LEFTMOST_CHILD('C', tree, length);
   cout << "found left child" << endl;
   char rightSibling = RIGHT_SIBLING('D', tree, length);
   cout << "found right sibling" << endl;

   cout << "The root of the tree is: ";
   cout << root << endl;
   cout << "The parent of G is: ";
   cout << parent << endl;
   cout << "The leftmost child of C is" ;
   cout << leftChild << endl;
   cout << "The right sibling of D is: " ;
   cout << rightSibling << endl;
}

任何帮助将非常感谢。谢谢你!

1 个答案:

答案 0 :(得分:0)

根本问题在于,在测试任何代码之前,您已编写了大量代码。编写代码时,从简单易用的东西开始,一次增加一点复杂性,在每一步测试,永远不会添加到无效的代码。

具体问题(或至少一个致命问题)在这里:

struct node
{
  char element;
  struct node *next;
}*start;

class list
{
public:
  //...
  list()
  {
    start = NULL;
  }
};

变量start全局变量。类list没有成员变量,但使用全局变量。每次构造start时,它都会将list设置为NULL,并且每个list都会使用相同的指针进行混乱。函数FIRST取消引用指针而不检查指针是否为NULL,如果是,则得到未定义的行为。

目前还不完全清楚你的意图,但你似乎误解了变量在C ++中是如何工作的。