Closed Thread
Results 1 to 8 of 8

Thread: Binary Search Tree Construction Help

  1. #1
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Binary Search Tree Construction Help

    Can anyone please help me out on how to construct a tree from its traversals? i know that we see inorder and preorder traversal but i don't know how to

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Binary Search Tree Construction Help

    Can you offer some sample data to illustrate the process with?
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  4. #3
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Re: Binary Search Tree Construction Help

    pre order = A B C D E F G H I J K L M
    in order = C E D F B A H J I K G M L

    these are the pre order and in order traversal of the tree which i want to construct

  5. #4
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143

    Re: Binary Search Tree Construction Help

    In order represents the order of the letters in the tree when scanned left-to-right, ignoring height in the tree.

    pre-order gives you their position in height. So A, for example, is the root, and B is the left child of A.

    So, preorder tells you that A is the root. in order tells you that C, E, D, F, and B are all on the left branch of A.

    Tree traversal - Wikipedia, the free encyclopedia
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: Binary Search Tree Construction Help

    This is giving me a headache. An "inorder" (left, root, right) traversal is ment to give a sorted result. So, "C E D F B A H J I K G M L" either isn't inorder or the tree isn't sorted, so you cant work out the tree from this.

    If "C E D F B A H J I K G M L" is preorder (root, left, right), then I get:

    Code:
            H
       A         J
        B       I K
         C         G
          E       M
         D F     L
    But, then a preorder traversal prints something else (?) Anyway, here is some code. use it as you will

    Tree traversal - Wikipedia, the free encyclopedia

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct Node
    {
      char c;
      struct Node* lt;
      struct Node* rt;
    } Node;
    
    Node* groot;
    
    Node* NodeCreate(char c) {
      Node* node = calloc(1, sizeof(Node));
      node->c = c;
      return node;
    }
    
    Node** NodeFind(Node** node, char c) {
      return *node ? c < (*node)->c ?
        NodeFind(&(*node)->lt, c) :
        NodeFind(&(*node)->rt, c) : node;
    }
    
    Node* NodeAdd(char c) {
      return *NodeFind(&groot, c) = NodeCreate(c);
    }
    
    void NodeOrderPre(Node* node) {
      if (!node) return;
      printf("&#37;c", node->c);
      NodeOrderPre(node->lt);
      NodeOrderPre(node->rt);
    }
    
    void NodeOrderIn(Node* node) {
      if (!node) return;
      NodeOrderIn(node->lt);
      printf("%c", node->c);
      NodeOrderIn(node->rt);
    }
    
    void NodeOrderPost(Node* node) {
      if (!node) return;
      NodeOrderPost(node->lt);
      NodeOrderPost(node->rt);
      printf("%c", node->c);
    }
    
    int main(int argc, char* argv[])
    {
      char s[100], *p = "gmbdlahkfcjie";
      while (*p) NodeAdd(*p++);
      printf("preorder: "); NodeOrderPre(groot); puts("");
      printf("inorder : "); NodeOrderIn (groot); puts("");
      gets(s);
      return 0;
    }

  7. #6
    ahmed is offline Programming Professional
    Join Date
    Oct 2008
    Posts
    300
    Rep Power
    0

    Re: Binary Search Tree Construction Help

    thanks a lot i got it how we do it

  8. #7
    vanexa7 is offline Newbie
    Join Date
    Feb 2010
    Posts
    1
    Rep Power
    0

    Re: Binary Search Tree Construction Help

    hi i have a problem similar to this but in my case i have the inorder and postorder, i'm doing it in java but if you tell me what i need to change in my case i think probably i can adapt it to java...

  9. #8
    alienkinetics's Avatar
    alienkinetics is offline Programmer
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154
    Rep Power
    0

    Re: Binary Search Tree Construction Help

    I did include a NodeOrderPost() function with the ANSIC code which is based on Wikipedia's definition.

    I dont think the code will convert to Java because it relies on a language to have "pass by reference", so the code would have to be rewritten.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. problem with a binary search tree
    By hadas_zr in forum C and C++
    Replies: 3
    Last Post: 07-14-2011, 07:43 AM
  2. Intermediate Check if a given a binary tree, if it is BST or not
    By fayyazlodhi in forum C Tutorials
    Replies: 0
    Last Post: 07-10-2011, 07:33 AM
  3. Tail Recursion (Binary Search Tree height() implementation)
    By CoryG89 in forum General Programming
    Replies: 1
    Last Post: 03-30-2011, 07:08 AM
  4. Binary tree
    By tomy in forum C and C++
    Replies: 6
    Last Post: 08-16-2010, 11:58 PM
  5. Replies: 1
    Last Post: 11-23-2007, 08:09 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts