Question

Java eclipse question. Given a binary search tree -------------M ---------G ---------N ------D----- H ---B----F How woul...

Java eclipse question.

Given a binary search tree

-------------M

---------G ---------N

------D----- H

---B----F

How would you find the farthest from the root and most right side of the node?

So, in this case, farthest nodes are B and F with height of 4

But the most right side of the node is F

Therefore answer is F.

I have

//Will call helper method.

public T findRightmostLowest(){

int lv = height();//This is the maxium height of the tree(4 in this case).

   return findRightmostLowest(root, 1, lv);

}

private T findRightmostLowest(Node node, int currLv, int maxLv){

}

I appreciate your time.

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

Find farthest nodes:

To find shortest nodes we traverse binary tree with DFS and push all nodes with max depth into vector.
Code:

private void findFarthestNodes(Node node, int depth, int maxDepth, Vector<Node> list) {
  if(node == NULL) {
  return;
}

DFS(node->left, depth+1, maxDepth, list);

DFS(node->right, depth+1, maxDepth, list);

if(depth == maxDepth)
list.add(node);​​​​​

}

Find right most node:

To find right most node we just have to move from node to it's right node while it exists.
Code:

private Node findRightmostNodes(Node node) {
  if(node == NULL) {
  return NULL;
}

while(node->right != NULL)
node = node->right

}


Comment down for any queries

Please give a thumb up

Add a comment
Know the answer?
Add Answer to:
Java eclipse question. Given a binary search tree -------------M ---------G ---------N ------D----- H ---B----F How woul...
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 : 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...

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

  • C++ ONLY This question has 2 parts (a, and b). The partial definition of tree with...

    C++ ONLY This question has 2 parts (a, and b). The partial definition of tree with up to 3-nodes for each node is given below. This is not a binary search tree! class Tree3 { public : Tree3() {} private : class Node { public : int Val ; Node * Left ; Node * Middle ; Node * Right ; }; }; Node * Root { nullptr }; a) A Node is considered balanced if the sum of the...

  • 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. How can I show that any node of a binary search tree of n nodes...

    a. How can I show that any node of a binary search tree of n nodes can be made the root in at most n − 1 rotations? b. using a, how can I show that any binary search tree can be balanced with at most O(n log n) rotations (“balanced” here means that the lengths of any two paths from root to leaf differ by at most 1)?

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

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

  • QUESTION 9 Consider the following binary search tree: If the root node, 50, is deleted, which node will become the new root?   A 15 B 24 C 37 D 62    QUESTION 10 In the...

    QUESTION 9 Consider the following binary search tree: If the root node, 50, is deleted, which node will become the new root?   A 15 B 24 C 37 D 62    QUESTION 10 In the following trees EXCEPT______, the left and right subtrees of any node have heights that differ by at most 1. A complete trees B perfect full trees C balanced binary trees D binary search trees    QUESTION 11 A perfect full binary tree whose height is 5 has...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...

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