Thread: display tree data in binary tree format

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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