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 / C++ / MFC
  4. Binary Tree from preorder traversal

Binary Tree from preorder traversal

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
7 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.
  • A Offline
    A Offline
    aksgh
    wrote on last edited by
    #1

    Hi, I have a tree such that it can be uniquely determined from its preorder.I have information on which are internal nodes and which are leaves.Something like this ----0 ----/-\ ---0--0 --/-\--/-\ -1--0-1--1 ----/-\ ----1--1 the preorder being 001011011 I need some logic to get the tree structure from this preorder

    M O 2 Replies Last reply
    0
    • A aksgh

      Hi, I have a tree such that it can be uniquely determined from its preorder.I have information on which are internal nodes and which are leaves.Something like this ----0 ----/-\ ---0--0 --/-\--/-\ -1--0-1--1 ----/-\ ----1--1 the preorder being 001011011 I need some logic to get the tree structure from this preorder

      M Offline
      M Offline
      Malli_S
      wrote on last edited by
      #2

      Following is not the full fleshed code, it's just a skeletal that gives you an idea for implementation (as u just asked for logic ;) ). Better you implement the rest of the part.

      class node
      {
      node *m_pLeftNode;
      node *m_pRightNode;

      int m\_iData;
      

      public :

      PostOrderTravarse()
      {
      	m\_pLeftNode->PostOrderTravarse();	//Travarse the right tree
      	m\_pRightNode->PostOrderTravarse();	//Travarse the left tree
      
      	cout<<m\_iData; 				// Visiting Node;
      }	
      
      PreOrderTravarse()
      {
      	cout<<m\_iData; 				// Visiting Node;
      
      	m\_pLeftNode->PreOrderTravarse();	//Travarse the right tree
      	m\_pRightNode->PreOrderTravarse();	//Travarse the left tree
      }	
      
      InOrderTravarse()
      {
      	m\_pLeftNode->InOrderTravarse();		//Travarse the right tree
      	cout<<m\_iData; 				// Visiting Node;
      	m\_pRightNode->InOrderTravarse();	//Travarse the left tree
      }	
      

      };

      All the best !

      -Malli...! :rose:****

      A 1 Reply Last reply
      0
      • M Malli_S

        Following is not the full fleshed code, it's just a skeletal that gives you an idea for implementation (as u just asked for logic ;) ). Better you implement the rest of the part.

        class node
        {
        node *m_pLeftNode;
        node *m_pRightNode;

        int m\_iData;
        

        public :

        PostOrderTravarse()
        {
        	m\_pLeftNode->PostOrderTravarse();	//Travarse the right tree
        	m\_pRightNode->PostOrderTravarse();	//Travarse the left tree
        
        	cout<<m\_iData; 				// Visiting Node;
        }	
        
        PreOrderTravarse()
        {
        	cout<<m\_iData; 				// Visiting Node;
        
        	m\_pLeftNode->PreOrderTravarse();	//Travarse the right tree
        	m\_pRightNode->PreOrderTravarse();	//Travarse the left tree
        }	
        
        InOrderTravarse()
        {
        	m\_pLeftNode->InOrderTravarse();		//Travarse the right tree
        	cout<<m\_iData; 				// Visiting Node;
        	m\_pRightNode->InOrderTravarse();	//Travarse the left tree
        }	
        

        };

        All the best !

        -Malli...! :rose:****

        A Offline
        A Offline
        aksgh
        wrote on last edited by
        #3

        ummmm...those are tree traversals I need to construct my tree FROM a preorder traversal

        1 Reply Last reply
        0
        • A aksgh

          Hi, I have a tree such that it can be uniquely determined from its preorder.I have information on which are internal nodes and which are leaves.Something like this ----0 ----/-\ ---0--0 --/-\--/-\ -1--0-1--1 ----/-\ ----1--1 the preorder being 001011011 I need some logic to get the tree structure from this preorder

          O Offline
          O Offline
          oobimoo
          wrote on last edited by
          #4

          You may get the same sequence from different binary trees. So its not possible.

          M 1 Reply Last reply
          0
          • O oobimoo

            You may get the same sequence from different binary trees. So its not possible.

            M Offline
            M Offline
            Malli_S
            wrote on last edited by
            #5

            Yup ! I agree with you, one can't get the same by rebuilding the tree from traversals.

            -Malli...! :rose:****

            A 1 Reply Last reply
            0
            • M Malli_S

              Yup ! I agree with you, one can't get the same by rebuilding the tree from traversals.

              -Malli...! :rose:****

              A Offline
              A Offline
              aksgh
              wrote on last edited by
              #6

              Not trying the be bossy or rude...but please look at my question...my tree CAN be uniquely determined from the preorder because I have information about internal nodes and leaves.Its a huffman tree.

              O 1 Reply Last reply
              0
              • A aksgh

                Not trying the be bossy or rude...but please look at my question...my tree CAN be uniquely determined from the preorder because I have information about internal nodes and leaves.Its a huffman tree.

                O Offline
                O Offline
                oobimoo
                wrote on last edited by
                #7

                aksgh wrote:

                Its a huffman tree.

                Yes didnt see that, you are right. So, for a huffman tree you could the following

                typedef struct _node_info {
                int is_leaf;
                char value;
                }node_info;

                typedef struct _node {
                node_info inf;
                node* l;
                node* r;
                } node;

                // returns the root of the tree, from an array of node_info's structs, that
                // represents the preorder traversal of the huffman tree. we assume a global stack s
                // initially empty
                node* built_tree(node_info* preorder_array, int preorder_array_size)
                {
                node *root, *n;
                int i;
                root = malloc(sizeof(node));
                stack_push(s, root);
                for (i = 0; i < preorder_array_size; i++) {
                assert(!stack_empty(s));
                n = stack_pop(s);
                n->inf = preorder_array[i];
                if (!n->inf.is_leaf) {
                n->r = malloc(sizeof(node));
                stack_push(s, n->r);
                n->l = malloc(sizeof(node));
                stack_push(s, n->l);
                }
                }
                assert(stack_empty(s));
                return root;
                }

                * i dont set r,l members to zero when i create a node, in order to save some space, but it is something you must do

                modified on Friday, September 19, 2008 8:09 AM

                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