Thread: a little help with node and BST plz...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    a little help with node and BST plz...

    insert
    Code:
    So, a binary search tree is basically a data structure created by node?
    and what exactly a node does?
    does a single node always have 3 boxes? (the key, left and right?)
    
    i have to write a program that will print out a tree...
    
     * Each node should be printed on a separate line;
     * the left subtree should be printed after the root;
     * The right subtree should be printed before the root;
     * The key value should be indented by 2h-2 spaces where h is the height of the node containing it. That is, the root should not be indented, the keys in its subtrees should be intended 2 spaces, the keys in their subtrees 4 spaces, and so on. 
    
    For example, the complete tree containing {1,2,3,4,5,6} would be printed like this:
    
      6
        5
    4
        3
      2
        1
    
    
    I think i just dont understand the question...
    and have a lot of unconcern....e.g., what is the height? aren't they just nodes?
    
    can someone give me some hints on where should i start?
    thx!!

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    37
    Actually in a BST ( binary search tree ) each node will contain an individual values. These are considered as keys.
    The properties of the binary search trees are as follows

    The left subtree of a node contains only nodes with keys less than the node's key.
    The right subtree of a node contains only nodes with keys greater than the node's key.
    Both the left and right subtrees must also be binary search trees.

    Example
    Code:
                                 6 
                               /   \
                        lst  5     7  rst
                            /  \
                           1   8
    lst means left sub tree
    rst means right sub tree
    Last edited by thillai_selvan; 02-26-2010 at 12:37 AM.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    Yes each node need 3 memory location , one is for store a nodes value then two pointers is point
    a another node in the left and one another in the right of its node.

    And we can traverser a whole tree ( for printing its value ) by the following way :
    o.post order traversal
    o.pre-order traversal
    o.in-order traversal
    I hope that u need to print the tree in post-order traversal

    The height of the tree is nothing but a level of the tree
    for ex:

    Code:
     
             1
       2          3
    4   5     6    7 
    
    This tree has height 3

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program terminates abruptly
    By roaan in forum C Programming
    Replies: 3
    Last Post: 08-28-2009, 03:53 PM
  2. parent in a binary search tree
    By roaan in forum C Programming
    Replies: 4
    Last Post: 08-26-2009, 07:08 PM
  3. Replies: 8
    Last Post: 04-28-2008, 02:46 AM
  4. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  5. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM