Question
in java please thanks!

Given a pointer to a binary tree write a routine which will traverse the tree and return the number of nodes in the tree whos
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Node.java

public class Node {
public int data;
public Node left, right;
  
public Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}

BinaryTree.java

public class BinaryTree {
  
public Node root;
private static int count = 0;
  
public BinaryTree()
{
this.root = null;
}

public Node getRoot() {
return root;
}

public void setRoot(Node root) {
this.root = root;
}
  
public int searchBinaryTree(Node temp)
{
if(temp == null)
return 0;
else
{
if(temp.data >= 59 && temp.data <= 112)
return (1 + this.searchBinaryTree(temp.left) + this.searchBinaryTree(temp.right));
}
return this.searchBinaryTree(temp.right) + this.searchBinaryTree(temp.left);
}
  
public int findMaximumNodeValue(Node temp)
{
if(temp == null)
return Integer.MIN_VALUE;
  
int result = temp.data;
int leftResult = findMaximumNodeValue(temp.left);
int rightResult = findMaximumNodeValue(temp.right);
  
if(leftResult > result)
result = leftResult;
if(rightResult > result)
result = rightResult;
return result;
}
  
public int countOfNodesHavingNoSons(Node temp)
{
if(temp == null || (temp.left == null && temp.right == null))
return 0;
  
return 1 + countOfNodesHavingNoSons(temp.left) + countOfNodesHavingNoSons(temp.right);
}
}

BinaryTreeDemo.java (Main class)

public class BinaryTreeDemo {
  
public static void main(String[]args)
{
BinaryTree bt1 = new BinaryTree();
  
bt1.root = new Node(60);
bt1.root.left = new Node(69);
bt1.root.right = new Node(59);
bt1.root.left.left = new Node(71);
bt1.root.left.right = new Node(21);
bt1.root.right.left = new Node(83);
bt1.root.right.right = new Node(100);
  
System.out.println("Number of nodes having value between 59 and 112 is = " + bt1.searchBinaryTree(bt1.root));
System.out.println("Node having maximum value is = " + bt1.findMaximumNodeValue(bt1.root));
  
BinaryTree bt2 = new BinaryTree();
bt2.root = new Node(33);
bt2.root.left = new Node(45);
bt2.root.right = new Node(25);
bt2.root.left.left = new Node(61);
bt2.root.left.right = new Node(17);
  
System.out.println("Count of nodes having no sons is = " + bt2.countOfNodesHavingNoSons(bt2.root) + "\n");
}
}

***************************************************************** SCREENSHOT **********************************************************

DD run Number of nodes having value between 59 and 112 is= 6 Node having maximum value is =100 Count of nodes having no sons

Add a comment
Know the answer?
Add Answer to:
in java please thanks! Given a pointer to a binary tree write a routine which will...
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
  • java please IV. Given a pointer to a binary tree write a routine which will traverse...

    java please IV. Given a pointer to a binary tree write a routine which will traverse the tree and return the 59 and 112 number of nodes in the tree whose information field which is an integer is between In addition return the value of the node with the largest integer and as well return a count or the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and left...

  • Given a pointer to a binary tree write a routine which will traverse the tree and...

    Given a pointer to a binary tree write a routine which will traverse the tree and return number of nodes in the tree who have just a right son or just a left son (do not include nodes have both right and left sons as well as do not include any nodes that do not have any sons at all).

  • in javascrift please Given a pointer to the root of a binary tree write a routine...

    in javascrift please Given a pointer to the root of a binary tree write a routine that will delete every node in the tree that is currently a leaf (has no sons), and when finished tell you how many nodes were deleted as well as how many nodes are left in the tree.

  • CODE IN JAVA** V. Given a pointer to the root of a binary tree and a...

    CODE IN JAVA** V. Given a pointer to the root of a binary tree and a pointer ‘p’ to a given node in the tree and a second pointer ‘q’ to another node in the tree write a routine which will return the total number of nodes in the tree that are on level ‘p’ and ‘q’. If they are on the same level you should multiply the answer by 3.

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

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

  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • 2. A regular binary tree is a binary tree whose internal nodes all have two subtrees...

    2. A regular binary tree is a binary tree whose internal nodes all have two subtrees (left and right). In other words, all their nodes have either zero subtrees (in which case they are leaves) or two subtrees (in which case they are internal nodes). Suppose that you have a boolean function that tells you, for each node of the tree, whether it is a leaf or not (call it: leaf(n), for node n). a) Write a recursive function that...

  • Create a binary search tree in C++, where it will have an add_node(int i) function which...

    Create a binary search tree in C++, where it will have an add_node(int i) function which when called in the main (i.e. add_node(5)) it will add it to the tree. This tree will also place the nodes added in the correct order (if the head node is 40, all the nodes that are added that are less than 40 go to the left of the tree, and all the nodes larger than 40 go to the right) Also add a...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

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
ADVERTISEMENT