Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to recover a binary tree from a traversal?
QtWS25 Last Chance

How to recover a binary tree from a traversal?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 2 Posters 1.4k Views
  • 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.
  • M Offline
    M Offline
    MasterBlade
    wrote on last edited by
    #1

    Hello I have a pre-order traversal of a binary tree stored in an array and I want to recover the tree.

    I know it's impossible with one traversal only but I have more info.

    For every node retrieved from the array I can use a function to get how many children (0, 1 or 2) that node has.

    In addition, if a node has only 1 child, that must be its left child.

    I think that's enough info to recover the tree. But I tried various ways and can't find a good solution.

    Any suggestions?

    Here is an easy example. The data for this tree would look like G2-D2-A0-F1-E0-M2-H0-Z0
    0_1534908784633_795187-20151023201552927-578458496.png

    jsulmJ 1 Reply Last reply
    0
    • M MasterBlade

      Hello I have a pre-order traversal of a binary tree stored in an array and I want to recover the tree.

      I know it's impossible with one traversal only but I have more info.

      For every node retrieved from the array I can use a function to get how many children (0, 1 or 2) that node has.

      In addition, if a node has only 1 child, that must be its left child.

      I think that's enough info to recover the tree. But I tried various ways and can't find a good solution.

      Any suggestions?

      Here is an easy example. The data for this tree would look like G2-D2-A0-F1-E0-M2-H0-Z0
      0_1534908784633_795187-20151023201552927-578458496.png

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @MasterBlade Is it a binary search tree? I mean is left_node <= current_node < right_node?
      If this is the case: is the array sorted?
      If the answers to both questions are yes, then you can implement it like a binary search: start in the middle, the value in the middle will be root node. Then you can recursively call same function for left and for right parts of the array and so on. This is an exercise I asked candidates in our company during interviews :-)

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      0
      • jsulmJ jsulm

        @MasterBlade Is it a binary search tree? I mean is left_node <= current_node < right_node?
        If this is the case: is the array sorted?
        If the answers to both questions are yes, then you can implement it like a binary search: start in the middle, the value in the middle will be root node. Then you can recursively call same function for left and for right parts of the array and so on. This is an exercise I asked candidates in our company during interviews :-)

        M Offline
        M Offline
        MasterBlade
        wrote on last edited by
        #3

        @jsulm said in How to recover a binary tree from a traversal?:

        @MasterBlade Is it a binary search tree? I mean is left_node <= current_node < right_node?
        If this is the case: is the array sorted?
        If the answers to both questions are yes, then you can implement it like a binary search: start in the middle, the value in the middle will be root node. Then you can recursively call same function for left and for right parts of the array and so on. This is an exercise I asked candidates in our company during interviews :-)

        Thank you for the quick reply!

        I'm sorry I have to answer no to both questions. :-(

        Actually each node on the tree has some specific meaning. These IDs are corresponding to different functionalities.

        You can see from this program that there is a logic structure in the tree. It's not a basic data stream and cannot be sorted.

        What I actually stored on the disk is a sequence of 1.34.60.0.101 which is a preorder traversal of the tree. The good news is that I know how many children a node has from its ID #. That makes things a lot easier.
        0_1534911665708_4.PNG

        jsulmJ 1 Reply Last reply
        0
        • M MasterBlade

          @jsulm said in How to recover a binary tree from a traversal?:

          @MasterBlade Is it a binary search tree? I mean is left_node <= current_node < right_node?
          If this is the case: is the array sorted?
          If the answers to both questions are yes, then you can implement it like a binary search: start in the middle, the value in the middle will be root node. Then you can recursively call same function for left and for right parts of the array and so on. This is an exercise I asked candidates in our company during interviews :-)

          Thank you for the quick reply!

          I'm sorry I have to answer no to both questions. :-(

          Actually each node on the tree has some specific meaning. These IDs are corresponding to different functionalities.

          You can see from this program that there is a logic structure in the tree. It's not a basic data stream and cannot be sorted.

          What I actually stored on the disk is a sequence of 1.34.60.0.101 which is a preorder traversal of the tree. The good news is that I know how many children a node has from its ID #. That makes things a lot easier.
          0_1534911665708_4.PNG

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @MasterBlade Then I would say that you need to store more information besides the actual ID values to be able to restore the tree, else I cannot see how you can know to which node a value belongs.

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • jsulmJ jsulm

            @MasterBlade Then I would say that you need to store more information besides the actual ID values to be able to restore the tree, else I cannot see how you can know to which node a value belongs.

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

            @jsulm said in How to recover a binary tree from a traversal?:

            @MasterBlade Then I would say that you need to store more information besides the actual ID values to be able to restore the tree, else I cannot see how you can know to which node a value belongs.

            That's true. I need to call an external function to know how many children a node has. I can rebuild the whole tree on a piece of paper. But I can't find a good way to convert it to codes. Could you give me some suggestions?

            jsulmJ 1 Reply Last reply
            0
            • M MasterBlade

              @jsulm said in How to recover a binary tree from a traversal?:

              @MasterBlade Then I would say that you need to store more information besides the actual ID values to be able to restore the tree, else I cannot see how you can know to which node a value belongs.

              That's true. I need to call an external function to know how many children a node has. I can rebuild the whole tree on a piece of paper. But I can't find a good way to convert it to codes. Could you give me some suggestions?

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @MasterBlade "G2-D2-A0-F1-E0-M2-H0-Z0"
              To me it looks like you need to start on the left side. First node is the root. Next one is child, either left or right side depending on value. You continue this way until a node with 0 children, in which case you go one level up.
              So:

              • Make G root
              • Make D left child
              • D has child A, so put it as child of D
              • A does not have children go back to D
              • There is child F put it as right child of D
              • ...
                Something like this.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              0
              • jsulmJ jsulm

                @MasterBlade "G2-D2-A0-F1-E0-M2-H0-Z0"
                To me it looks like you need to start on the left side. First node is the root. Next one is child, either left or right side depending on value. You continue this way until a node with 0 children, in which case you go one level up.
                So:

                • Make G root
                • Make D left child
                • D has child A, so put it as child of D
                • A does not have children go back to D
                • There is child F put it as right child of D
                • ...
                  Something like this.
                M Offline
                M Offline
                MasterBlade
                wrote on last edited by
                #7

                @jsulm said in How to recover a binary tree from a traversal?:

                @MasterBlade "G2-D2-A0-F1-E0-M2-H0-Z0"
                To me it looks like you need to start on the left side. First node is the root. Next one is child, either left or right side depending on value. You continue this way until a node with 0 children, in which case you go one level up.
                So:

                • Make G root
                • Make D left child
                • D has child A, so put it as child of D
                • A does not have children go back to D
                • There is child F put it as right child of D
                • ...
                  Something like this.

                It seems a little bit complicated, especially the "going back" part. But I can give it a try.
                Is it possible for a recursion solution?

                jsulmJ 1 Reply Last reply
                0
                • M MasterBlade

                  @jsulm said in How to recover a binary tree from a traversal?:

                  @MasterBlade "G2-D2-A0-F1-E0-M2-H0-Z0"
                  To me it looks like you need to start on the left side. First node is the root. Next one is child, either left or right side depending on value. You continue this way until a node with 0 children, in which case you go one level up.
                  So:

                  • Make G root
                  • Make D left child
                  • D has child A, so put it as child of D
                  • A does not have children go back to D
                  • There is child F put it as right child of D
                  • ...
                    Something like this.

                  It seems a little bit complicated, especially the "going back" part. But I can give it a try.
                  Is it possible for a recursion solution?

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @MasterBlade Yes, you can do it recursively

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  M 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @MasterBlade Yes, you can do it recursively

                    M Offline
                    M Offline
                    MasterBlade
                    wrote on last edited by
                    #9

                    @jsulm said in How to recover a binary tree from a traversal?:

                    @MasterBlade Yes, you can do it recursively

                    Could you give me a hint :)

                    jsulmJ 1 Reply Last reply
                    0
                    • M MasterBlade

                      @jsulm said in How to recover a binary tree from a traversal?:

                      @MasterBlade Yes, you can do it recursively

                      Could you give me a hint :)

                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      @MasterBlade Well, pass current node as parent for the next, the array and current position in it to the recursive function, something like this. Please figure out the rest, I don't have time to implement it for you. Your question is not even related to Qt.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved