Question

Java : This function is to search through a binary tree left and right and return...

Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception.

public class MyIntSET {

   private Node root;

   private static class Node {

       public final int key;

       public Node left, right;

       public Node(int key) { this.key = key; }

   }

   public int sizeAboveDepth(int k) {

       return abovehelp(root, k, 0);

   }

  

   public int abovehelp(Node x, int k, int count) {

       int szl = abovehelp(x.left, k, count);

       int szr = abovehelp(x.right, k, count);

       if (x != null && count > k) {

           count ++;

           return 1 + szl + szr;

       }

       count ++;

       return 0 + szl + szr;

   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Node

{

int data;

Node left, right;

Node(int item)

{

data = item;

left = right = null;

}

}

class BinaryTree

{

Node root;

void countNodes(Node node, int k,int &c)

{

if (node == null)

return;

if (k == 0)

{

return;

}

else

{

countNodes(node.left, k - 1,c++);

countNodes(node.right, k - 1,c++);

}

}

public static void main(String args[]) {

BinaryTree tree = new BinaryTree();

/* Constructed binary tree is

1

/ \

2 3

/ \ /

4 5 8

*/

tree.root = new Node(1);

tree.root.left = new Node(2);

tree.root.right = new Node(3);

tree.root.left.left = new Node(4);

tree.root.left.right = new Node(5);

tree.root.right.left = new Node(8);

int c=0

tree.countNodes(tree.root, 2,c);

System.out.print(c);

}

}

Add a comment
Know the answer?
Add Answer to:
Java : This function is to search through a binary tree left and right and return...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Writing traversal methods You will create two files in the csc403 package: one called A3SimplerBST.java and...

    Writing traversal methods You will create two files in the csc403 package: one called A3SimplerBST.java and the other TestA3.java. Details on these programs appears below. Copy the file SimplerBST.java from the week 1 examples package and rename the file and class A3SimplerBST. Add two public methods: one named twoChildrenCount that takes no parameters and that, when called, traverses every node of the tree to count how many nodes have two children. one named sameValueCount that takes a paremeter of generic...

  • Removing Nodes from a Binary Tree in Java This section requires you to complete the following...

    Removing Nodes from a Binary Tree in Java This section requires you to complete the following method within BinaryTree.java: public void remove(K key) { } The remove method should remove the key from the binary tree and modify the tree accordingly to maintain the Binary Search Tree Principle. If the key does not exist in the binary tree, no nodes should be removed. In case there is a non-null left child, it should take the place of the removed node....

  • JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with...

    JAVA QUESTION: *******THE QUESTION:******** /** numberOfNodesAtDepth    *    * Returns the number of nodes with depth == d    * Precondition: none    *    * param: d the depth to search for    *    * hint: use a recursive helper function    *        * ToDo 4    */    public int numNodesAtDepth(int d) {        return -1;    } **********USEFUL CODE FROM THE ASSIGNMENT:*********** public class simpleBST<Key extends Comparable<Key>, Value> {    private Node root;            ...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

    In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...

  • Java binary search tree Add the following print method to the binary search tree class created...

    Java binary search tree Add the following print method to the binary search tree class created in class (on D2L). This method should print all the nodes in the tree in level order (root first, then all children of root, then all children of those). Ensure your method runs in O(N), include comments to show how it conforms to this rule. Method header: public void printInLevelOrder() public class BinarySearchTree<E extends Comparable<? super E>> { private Node root; public BinarySearchTree() {...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • C++ Binary Search Tree question. I heed help with the level 2 question please, as level...

    C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...

  • A binary tree is constructed of nodes that are instances of the following class: public class...

    A binary tree is constructed of nodes that are instances of the following class: public class Node public int val public Node left public Node right) Consider the following method public static Node mystery Node root) rootghtanul return root else return mystery root ) You consult Professor Kennedy and hegves an opinion about what the method does when passed a reference to the root node of a binary tree. Assuming he is correct what does the mystery function do? it...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT