How do you count number of branches, in this case branches with even integers. Here's what I have so far. It seems to work for a couple of the cases.
public int evenBranches() {
return evenBranches(overallRoot);
}
private int evenBranches(IntTreeNode root) {
if (root == null) {
return 0;
}
int val = 0;
if (root.left != null) {
val += evenBranches(root.left);
} else if (root.right != null) {
val += evenBranches(root.right);
}
if (root.data % 2 == 0) {
return val + 1;
} else {
return val;
}
}
You can modify the evenBranches() method as below: I think It will cover all edge cases, If any testcase is left, let me know, I will fix it.
public int evenBranches() {
return evenBranches(overallRoot, 0);
}
private int evenBranches(IntTreeNode root, int count) {
if(root == null || (root.left == null && root.right == null)) {
return count;
}
if(root.data % 2 == 0) {
count++;
}
count += evenBranches(root.left, count);
count += evenBranches(root.right, count);
return count;
}
You may need to remove the else condition when checking the occurrences in right branch. Otherwise it will check only one side. eg:
private int evenBranches(IntTreeNode root) {
if (root == null) {
return 0;
}
int val = 0;
if (root.left != null) {
val += evenBranches(root.left);
}
if (root.right != null) {
val += evenBranches(root.right);
}
if (root.data % 2 == 0) {
return val + 1;
} else {
return val;
}
}
You can very well achieve the desired results by using a global variable, and applying BFS (breadth first search) on your tree, in this manner:
int evencount = 0; // global-var.
public int evenBranches() {
evenBranches(overallRoot);
return evencount;
}
private void evenBranches(IntTreeNode root) {
if(!root) return;
if( (root.left || root.right) && (root.data % 2 == 0)){
evencount++;
}
evenBranches(root.left);
evenBranches(root.right);
}
Related
I am trying to implement a getHeight method that has a o(1) time complexity. To do this I am aiming to assign every node their own height and to return the height of the root with the getHeight method. To do this I need to update the height of the nodes everytime I add or remove.
Thus, I chose to do this:
private void updateHeight(Node node)
{
if (node == null)
{
return;
}
updateHeight(node.left);
updateHeight(node.right);
this.helpHeight(node);
}
private void helpHeight(Node node)
{
if (this.isLeaf(node))
{
node.height = 0;
}
else if (node.left == null)
{
node.height = node.right.height + 1;
}
else if (node.right == null)
{
node.height = node.left.height + 1;
}
else
{
node.height = 1 + max(node.left.height, node.right.height);
}
}
private int max(int height, int height2) {
if (height > height2)
{
return height;
}
else
{
return height2;
}
}
public int height()
{
return root.height + 1;
}
Then I will call the updateHeight(Node node) method with the root as the parameter. However, it's not working when I test it this way:
public static void main(String[] arg)
{
BST bst = new BST();
bst.add("a");
bst.add("b");
bst.add("c");
System.out.println(bst.height());
bst.remove("c");
System.out.println(bst.height());
}
It's returning 2, then 2.
Just in case, here is my add method:
public boolean add(E e) throws NullPointerException
{
if (e == null)
{
throw new NullPointerException("Error: Cannot add a null object to the tree.");
}
if (root == null)
{
root = new Node(e);
return true;
}
Node current = root;
while (current != null ) {
if (current.data.compareTo(e) > 0) {
//add in the left subtree
if (current.left == null ) {
current.left = new Node (e);
size++;
this.updateHeight(root);
return true;
}
else {
current = current.left;
}
}
else if (current.data.compareTo(e) < 0) {
//add in the right subtree
if (current.right == null ) {
size++;
this.updateHeight(root);
current.right = new Node(e);
return true;
}
else {
current = current.right;
}
}
else { //duplicate
return false;
}
}
//should never get to this line
return false;
}
I want to count the number of failed students who got a grade under 60 recursively using a binary search tree in java. I don't know if my method Is correct.
find the helper method to be used in the public wrapper method after
private int NumberfailStudents(int numoffailed, BNode current) {
// BaseCase
if (current == null) {
return 0 + numofpassed;
} else {
if (current.data.getGrades() < 60) {
return ++numoffailed;
}
if (current.left != null) {
return NumberfailStudents(numoffailed, current.left);
}
if (current.right != null) {
return NumberfailStudents(numoffailed, current.right);
}
}
return numoffailed;
}
If the if-condition that checking the current is null should return numoffailed not numofpassed
I have updated the NumberFailStudents to match the naming convention
private int NumberFailStudents(int numOfFailed, BNode current) {
if (current == null)
return numOfFailed;
if (current.data.getGrades() < 60)
++numOfFailed;
if (current.left != null)
numOfFailed = NumberFailStudents(numOfFailed, current.left);
if (current.right != null)
numOfFailed = NumberFailStudents(numOfFailed, current.right);
return numOfFailed;
}
This is the most common implementation for binary tree height I have found on internet
public int height(BinaryNode t) {
if (t == null) {
return 0;
} else {
return 1 + Math.max(height(t.left), height(t.right));
}
}
But shouldn't it modified like this since it is adding an unnecessary 1 at leaf nodes
public int height(BinaryNode t) {
if (t == null) {
return 0;
} else if (t.left == null && t.right == null) {
return 0;
} else {
return 1 + Math.max(height(t.left), height(t.right));
}
}
Basically I don't know why my integer n doesn't get higher.
Thanks for every answer!
public int n;
public int firstIndexOf(T val) {
if (val == this.getValue()) {
return n;
} else {
if (val != this.getValue() && this.next != null) {
n++;
return this.next.firstIndexOf(val);
} else {
return -1;
}
}
}
The increment (n++) was in the wrong condition check:
private int n = 0;
public int firstIndexOf(int val) {
if ( val == this.getValue() ) {
return n+1; //This is were the increment should happen
}
else {
if ( this.next != null ) {
return this.next.firstIndexOf(val);
} else {
return -1;
}
}
}
Also if you want to count all the elements in your linked list which have the same value as val, you can use this:
private int n = 0;
public int firstIndexOf(int val) {
if ( val == this.getValue() ) {
if ( this.next != null ) {
return 1 + this.next.firstIndexOf(val);
}
else {
return 1;
}
} else {
if ( this.next != null ) {
return this.next.firstIndexOf(val);
} else {
return 0;
}
}
}
public void insert(int v) {
Node t = root;
int compareResult;
compareResult = 0;
if ((t.getData()).equals(v)) return;
if (t == null) {
Node n = new Node<>(v, t, null, null);
} else
while (t != null) {
compareResult = v.compareTo(t.getData());
if (compareResult > 0) {
if (t.getRight() != null) {
t = t.getRight();
} else {
Node n = t.getRight();
break;
}
}
else {
compareResult = v.compareTo(t.getData());
if (compareResult < 0) {
if (t.getLeft() != null) {
t = t.getLeft();
} else {
Node n = t.getLeft();
break;
}
}
}
}
v is a primitive int. You can't call methods on primitive types in Java.
You can however replace v.compareTo(t.getData()) with Integer.compare(v, t.getData())