Thread: display tree data in binary tree format

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Arrow display tree data in binary tree format

    I'm having problems getting this display to work.
    I feel my logic is off.

    Below use level order traversal to place the data from the node into a temporary storage array.
    Code:
     
    		 binaryTreeArrayPtr[levelCounter++]=p->info;
    Below is also in my level order traversal. This is how I attempt to put NULL values into my array for holder locations
    Code:
     
    		 if (p->llink==NULL)
    		 {
    			binaryTreeArrayPtr[(2*nodeCounter)+1]=NULL;
    		 }
    		 if (p->rlink==NULL)
    		 {
    			binaryTreeArrayPtr[(2*nodeCounter)+2]=NULL;
    		 }
    Next I use this to display the details of the array in binary tree format.
    Code:
     
    //I want to try to shrink this by a using a loop
    	cout<<setw(40)<<binaryTreeArrayPtr[0]<<endl<<endl<<endl;
    	cout<<setw(35)<<"/"<<setw(10)<<"\\"<<endl<<endl;
    	cout<<setw(32)<<binaryTreeArrayPtr[1]<<setw(16)<<binaryTreeArrayPtr[2]<<endl<<endl;
    	cout<<setw(28)<<"/"<<setw(6)<<"\\"<<setw(12)<<"/"<<setw(6)<<"\\"<<endl<<endl;
    	cout<<setw(26)<<binaryTreeArrayPtr[3]<<setw(10)<<binaryTreeArrayPtr[4]<<setw(8)<<binaryTreeArrayPtr[5]
    		<<setw(10)<<binaryTreeArrayPtr[6]<<endl<<endl;
    	cout<<setw(24)<<"/"<<setw(3)<<"\\"<<setw(7)<<"/"<<setw(3)<<"\\"<<setw(6)<<"/"<<setw(3)
    		<<"\\"<<setw(7)<<"/"<<setw(3)<<"\\"<<endl<<endl;
    	cout<<setw(22)<<binaryTreeArrayPtr[7]<<setw(6)<<binaryTreeArrayPtr[8]<<setw(5)<<binaryTreeArrayPtr[9]
    		<<setw(5)<<binaryTreeArrayPtr[10]<<setw(4)<<binaryTreeArrayPtr[11]<<setw(5)<<binaryTreeArrayPtr[12]
    		<<setw(5)<<binaryTreeArrayPtr[13]<<setw(6)<<binaryTreeArrayPtr[14]<<endl;
    I'm having problems with it displaying correctly.
    see the below output.
    Code:
    //program display
    Tree nodes in levelorder: 5 2 7 1 3 6 8 4 9
    Number of Nodes: 9
    Number or Leaves: 4
                                           5
    
    
                                      /         \
    
                                   0               0
    
                               /     \           /     \
    
                             1         3       6         8
    
                           /  \      /  \     /  \      /  \
    
                         4     9-842150451-842150451-842150451-842150451-842150451-8
    42150451
    
    Press any key to end
    I was trying to see if writing all the code out would clue me into a loop or some kind of algorithm that would me to shrink the code.

    I have only found that i have an issue with my logic, and that there is no method to the madness.
    Last edited by xviddivxoggmp3; 12-08-2003 at 05:23 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Not helping you directly, but maybe this will of some use. It's a function to print a binary tree. It does it sideways, ie the far right node is at the top of the screen, the far left is at the bottom, the head node is on the left of the screen, in the middle of the left and right nodes (depending on how well balanced the tree is).

    Code:
    void PrintTree( TreePtr tp, int spaces )
    {
      int i;
    
      if( tp != NULL )
      {
        PrintTree( tp->right, spaces + 3 );
        for( i = 0; i < spaces; i++ )
          cout <<' ';
        cout << tp->data << endl;
        PrintTree( tp->left, spaces + 3 );
      }
    }
    
    
    //  Somewhere else in the program...:
    
    PrintTree( tp, 0 );
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    I'm required to print it from top - down.
    This is creating an issue with the display.
    Thanks for the code.
    I will review it to see if I can modify it to do this.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    Tha 1 Sick RAT
    Join Date
    Dec 2003
    Posts
    271
    Tripping I couldn't even read the dense stuff. If I stare will I see a picture in there somewhere?

  5. #5
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    i fixed it with a forcing a -1 value at the null locations.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary Search Tree - one last question
    By tms43 in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2006, 03:58 AM
  2. Building a Binary Search Tree off sorted data
    By BlackCitan in forum C Programming
    Replies: 0
    Last Post: 12-01-2003, 09:52 PM
  3. binary search tree help
    By noob2c in forum C++ Programming
    Replies: 6
    Last Post: 11-09-2003, 02:51 PM
  4. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM
  5. BST/Red and Black Tree
    By ghettoman in forum C++ Programming
    Replies: 0
    Last Post: 10-24-2001, 10:45 PM