Question

What is the RUN TIME COMPLEXITY of the following code: ................................................................................. public void printLevelsRecursively(Node root) {...

What is the RUN TIME COMPLEXITY of the following code:

.................................................................................

public void printLevelsRecursively(Node root) {

for (int i = 1; i <= heightOfTree(root); i++) {
System.out.print("Level " + i + " : ");
printSingleLevelRecursively(root, i);
System.out.print("\n");

}
}
public int heightOfTree(Node root) {
if (root != null)
   return super.max(heightOfTree(root.l), heightOfTree(root.r)) + 1;
return 0;
}

public void printSingleLevelRecursively(Node root, int level) {
if (root == null)
return;
if (level == 1)
System.out.print(root.key + " ");
else if (level > 1) {
printSingleLevelRecursively(root.l, level - 1);
printSingleLevelRecursively(root.r, level - 1);
}
}

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

Height Balanced Tree – (BST -binary search tree)
AVL tree is binary search tree with additional property that difference between height of left sub-tree and right sub-tree of any node can’t be more than 1. For example, BST shown in Figure 2 is not AVL as difference between left sub-tree and right sub-tree of node 3 is 2. However, BST shown Figure      

figure 1 figure 2. figure 3

3 is AVL tree.

  • Searching: For searching element 1, we have to traverse elements (in order 5, 4, 1) = 3 = log2n. Therefore, searching in AVL tree has worst case complexity of O(log2n).
  • Insertion: For inserting element 12, it must be inserted as right child of 9. Therefore, we need to traverse elements (in order 5, 7, 9) to insert 12 which has worst case complexity of O(log2n).
  • Deletion: For deletion of element 9, we have to traverse elements to find 9 (in order 5, 7, 9). Therefore, deletion in binary tree has worst case complexity of O(log2n).
Add a comment
Know the answer?
Add Answer to:
What is the RUN TIME COMPLEXITY of the following code: ................................................................................. public void printLevelsRecursively(Node root) {...
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
  • Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public...

    Using the following implementation of Tree class Node   {   public int iData;              // data item (key)   public double dData;           // data item   public Node leftChild;         // this node's left child   public Node rightChild;        // this node's right child   public void displayNode()      // display ourself      {      System.out.print('{');      System.out.print(iData);      System.out.print(", ");      System.out.print(dData);      System.out.print("} ");      }   }  // end class Node //------------------------------------------------------------------ import java.io.IOException; import java.util.Stack; public class Tree { private Node root; // first node of tree // ------------------------------------------------------------- public Tree() // constructor { root = null; }...

  • Question - modify the code below so that for a node, the value of every node...

    Question - modify the code below so that for a node, the value of every node of its right subtree is less the node, and the value of each node of its left subtree is greater than the node. - create such a binary tree in the Main method, and call the following method:  InOrder(Node theRoot),  PreOrder(Node theRoot),  PostOrder(Node theRoot),  FindMin(),  FindMax(),  Find(int key) using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;...

  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])    {        Scanner s=new Scanner(System.in);        ScoresCircularDoubleLL score=new ScoresCircularDoubleLL();        while(true)        {            System.out.println("1--->Enter a number\n-1--->exit");            System.out.print("Enter your choice:");            int choice=s.nextInt();            if(choice!=-1)            {                System.out.print("Enter the score:");                int number=s.nextInt();                GameEntry entry=new GameEntry(number);   ...

  • Can you take a look at my code that why the maxDepth function is not working?...

    Can you take a look at my code that why the maxDepth function is not working? public class BinaryTree {          class Node{        int key;        Node left,right;               public Node(int item) {            key = item;            left = right = null;        }    }       Node root;       public void BinaryTree(){        root = null;    }           void...

  • Hi, So I have a finished class for the most part aside of the toFile method...

    Hi, So I have a finished class for the most part aside of the toFile method that takes a file absolute path +file name and writes to that file. I'd like to write everything that is in my run method also the toFile method. (They are the last two methods in the class). When I write to the file this is what I get. Instead of the desired That I get to my counsel. I am having trouble writing my...

  • Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time...

    Doubly Linked List The assignment is to modify the below code in any way (like changing the method of a function). Time complexity is omitted. Any methods/functions below could be changed into something different. I was thinking of changing the method of getting size of list and maybe change from numbers to letters for nodes. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null;...

  • 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (...

    10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ] else if (s.charAt (i)'t') System.out.print (s.charAt (i-2)) i+ti else System. out. print (s . charAt (İ) ) ; if (i<2) System.out.print ("y"); System.out.println () 10. What prints when the following code is executed? public static void main (String args) "Cattywampus"; for (int i-s.length )-1 i> 0 i-2) if (s.charAt (i)a') System.out.print(""); ]...

  • Please provide the big oh notation for running time and space complexity for the following functions...

    Please provide the big oh notation for running time and space complexity for the following functions (available, into, out, size, and printAll): int SplayTreeInventory::available(string id){   Node* temp=stmap->findSplay(id); if(temp!=NULL) return temp->value; else return -1;   } void SplayTreeInventory::into(string id, int amount) { Node* temp=stmap->findSplay(id); if(temp==NULL){ stmap->putSplay(id, amount); } else{ temp->key+=amount; } } void SplayTreeInventory::out(string id, int amount) { Node* temp=stmap->findSplay(id); if(temp==NULL){ } else{ if(temp->value<=amount) stmap->erase(id); else temp->value-=amount; } } int SplayTreeInventory::size() { return stmap->size(); } void SplayTreeInventory::printAll() { printAllHelper(this->stmap->root); }

  • Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)...

    Java Programming: The following is my code: public class KWSingleLinkedList<E> {    public void setSize(int size)    {        this.size = size;    }    /** Reference to list head. */    private Node<E> head = null;    /** The number of items in the list */    private int size = 0;       /** Add an item to the front of the list.    @param item The item to be added    */    public void addFirst(E...

  • Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak...

    Q. write the algorithm for each function in this code: void insert(int x, node*&p) { //cheak if the pointer is pointing to null. if (p==NULL) {     p = new node;     p->key=x;     p->left=NULL;     p->right=NULL; } else {     //Cheak if the element to be inserted is smaller than the root.     if (x < p->key)     {       //call the function itself with new parameters.       insert(x,p->left);     }     //cheak if the alement to be inserted...

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