finding height of a binary search tree without using recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AceKnocks
    New Member
    • Jan 2008
    • 14

    finding height of a binary search tree without using recursion

    I have been given this interface,
    Code:
    interface BinarySearchTree {
       public void insert(Integer data);
       public int size();
       public int height();
       public boolean contains(Integer target);
    }
    and I have to implement BST with all these functions. I have implemented the first insert and size like this way -
    Code:
    class Node {
        Node left, right;
        Integer data;
        Node () {
            left = right = null;
            data = 0;
        }
    }
    
    public class BSTree extends Node implements BinarySearchTree {
        static Node root;
        static int countNode;
        /**
         * Creates a new instance of BSTree
         */
        public BSTree() {
            root = null;
        }
        public void insert(Integer data) {
            if (root == null) {
                root.data = data;
                countNode++;
            } else {
                Node temp = new Node();
                temp = root;
                while (temp != null) {
                    if (temp.data < data) temp = temp.right;
                    else {
                        temp = temp.left;
                    }
                    temp.data = data;
                    countNode++;
                }
            }
        }
        public int size () {
            return countNode;
        }
        
        public int height() { 
            Node temp = new Node();
            /* could have used these for recursion 
            final boolean flag = true;
            if (flag) */
                temp = root;
            if (temp == null) {
                return 0;
            } else {
            /* would have been easy to find height using this recursion
                return 1 + max(height(temp.left), height(temp.right)); */
            }
        }
        
        public boolean contains (Integer target) {
            
        }
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            BSTree bs = new BSTree();
            bs.insert(12);
            bs.insert(3);
            bs.insert(14);
        }
        
    }
    The objective requires that the height be implemented without using an argument. Do you have some ideas?
  • BigDaddyLH
    Recognized Expert Top Contributor
    • Dec 2007
    • 1216

    #2
    Hint: methods are applied to objects. With your height method, you seem to only want to pass objects as arguments. Hmmm...

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Most recursive methods dealing with trees can be written with some loops - try thinking of, in general, what your recursive calls would do, then try to mimic that with loops.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Originally posted by Ganon11
        Most recursive methods dealing with trees can be written with some loops - try thinking of, in general, what your recursive calls would do, then try to mimic that with loops.
        I didn't see anywhere (other that the topic!) in the original post where the OP was required to write a height function that wasn't recursive, just one that worked!

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Guess I read into that by the phrase "The objective requires that the height be implemented without using an argument." Most recursive functions have an argument, though I suppose you could implement the recursion without an argument. Then again, if you did so (at least the way I'm thinking) you may as well just use loops.

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            @OP: you keep a separate variable countNode to keep track of the number of
            nodes in the tree. You can do similar things where you keep track of the depth
            or height of a node in the nodes themselves. At every insert you can update a
            separate variable treeHeight and return its value at any time. That way you don't
            need any recursion nor arguments to methods and what else ...

            kind regards,

            Jos

            Comment

            • BigDaddyLH
              Recognized Expert Top Contributor
              • Dec 2007
              • 1216

              #7
              [CODE=Java]static Node root;
              static int countNode;
              ...

              public BSTree() {
              root = null;
              }
              ...
              public int size () {
              return countNode;
              }
              [/CODE]
              I just noticed that the root and the countNode (tree size) fields are static. What happens when you have another tree?

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                Originally posted by JosAH
                @OP: you keep a separate variable countNode to keep track of the number of
                nodes in the tree. You can do similar things where you keep track of the depth
                or height of a node in the nodes themselves. At every insert you can update a
                separate variable treeHeight and return its value at any time. That way you don't
                need any recursion nor arguments to methods and what else ...

                kind regards,

                Jos
                I'm from the compute-don't-cache school (of default behaviours). getSize is easy to write recursively (I hope this isn;t giving away the exercise!):
                [CODE=Java]public int getSize () {
                return _size(root);
                }

                private static int _size(Node n) {
                return n==null? 0 : _size(n.left) + _size(n.right);
                }[/CODE]
                Method getHeight is just as easy to write recursively. (I think it's actually much harder to write iteratively, since it is not a simple tail recursion.)

                Maybe it's my background (math) but I usually find recursion easier to write and understand than iteration.

                Comment

                • JosAH
                  Recognized Expert MVP
                  • Mar 2007
                  • 11453

                  #9
                  Originally posted by BigDaddyLH
                  [CODE=Java]static Node root;
                  static int countNode;
                  ...

                  public BSTree() {
                  root = null;
                  }
                  ...
                  public int size () {
                  return countNode;
                  }
                  [/CODE]
                  I just noticed that the root and the countNode (tree size) fields are static. What happens when you have another tree?
                  Those variables are static because trees themselves don't move either; when will
                  people ever learn ...

                  kind regards,

                  Jos ;-)

                  Comment

                  • BigDaddyLH
                    Recognized Expert Top Contributor
                    • Dec 2007
                    • 1216

                    #10
                    Originally posted by JosAH
                    Those variables are static because trees themselves don't move either; when will
                    people ever learn ...

                    kind regards,

                    Jos ;-)
                    I must have been thinking of Ents

                    Comment

                    • JosAH
                      Recognized Expert MVP
                      • Mar 2007
                      • 11453

                      #11
                      Originally posted by BigDaddyLH
                      I must have been thinking of Ents
                      How silly: Ents are totally extinct because they were all male. You can use
                      final static variables for them.

                      kind regards,

                      Jos ;-)
                      Last edited by JosAH; Jan 7 '08, 08:59 PM. Reason: typo

                      Comment

                      • JosAH
                        Recognized Expert MVP
                        • Mar 2007
                        • 11453

                        #12
                        Originally posted by BigDaddyLH
                        Maybe it's my background (math) but I usually find recursion easier to write and understand than iteration.
                        Let's shake hands but the way I read the question was about a non recursive
                        method and I'm also from the lazy bones camp and I think it's much easier
                        to cache stuff manipulated in the insert method than to set up explicit stacks
                        and/or trying to be clever with pointer fiddling etc. Recursive methods are much
                        easier to handle and implement though; I agree.

                        kind regards,

                        Jos

                        Comment

                        • BigDaddyLH
                          Recognized Expert Top Contributor
                          • Dec 2007
                          • 1216

                          #13
                          Originally posted by JosAH
                          Let's shake hands but the way I read the question was ...
                          Hear, hear. Now if we weren't on far sides of the world, I would say that calls for a beer!

                          Comment

                          • JosAH
                            Recognized Expert MVP
                            • Mar 2007
                            • 11453

                            #14
                            Originally posted by BigDaddyLH
                            Hear, hear. Now if we weren't on far sides of the world, I would say that calls for a beer!
                            Me too! No problem though, distance is just a figment of people's imagination and
                            we can solve this the mathematical way: I buy and drink your beer as well ;-)

                            kind regards,

                            Jos

                            Comment

                            • r035198x
                              MVP
                              • Sep 2006
                              • 13225

                              #15
                              Originally posted by JosAH
                              Me too! No problem though, distance is just a figment of people's imagination and
                              we can solve this the mathematical way: I buy and drink your beer as well ;-)

                              kind regards,

                              Jos
                              Public drinking is not allowed in the public forums.
                              Admin.

                              Comment

                              Working...