Question

The code should be in java. Question: In an infinite binary tree where every node has...

The code should be in java.

Question: In an infinite binary tree where every node has two children, the nodes are labelled in row order. In each row, the labeling is left to right.

CPT-287_-_Hybrid_-_Lecture_Assignment_6_-_Picture_2_-_PNG.png

Given the label of a node in this tree, please write a method to return the labels in the path from the root of the tree to the node with that label. Your algorithm should have time complexity of LaTeX: \color{blue}{O(\log{n})} O ( log ⁡ n ).

Example
Method Argument 8
Expected Method Return Value [0, 1, 3, 8]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts regarding this question, please comment in the comment section, I am sure to help. If you like my answer. please up vote it, It will encourage me to answer more.

If you will draw the binary tree, you will see that the parent of a node labelled as "l " is (l-1)/2. For example, prent of node of label 8 is (8-1 )/2 =3, parent of 3 is (3-1)/2 = 1 and parent of 1 is (1-1)/2 = 0. So, just dividing the label minus one by 2 will give the parent node and we will reach the root and hence, we will get the path from root to that label .

Code for the above program is:-

import java.util.*;
public class TreeLabel
{

public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the label:- ");
int label = sc.nextInt();

ArrayList<Integer> labelList = new ArrayList<Integer>();
int x =label;
while(x!=0)
{
labelList.add(x);
x=(x-1)/2;
}
labelList.add(0); //"0" is always the root node.
Collections.reverse(labelList);
System.out.println(" Labels in the path from the root of the tree to the node with the label "+label+":- ");
System.out.println(labelList);
}
}

Code Snippet:-

import java.util.*; public class TreeLabel { public static void main(String args[]) Scanner sc = new Scanner (System.in); Sys

Output Snippet:-

C:\Users\Dell\Desktop>javac TreeLabel.java C:\Users\Dell\Desktop>java TreeLabel Enter the label:- Labels in the path from the

Following in above given figure is the output for the above code.

Add a comment
Know the answer?
Add Answer to:
The code should be in java. Question: In an infinite binary tree where every node has...
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
  • A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ

    Data structures C++1- A balanced binary tree is a binary tree structure in which the left and right subtrees of every node differ in height by no more than 1 Out of the following choices, which is the minimum set of nodes, if removed, will make the BST balanced?2- Which of the following is true for Red-Black Trees ? Select all choices that apply! Select one or more: a. For each node in the tree, all paths from that node to any leaf nodes contain...

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

  • In a binary tree, the balance ratio of node v, bal(v), is the number of nodes...

    In a binary tree, the balance ratio of node v, bal(v), is the number of nodes in the left subtree of node v divided by the sum of the number of nodes in the right and left subtrees of node v. bal(a leaf node) = ½. A tree T is said to be ε-balanced if, for all nodes v in T, ½ - ε < bal(v) < ½ + ε. Design an efficient recursive algorithm that determines whether a binary...

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

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

  • You are given a binary tree of the form: Each node in the tree has a...

    You are given a binary tree of the form: Each node in the tree has a left child and a right child. Each of the children will be extended as a linked list. Every node has the following attributes: key, left node, right node, and next node. The next node allows a node, that is a part of the tree, to be extended as a linked list. The diamonds represent the next nodes, which are part of the linked list...

  • Which of the following is true for a Binary Tree? Question 7 options: A binary tree...

    Which of the following is true for a Binary Tree? Question 7 options: A binary tree is a tree, since no node has more than 2 children, they are known as the left child and the right child The root node is the node without a parent A leaf node is a node without child nodes All of the above

  • Recall that in a binary search tree, at every node, all elements to the left of...

    Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...

  • Let T be a proper binary tree. Given a node v ∈ T, the imbalance of...

    Let T be a proper binary tree. Given a node v ∈ T, the imbalance of v, denoted imbalance(v), is defined as the difference, in absolute value, between the number of leaves of the left subtree of v and the number of leaves of the right subtree of v. (If v is a leaf, then imbalance(v) is defined to be 0.) Define imbalance(T) = maxv∈T imbalance(v). (a) Provide an upper bound to the imbalance of a proper binary tree with...

  • Consider a standard binary tree with n nodes, where every node has a pointer to two...

    Consider a standard binary tree with n nodes, where every node has a pointer to two children, either of which may be null. In this tree, are there more null child pointers, or non-null child pointers? Prove your answer. Remember that n could be any integer greater than zero, so we're not just talking about one particular tree for some fixed n, but ANY tree.

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