+ Reply to Thread
Results 1 to 8 of 8

Thread: Binary Search Tree Construction Help

  1. #1
    Programming Professional ahmed is an unknown quantity at this point
    Join Date
    Oct 2008
    Posts
    214

    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. #2
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    Re: Binary Search Tree Construction Help

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

  3. #3
    Programming Professional ahmed is an unknown quantity at this point
    Join Date
    Oct 2008
    Posts
    214

    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

  4. #4
    Super Moderator WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther has much to be proud of WingedPanther's Avatar
    Join Date
    Jul 2006
    Age
    36
    Posts
    11,689
    Blog Entries
    57

    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
    CodeCall Blog | CodeCall Wiki | Shareware
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #5
    Programmer alienkinetics has a little shameless behaviour in the past alienkinetics's Avatar
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154

    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("%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;
    }

  6. #6
    Programming Professional ahmed is an unknown quantity at this point
    Join Date
    Oct 2008
    Posts
    214

    Re: Binary Search Tree Construction Help

    thanks a lot i got it how we do it

  7. #7
    Newbie vanexa7 is an unknown quantity at this point
    Join Date
    Feb 2010
    Posts
    1

    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...

  8. #8
    Programmer alienkinetics has a little shameless behaviour in the past alienkinetics's Avatar
    Join Date
    Feb 2010
    Location
    Australia
    Posts
    154

    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.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

     

Similar Threads

  1. Trees 101
    By WingedPanther in forum C Tutorials
    Replies: 2
    Last Post: 01-31-2009, 09:54 AM
  2. Google Hacking
    By John in forum Tutorials
    Replies: 6
    Last Post: 07-22-2008, 09:37 AM
  3. Replies: 1
    Last Post: 11-23-2007, 10:09 AM
  4. need help with simple C++ TicTacToe game with AI
    By flupke1 in forum C and C++
    Replies: 11
    Last Post: 08-14-2007, 10:27 AM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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