确定二叉树的高度

时间:2013-09-12 06:52:58

标签: height binary-tree

我编写了一个代码来创建和显示二叉树。这个程序工作并满足了我所在课程的要求,但是之后我试图添加到工作程序中找到树的高度,到目前为止没有运气。我查看了许多示例,并且我对我的程序所做的任何添加或更改均无效。

#include <cstdlib>
#include <iostream>
#include <cstdio>

using namespace std;

class TreeNode
{     public:
             TreeNode *pLeft;
             int data;
             TreeNode *pRight;
             TreeNode *leftHeight;
             TreeNode *rightHeight;


             TreeNode(int num)
             { 
               data = num;
               pLeft = NULL;
               pRight = NULL;

             }
}; ////////////////////////////////////////////
   class Binary_Tree
   {     private:
             //int getHeight;

         public:
            TreeNode *rootPtr;

             Binary_Tree()
             { rootPtr = NULL;
         }
      /* ---------------------------------- */                
        void Insert(int value)
         {
              TreeNode *pNewNode = new TreeNode(value);
              TreeNode *pCurrent;
              TreeNode *pPrevious;

              pPrevious = NULL;
              pCurrent = rootPtr;

              while(pCurrent != NULL)
              {   pPrevious = pCurrent;
                  if (value < (pPrevious -> data))
                       pCurrent = pCurrent -> pLeft;
                  else 
                       pCurrent = pCurrent -> pRight;
              }
              if(pPrevious == NULL)
              {   rootPtr = pNewNode;
              }
              else
              {     if (value < (pPrevious -> data))
                    {   pPrevious -> pLeft = pNewNode;
                        pNewNode -> pLeft = pCurrent;
                    }
                    else
                    {   pPrevious -> pRight = pNewNode;
                        pNewNode -> pRight = pCurrent;
                    }
               }
         }

         /* ------------------------------------------- */

         int getHeight(TreeNode *TreePtr)
         {
                       if( TreePtr == NULL)
         {
              return(0);
              }

              else
                  {
                        leftHeight = getHeight(r->pLeft);
                        rightHeight = getHeight(r->pRight);

                        if(leftHeight > rightHeight)
                        {
                                      return(leftHeight + 1);
                        }
                        else
                        {
                            return(rightHeight + 1);
                        }
                  }
              }
         /* ------------------------------------------- */

         void Display(TreeNode *TreePtr, int count)
         {

                         if(TreePtr !=NULL)
                          {
                          count++;
                          Display(TreePtr -> pRight, count);
                          for(int i = i; i < count; i++)
                          {
                                  printf("  "); 

                          }

             printf("%d \n", TreePtr -> data);

              Display(TreePtr -> pLeft, count);
                               }
              }//////////////////////end display //////////////////////

                          void DisplayInOrder(TreeNode *TreePtr)
                          {
                               if(TreePtr != NULL)
                               {
                                          DisplayInOrder(TreePtr -> pLeft);

                                          cout << TreePtr -> data << endl;

                                          DisplayInOrder(TreePtr -> pRight);
                               }
                          }

              };
                          /* -------------------------------------- */



         /////////////////////////////////////////////////////////////


         int main()
         { int number[8] = {7, 9, 3, 2, 12, 5, 8};
             Binary_Tree Tree; 

             for (int i = 0; i <8; i++)
                 {
                      cout << "data inserted = "<< number[i] << endl;
                      Tree.Insert(number[i]);
                 }
                 cout << endl << "Display Tree" << endl << endl;
                 Tree.Display(Tree.rootPtr, 0);
                 cout << endl;

                 cout << endl << "tree height" << endl << endl;
                 getHeight(root);
                 cout << endl;


                 Tree.DisplayInOrder(Tree.rootPtr);

                 system("PAUSE");
                 return EXIT_SUCCESS;
         }

2 个答案:

答案 0 :(得分:0)

有编译问题。固定后,它似乎打印高度。请注意值为4

的“最终高度”行
#include <cstdlib>
#include <iostream>
#include <cstdio>

using namespace std;

class TreeNode
{     public:
             TreeNode *pLeft;
             int data;
             TreeNode *pRight;
             TreeNode *leftHeight;
             TreeNode *rightHeight;


             TreeNode(int num)
             { 
               data = num;
               pLeft = NULL;
               pRight = NULL;

             }
}; ////////////////////////////////////////////
   class Binary_Tree
   {     private:
             //int getHeight;

         public:
            TreeNode *rootPtr;

             Binary_Tree()
             { rootPtr = NULL;
         }
      /* ---------------------------------- */                
        void Insert(int value)
         {
              TreeNode *pNewNode = new TreeNode(value);
              TreeNode *pCurrent;
              TreeNode *pPrevious;

              pPrevious = NULL;
              pCurrent = rootPtr;

              while(pCurrent != NULL)
              {   pPrevious = pCurrent;
                  if (value < (pPrevious -> data))
                       pCurrent = pCurrent -> pLeft;
                  else 
                       pCurrent = pCurrent -> pRight;
              }
              if(pPrevious == NULL)
              {   rootPtr = pNewNode;
              }
              else
              {     if (value < (pPrevious -> data))
                    {   pPrevious -> pLeft = pNewNode;
                        pNewNode -> pLeft = pCurrent;
                    }
                    else
                    {   pPrevious -> pRight = pNewNode;
                        pNewNode -> pRight = pCurrent;
                    }
               }
         }

         /* ------------------------------------------- */

         int getHeight(TreeNode *TreePtr)
         {
                       if( TreePtr == NULL)
         {
              return(0);
              }

              else
                  {
                        int leftHeight = getHeight(TreePtr->pLeft);
                        int rightHeight = getHeight(TreePtr->pRight);

                        if(leftHeight > rightHeight)
                        {
                                      return(leftHeight + 1);
                        }
                        else
                        {
                            return(rightHeight + 1);
                        }
                  }
              }
         /* ------------------------------------------- */

         void Display(TreeNode *TreePtr, int count)
         {

                         if(TreePtr !=NULL)
                          {
                          count++;
                          Display(TreePtr -> pRight, count);
                          for(int i = i; i < count; i++)
                          {
                                  printf("  "); 

                          }

             printf("%d \n", TreePtr -> data);

              Display(TreePtr -> pLeft, count);
                               }
              }//////////////////////end display //////////////////////

                          void DisplayInOrder(TreeNode *TreePtr)
                          {
                               if(TreePtr != NULL)
                               {
                                          DisplayInOrder(TreePtr -> pLeft);

                                          cout << TreePtr -> data << endl;

                                          DisplayInOrder(TreePtr -> pRight);
                               }
                          }

              };
                          /* -------------------------------------- */



         /////////////////////////////////////////////////////////////


         int main()
         { int number[8] = {7, 9, 3, 2, 12, 5, 8};
             Binary_Tree Tree; 

             for (int i = 0; i <8; i++)
                 {
                      cout << "data inserted = "<< number[i] << endl;
                      Tree.Insert(number[i]);
                 }
                 cout << endl << "Display Tree" << endl << endl;
                 Tree.Display(Tree.rootPtr, 0);
                 cout << endl;

                 cout << endl << "tree height" << endl << endl;
                 int height = Tree.getHeight(Tree.rootPtr);
                 cout << endl;
                 cout << "final height: " << height << endl;


                 Tree.DisplayInOrder(Tree.rootPtr);

                 system("PAUSE");
                 return EXIT_SUCCESS;
         }

答案 1 :(得分:0)

您的代码中有一些错误:

  • 您正在使用r,它应该是TreePtr
  • 您没有声明root,我想您想使用Tree
  • 需要声明
  • leftHeightrightHeight
  • getHeight的声明在课堂内,因此您需要致电Tree.getHeight。顺便说一句,这个方法不应该在类中,或者不应该得到树节点。

这是固定代码:

#include <cstdlib>
#include <iostream>
#include <cstdio>

using namespace std;

class TreeNode
{     public:
             TreeNode *pLeft;
             int data;
             TreeNode *pRight;
             TreeNode *leftHeight;
             TreeNode *rightHeight;


             TreeNode(int num)
             { 
               data = num;
               pLeft = NULL;
               pRight = NULL;

             }
}; ////////////////////////////////////////////
   class Binary_Tree
   {     private:
             //int getHeight;

         public:
            TreeNode *rootPtr;

             Binary_Tree()
             { rootPtr = NULL;
         }
      /* ---------------------------------- */                
        void Insert(int value)
         {
              TreeNode *pNewNode = new TreeNode(value);
              TreeNode *pCurrent;
              TreeNode *pPrevious;

              pPrevious = NULL;
              pCurrent = rootPtr;

              while(pCurrent != NULL)
              {   pPrevious = pCurrent;
                  if (value < (pPrevious -> data))
                       pCurrent = pCurrent -> pLeft;
                  else 
                       pCurrent = pCurrent -> pRight;
              }
              if(pPrevious == NULL)
              {   rootPtr = pNewNode;
              }
              else
              {     if (value < (pPrevious -> data))
                    {   pPrevious -> pLeft = pNewNode;
                        pNewNode -> pLeft = pCurrent;
                    }
                    else
                    {   pPrevious -> pRight = pNewNode;
                        pNewNode -> pRight = pCurrent;
                    }
               }
         }

         /* ------------------------------------------- */

         int getHeight(TreeNode *TreePtr)
         {
                       if( TreePtr == NULL)
         {
              return(0);
              }

              else
                  {
                        int leftHeight = getHeight(TreePtr->pLeft);
                        int rightHeight = getHeight(TreePtr->pRight);

                        if(leftHeight > rightHeight)
                        {
                                      return(leftHeight + 1);
                        }
                        else
                        {
                            return(rightHeight + 1);
                        }
                  }
              }
         /* ------------------------------------------- */

         void Display(TreeNode *TreePtr, int count)
         {

                         if(TreePtr !=NULL)
                          {
                          count++;
                          Display(TreePtr -> pRight, count);
                          for(int i = i; i < count; i++)
                          {
                                  printf("  "); 

                          }

             printf("%d \n", TreePtr -> data);

              Display(TreePtr -> pLeft, count);
                               }
              }//////////////////////end display //////////////////////

                          void DisplayInOrder(TreeNode *TreePtr)
                          {
                               if(TreePtr != NULL)
                               {
                                          DisplayInOrder(TreePtr -> pLeft);

                                          cout << TreePtr -> data << endl;

                                          DisplayInOrder(TreePtr -> pRight);
                               }
                          }

              };
                          /* -------------------------------------- */



         /////////////////////////////////////////////////////////////


         int main()
         { int number[8] = {7, 9, 3, 2, 12, 5, 8};
             Binary_Tree Tree; 

             for (int i = 0; i <8; i++)
                 {
                      cout << "data inserted = "<< number[i] << endl;
                      Tree.Insert(number[i]);
                 }
                 cout << endl << "Display Tree" << endl << endl;
                 Tree.Display(Tree.rootPtr, 0);
                 cout << endl;

                 cout << endl << "tree height" << endl << endl;
                 cout << Tree.getHeight(Tree.rootPtr) << endl;
                 cout << endl;


                 Tree.DisplayInOrder(Tree.rootPtr);

                 system("PAUSE");
                 return EXIT_SUCCESS;
         }