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. ATL / WTL / STL
  4. Binary tree question

Binary tree question

Scheduled Pinned Locked Moved ATL / WTL / STL
questiondata-structurestutorial
13 Posts 2 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.
  • S Offline
    S Offline
    sadas232341s
    wrote on last edited by
    #1

    I make the program to show the leaves in BST, but how to show only nodes that have leaves as children?

    int OnlyLeafNodes(Elem *t)
    {
    if(NULL == t) return 0;
    if(NULL == t->left && NULL == t->right) cout << t->key << endl;
    else return OnlyLeafNodes(t->left) + OnlyLeafNodes(t->right);
    }

    Here is the INPUT and OUTPUT that I use for test. It has to show 4, instead of 2 and 5. Because 4 is the node that has only leaves. http://img40.imageshack.us/img40/1421/treex.jpg

    B 1 Reply Last reply
    0
    • S sadas232341s

      I make the program to show the leaves in BST, but how to show only nodes that have leaves as children?

      int OnlyLeafNodes(Elem *t)
      {
      if(NULL == t) return 0;
      if(NULL == t->left && NULL == t->right) cout << t->key << endl;
      else return OnlyLeafNodes(t->left) + OnlyLeafNodes(t->right);
      }

      Here is the INPUT and OUTPUT that I use for test. It has to show 4, instead of 2 and 5. Because 4 is the node that has only leaves. http://img40.imageshack.us/img40/1421/treex.jpg

      B Offline
      B Offline
      barneyman
      wrote on last edited by
      #2

      something like this should do it

      int OnlyLeafNodes(Elem *t, int currentDepth=0)
      {
      // if 'this' is null, return
      if(!t)
      return currentDepth;

      // inc the depth because we represent another one
      currentDepth++;
      
      // ask each of the children, how deep they go, relative to me
      int leftDepth=OnlyLeafNodes(t->left,currentDepth)-currentDepth);
      int rightDepth=OnlyLeafNodes(t->right,currentDepth)-currentDepth);
      
      // spots 1,1 | 1,0 | 0,1
      if(max(leftDepth,rightDepth)==1)
      	cout << t->key << endl;  
      
      // and return the depth this node represents
      return currentDepth;
      

      }

      I haven't compiled it, sorry, but the general idea is to find all children who only descend one past the currentDepth

      S 1 Reply Last reply
      0
      • B barneyman

        something like this should do it

        int OnlyLeafNodes(Elem *t, int currentDepth=0)
        {
        // if 'this' is null, return
        if(!t)
        return currentDepth;

        // inc the depth because we represent another one
        currentDepth++;
        
        // ask each of the children, how deep they go, relative to me
        int leftDepth=OnlyLeafNodes(t->left,currentDepth)-currentDepth);
        int rightDepth=OnlyLeafNodes(t->right,currentDepth)-currentDepth);
        
        // spots 1,1 | 1,0 | 0,1
        if(max(leftDepth,rightDepth)==1)
        	cout << t->key << endl;  
        
        // and return the depth this node represents
        return currentDepth;
        

        }

        I haven't compiled it, sorry, but the general idea is to find all children who only descend one past the currentDepth

        S Offline
        S Offline
        sadas232341s
        wrote on last edited by
        #3

        what is max

        B S 2 Replies Last reply
        0
        • S sadas232341s

          what is max

          B Offline
          B Offline
          barneyman
          wrote on last edited by
          #4

          sorry

          #define max(a,b) a>b?a:b

          S 1 Reply Last reply
          0
          • S sadas232341s

            what is max

            S Offline
            S Offline
            sadas232341s
            wrote on last edited by
            #5

            And how to call the function? I mean what has to be the second argument innerDepth. I call it with the tree and the Height as second argument and it gives me 4, 3, 1 as output. I need only 4.

            B 1 Reply Last reply
            0
            • B barneyman

              sorry

              #define max(a,b) a>b?a:b

              S Offline
              S Offline
              sadas232341s
              wrote on last edited by
              #6

              Yes I define my own Max function, but why the output is 4,3,1 and not only 4?

              B 1 Reply Last reply
              0
              • S sadas232341s

                Yes I define my own Max function, but why the output is 4,3,1 and not only 4?

                B Offline
                B Offline
                barneyman
                wrote on last edited by
                #7

                i think the return should be

                // and return the depth this node represents
                return currentDepth+(maxDepth,rightdepth);
                
                S 1 Reply Last reply
                0
                • S sadas232341s

                  And how to call the function? I mean what has to be the second argument innerDepth. I call it with the tree and the Height as second argument and it gives me 4, 3, 1 as output. I need only 4.

                  B Offline
                  B Offline
                  barneyman
                  wrote on last edited by
                  #8

                  call it with

                  OnlyLeafNodes(rootNode,0);

                  1 Reply Last reply
                  0
                  • B barneyman

                    i think the return should be

                    // and return the depth this node represents
                    return currentDepth+(maxDepth,rightdepth);
                    
                    S Offline
                    S Offline
                    sadas232341s
                    wrote on last edited by
                    #9

                    maxDepth is? and why is it in brackets? Is it a function or something? i don' t know

                    S B 2 Replies Last reply
                    0
                    • S sadas232341s

                      maxDepth is? and why is it in brackets? Is it a function or something? i don' t know

                      S Offline
                      S Offline
                      sadas232341s
                      wrote on last edited by
                      #10

                      OK I think I did it it has to be

                      return currentDepth - leftDepth + rightDepth;

                      S 1 Reply Last reply
                      0
                      • S sadas232341s

                        OK I think I did it it has to be

                        return currentDepth - leftDepth + rightDepth;

                        S Offline
                        S Offline
                        sadas232341s
                        wrote on last edited by
                        #11

                        Now If you can explain me how the whole function works...

                        B 1 Reply Last reply
                        0
                        • S sadas232341s

                          maxDepth is? and why is it in brackets? Is it a function or something? i don' t know

                          B Offline
                          B Offline
                          barneyman
                          wrote on last edited by
                          #12

                          // and return the depth this node represents
                          return currentDepth+max(leftDepth,rightdepth)

                          1 Reply Last reply
                          0
                          • S sadas232341s

                            Now If you can explain me how the whole function works...

                            B Offline
                            B Offline
                            barneyman
                            wrote on last edited by
                            #13

                            it works by only printing those nodes who have only one depth below them, no more

                            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