Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Printing BST

Printing BST

Scheduled Pinned Locked Moved C#
data-structuresquestion
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KeithF
    wrote on last edited by
    #1

    Hi Folks, I Need to print a Binary TREE like so: 10 / \ 5 15 / / \ 2 14 17 I have the code to add to the tree and that all works ok and i have a method to print out the full tree starting at the root, then all the left nodes then the right nodes:

    public void ShowTree(Node T)
    {
    if (T != null)
    {
    Console.Write(T.DATA+ "\n");
    ShowTree(T.LEFT);
    ShowTree(T.RIGHT);
    }

        }
    

    I have tried something like this:

    private static void ShowTree(Node tp, int spaces)
    {
    int i = 0;

            if (tp != null)
            {
                ShowTree(tp.left, spaces + 3);
    
                for (i = 0; i < spaces; i++)
                {
                    Console.Write(" ");
                }
    
                Console.WriteLine(tp.data);
                ShowTree(tp.right, spaces + 3);
            }
        }
    

    And a few other approaches but i cannot get it to do as i want. Any suggestions on how i can do this?

    A 1 Reply Last reply
    0
    • K KeithF

      Hi Folks, I Need to print a Binary TREE like so: 10 / \ 5 15 / / \ 2 14 17 I have the code to add to the tree and that all works ok and i have a method to print out the full tree starting at the root, then all the left nodes then the right nodes:

      public void ShowTree(Node T)
      {
      if (T != null)
      {
      Console.Write(T.DATA+ "\n");
      ShowTree(T.LEFT);
      ShowTree(T.RIGHT);
      }

          }
      

      I have tried something like this:

      private static void ShowTree(Node tp, int spaces)
      {
      int i = 0;

              if (tp != null)
              {
                  ShowTree(tp.left, spaces + 3);
      
                  for (i = 0; i < spaces; i++)
                  {
                      Console.Write(" ");
                  }
      
                  Console.WriteLine(tp.data);
                  ShowTree(tp.right, spaces + 3);
              }
          }
      

      And a few other approaches but i cannot get it to do as i want. Any suggestions on how i can do this?

      A Offline
      A Offline
      Alan Balkany
      wrote on last edited by
      #2

      The secret to drawing a binary tree is to start with the leaves. A depth-first search will go through the leaves in order from left to right. Increment each leaf's X coordinate to allow space for the previous leaf. The Y coordinate comes from the depth of the leaf's level. When the coordinates have been assigned to all of an interior node's sons, you can use them to position the interior node in the middle, above its sons. When all nodes have coordinates, just cycle through them and draw them.

      "Microsoft -- Adding unnecessary complexity to your work since 1987!"

      A 1 Reply Last reply
      0
      • A Alan Balkany

        The secret to drawing a binary tree is to start with the leaves. A depth-first search will go through the leaves in order from left to right. Increment each leaf's X coordinate to allow space for the previous leaf. The Y coordinate comes from the depth of the leaf's level. When the coordinates have been assigned to all of an interior node's sons, you can use them to position the interior node in the middle, above its sons. When all nodes have coordinates, just cycle through them and draw them.

        "Microsoft -- Adding unnecessary complexity to your work since 1987!"

        A Offline
        A Offline
        April Fans
        wrote on last edited by
        #3

        Wow, couldn't have said it better myself! :) :) Thanks Alan!

        April Comm100 - Leading Live Chat Software Provider

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups