SRL Archive - General

URGENT help needed!


[XoL]

2013-12-11 01:00:25

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 :p

Brandon

2013-12-11 09:55:22

Assuming the node structure is:

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..

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.

[XoL]

2013-12-11 18:36:47

Thanks brandon, I solved the other ones as well.

Oldvendor

2014-08-18 22:11:09

Okay good.