Question: 3. Binary search trees (a) Given a binary tree, give efficient algorithms to determine (i) the to……

3. Binary search trees (a) Given a binary tree, give efficient algorithms to determine (i) the total number of nodes; and (ii) its height. Analyze the two algorithms (b) Given a red-black tree, give efficient algorithms to determine (i) the total number of nodes) its height; and (ii) its black height. Analyze the three algorithms. Are there any differences to part (a) one can observe?3. Binary search trees (a) Given a binary tree, give efficient algorithms to determine (i) the total number of nodes; and (ii) its height. Analyze the two algorithms (b) Given a red-black tree, give efficient algorithms to determine (i) the total number of nodes) its height; and (ii) its black height. Analyze the three algorithms. Are there any differences to part (a) one can observe?

Expert Answer

answers

(a) (i) Ans:- The idea is to use level-order traversal to solve this problem efficiently.

Don't use plagiarized sources. Get Your Custom Essay on
Question: 3. Binary search trees (a) Given a binary tree, give efficient algorithms to determine (i) the to……
Get an essay WRITTEN FOR YOU, Plagiarism free, and by an EXPERT!
Order Essay
1) Create an empty Queue Node and push root node to Queue.
2) Do following while nodeQeue is not empty.
   a) Pop an item from Queue and process it.
      a.1) If it is full node then increment count++.
   b) Push left child of popped item to Queue, if available.
   c) Push right child of popped item to Queue, if available.

Implementation in Java:

// Java program to count

// full nodes in a Binary Tree

// using Iterative approach

import java.util.Queue;

import java.util.LinkedList;

 

// Class to represent Tree node

class Node

{

int data;

Node left, right;

 

public Node(int item)

{

data = item;

left = null;

right = null;

}

}

 

// Class to count full nodes of Tree

class BinaryTree

{

 

Node root;

 

/* Function to get the count of full Nodes in

a binary tree*/

int getfullCount()

{

// If tree is empty

if (root==null)

return 0;

 

// Initialize empty queue.

Queue<Node> queue = new LinkedList<Node>();

// Do level order traversal starting from root

queue.add(root);

 

int count=0; // Initialize count of full nodes

while (!queue.isEmpty())

{

 

Node temp = queue.poll();

if (temp.left!=null && temp.right!=null)

count++;

 

// Enqueue left child

if (temp.left != null)

{

queue.add(temp.left);

}

 

// Enqueue right child

if (temp.right != null)

{

queue.add(temp.right);

}

}

return count;

}

 

public static void main(String args[])

{

BinaryTree tree_level = new BinaryTree();

tree_level.root = new Node(2);

tree_level.root.left = new Node(7);

tree_level.root.right = new Node(5);

tree_level.root.left.right = new Node(6);

tree_level.root.left.right.left = new Node(1);

tree_level.root.left.right.right = new Node(11);

tree_level.root.right.right = new Node(9);

tree_level.root.right.right.left = new Node(4);

 

System.out.println(tree_level.getfullCount());

 

}

}

Time Complexity: O(n)

Auxiliary Space: O(n) where n is number of nodes in given binary tree

 

(a) (ii) Ans:-  Recursively calculate height of left and right subtrees of a node and assign height to the node as max of the heights of two children plus 1.

Algorithm :

  

maxDepth()
1. If tree is empty then return 0
2. Else
     (a) Get the max depth of left subtree recursively  i.e., 
          call maxDepth( tree->left-subtree)
     (a) Get the max depth of right subtree recursively  i.e., 
          call maxDepth( tree->right-subtree)
     (c) Get the max of max depths of left and right 
          subtrees and add 1 to it for the current node.
         max_depth = max(max dept of left subtree,  
                             max depth of right subtree) 
                             + 1
     (d) Return max_depth

Time Complexity: O(n)

See the below diagram for more clarity about the execution of the recursive function maxDepth() for above example tree.

maxDepth('1') = max(maxDepth('2'), maxDepth('3')) + 1
                               = 2 + 1
                                  /    
                                /         
                              /             
                            /                 
                          /                     
               maxDepth('2') = 1                maxDepth('3') = 1
= max(maxDepth('4'), maxDepth('5')) + 1
= 1 + 1   = 2         
                   /    
                 /        
               /            
             /                
           /                    
 maxDepth('4') = 1     maxDepth('5') = 1

Implementation in Java:

// Java program to find height of tree

 

// A binary tree node

class Node

{

int data;

Node left, right;

 

Node(int item)

{

data = item;

left = right = null;

}

}

 

class BinaryTree

{

Node root;

 

/* Compute the “maxDepth” of a tree — the number of

nodes along the longest path from the root node

down to the farthest leaf node.*/

int maxDepth(Node node)

{

if (node == null)

return 0;

else

{

/* compute the depth of each subtree */

int lDepth = maxDepth(node.left);

int rDepth = maxDepth(node.right);

 

/* use the larger one */

if (lDepth > rDepth)

return (lDepth + 1);

else

return (rDepth + 1);

}

}

 

/* Driver program to test above functions */

public static void main(String[] args)

{

BinaryTree tree = new BinaryTree();

 

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);

 

System.out.println(“Height of tree is : ” +

tree.maxDepth(tree.root));

}

}

Analyze:-   

The height of the binary tree is the longest path from root node to any leaf node in the tree. For example, the height of binary tree is 2 as longest path from root node to node 2 is 2. Also, the height of binary tree is 4.

Binary Tree –
In a binary tree, a node can have maximum two children.

Calculating minimum and maximum height from number of nodes –
If there are n nodes in binary tree, maximum height of the binary tree is n-1 and minimum height isfloor(log2n).

For example, left skewed binary tree with 5 nodes has height 5-1 = 4 and binary tree with 5 nodes has height floor(log25) = 2.

Calculating minimum and maximum number of nodes from height –
If binary tree has height h, minimum number of nodes is n+1 (in case of left skewed and right skewed binary tree).

Binary Search Tree –
In a binary search tree, left child of a node has value less than the parent and right child has value greater than parent.

Calculating minimum and maximum height from the number of nodes –
If there are n nodes in a binary search tree, maximum height of the binary search tree is n-1 and minimum height is floor(log2n).

Top Grade Homework
Order NOW For a 10% Discount!
Pages (550 words)
Approximate price: -

Why Work with Us

Top Quality and Well-Researched Papers

We always make sure that writers follow all your instructions precisely. You can choose your academic level: high school, college/university or professional, and we will assign a writer who has a respective degree.

Professional and Experienced Academic Writers

We have a team of professional writers with experience in academic and business writing. Many are native speakers and able to perform any task for which you need help.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents