Results 1 to 4 of 4

Thread: URGENT help needed!

  1. #1
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default URGENT help needed!

    Suppose IntBSTNode is a BSTnode with integer data. Write a new static method to meet the following specifications:

    Public void increase (IntBSTNode root)
    //precondition: root is the root reference of a binary search tree.
    //postcondition: every node of the tree has had its data increased by one.

    Me: 2) Using BSTNode class, write a new static method to meet the follow specifications:

    Public static int manyNodes (BSTNode root)
    //precondition: Root is the root reference of a binary search tree
    //Postcondition: The return value is the nunber of nodes in the tree/ //notes: The empty tree has 0 nodes, and a tree with just a root has 1 node.

    Me: 3) Using BSTNode class, write a new static method to meet the follow specifications:
    Public static int manyNodes (BSTNode root)
    //precondition: Root is the root reference of a binary search tree
    //Postcondition: the return value is the depth of the binary tree.

    Please post the solutions, these are study material for an exam tomorrow and I completely forgot I had these.

    If you can explain that would be great too




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Assuming the node structure is:
    Java Code:
    public class Node<T extends Comparable<T>> {

        Node Left, Right;
        T Value;

        public Node(T Value) {
            this.Value = Value;
        }
    }


    and that you don't mind recursion.. Then..
    Java Code:
    public int count(Node<T> root) {
        if (root == null) {
            return 0;
        }

        return count(root.Left) + count(root.Right) + 1;
    }

    public void increment(Node<Integer> root) {
        if (root == null) {
            return;
        }

        root.Value += 1;

        increment(root.Left);
        increment(root.Right);
    }

    will work just fine.
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Oct 2011
    Location
    Chicago
    Posts
    3,352
    Mentioned
    21 Post(s)
    Quoted
    437 Post(s)

    Default

    Thanks brandon, I solved the other ones as well.




    Anti-Leech Movement Prevent Leeching Spread the word
    Insanity 60 Days (Killer workout)
    XoL Blog (Workouts/RS/Misc)

  4. #4
    Join Date
    Aug 2014
    Posts
    1
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    Okay good.

Thread Information

Users Browsing this Thread

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

Posting Permissions

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