0% found this document useful (0 votes)
91 views20 pages

Assignment 2

This document discusses different binary search tree (BST) operations including: 1) Creating a BST from an array by adding elements in sorted order to produce a maximum height tree 2) Creating a minimum height BST from a sorted array by choosing the mid-element as the root at each step 3) Deleting elements from a BST by removing the root node at each step It also covers building a max-heap from an unsorted array by heapifying the tree in reverse order, and performing heapsort by repeatedly removing the maximum element and heapifying the tree.

Uploaded by

MEHUL RATHOD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views20 pages

Assignment 2

This document discusses different binary search tree (BST) operations including: 1) Creating a BST from an array by adding elements in sorted order to produce a maximum height tree 2) Creating a minimum height BST from a sorted array by choosing the mid-element as the root at each step 3) Deleting elements from a BST by removing the root node at each step It also covers building a max-heap from an unsorted array by heapifying the tree in reverse order, and performing heapsort by repeatedly removing the maximum element and heapifying the tree.

Uploaded by

MEHUL RATHOD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Krishika Gupta

216088403
ITEC 2620
Prof. Xin Tan
BST
BST maximum
Array a = [1,2,3,4,5,6]

Step 1 - Add 0 to the tree

Step 2 - Add 1

Step 3 - Add 2
Step 4 - Add 3

Step 5 - Add 4
Step 6 - Add 5
Step 7 - Add 6
BST minimum
Array a = [0,1,2,3,4,5,6]

Procedure to follow : -

1) Get the Middle of the array and make it root.


2) Recursively do the same for the left half and right half.
a) Get the middle of left half and make it left child of the root
created in step 1.
b) Get the middle of right half and make it right child of the
root created in step 1.

For creating minimum height BST we will need sorted array

Now find the mid of the array and that will be the root.

Step 1 :​ To find mid of array mid = (start + end)/2

Here ( 0 + Array.length -1 = 6 - 1 ) ) / 2 = (0 + 5) / 2 = round of(2.5) = 3

So 3rd element of array = a[2] = 3 = mid of array

And divide array into 2 parts from the mid

So left will be [0,1,2]

And right will be [4,5,6]

Add 3 as a root first


Step 2 :​ For left side of tree we will consider only array till element 3 = [0,1,2] and will follow
step one which will five us element 1

Add 1 to the tree

Follow Step one till no element in array left


Step 3 :​ For right side of tree we will consider only array from mid to end element = [4,5,6] and
will follow step one which will give us element 5

Add 5 to the tree

Follow Step one till no element in array left


BST Deletion
Take Input from previous BST minimum tree

Input :

Step 1 :​ remove root node 3 and that will make node 4 as root node as it is smallest in the right
hand tree
Step 2 :​ remove root node 4

Step 3 : ​remove root node 5

Step 4 :​ remove root node 6


Step 5 :​ remove root node 1

Step 6 :​ remove root node 2

Step 7 : ​remove root node 0

Empty
Heaps
Building heap using BuildMaxHeap(A)
[73,6,57,88,60,42,83,72,48,85]

Corresponding Binary Tree is:

The task is to build a Max-Heap from the above array.

Total Nodes = 10.


Last Non-leaf node index = (10/2) - 1 = 4.
Therefore, the last non-leaf node = 60.

To build the heap, heapify only the nodes:


[73, 6, 57, 88, 60] in reverse order.
Heapify 60: Swap 60 and 85.

Heapify 88: No need to swap.


Heapify 57: Swap 57 and 83.

Heapify 6: Swap 6 and 88. Then 72 and 6.


Heapify 73: Swap 73 and 88.Then swap 85 and 73.
Heap sort using Heapsort(H)
Input :

Remove root 88 and apply max-heapify


Now remove 85 and apply max-heapify

Now remove 83 and apply max-heapify


Now remove 73 and max-heapify

Now remove 72 and max-heapify


Now remove 60 and max-heapify

Now remove 57 and max-heapify


Now remove 48 and max-heapify

Now remove 42 and max-heapify

Remove 6 and the tree will be empty and heap sort done.

You might also like