0% found this document useful (0 votes)
4 views9 pages

Heap BST AVL Algorithms

The document outlines various algorithms for data structures including Binary Search Trees (BST), Heaps, AVL Trees, and sorting methods like Heapsort, Merge Sort, and Quick Sort. It also describes graph algorithms such as BFS, DFS, Kruskal’s, Prim’s, Dijkstra, and Topological Sorting, along with the construction of a Huffman Tree. Each algorithm is presented with its respective pseudocode for insertion, deletion, and balancing operations.
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)
4 views9 pages

Heap BST AVL Algorithms

The document outlines various algorithms for data structures including Binary Search Trees (BST), Heaps, AVL Trees, and sorting methods like Heapsort, Merge Sort, and Quick Sort. It also describes graph algorithms such as BFS, DFS, Kruskal’s, Prim’s, Dijkstra, and Topological Sorting, along with the construction of a Huffman Tree. Each algorithm is presented with its respective pseudocode for insertion, deletion, and balancing operations.
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/ 9

1.

BST Insertion
BST-INSERTION(root, key):
if root = NULL:
return NEWNODE(key)

if key < root.key:


root.left ← BST-INSERTION(root.left, key)
else:
root.right ← BST-INSERTION(root.right, key)

return root

2. BST Deletion
BST-DELETION(root, key):
if root = NULL:
return NULL

if key < root.key:


root.left ← BST-DELETION(root.left, key)
else if key > root.key:
root.right ← BST-DELETION(root.right, key)
else:
if root.left = NULL:
return root.right
else if root.right = NULL:
return root.left

temp ← MIN-VALUE(root.right)
root.key ← temp.key
root.right ← BST-DELETION(root.right, temp.key)

return root

MIN-VALUE(node):
while node.left ≠ NULL:
node ← node.left
return node

3. Heaps Insertion (Max Heap)


MAXHEAP-INSERTION(A, n, key):
n ← n + 1
A[n] ← key
i ← n
while i > 1 and A[i // 2] < A[i]:
swap A[i], A[i // 2]
i ← i // 2
4. Heaps Deletion (Max Heap - Remove Root)
MAXHEAP-DELETION(A, n):
if n = 0:
return

A[1] ← A[n]
n ← n - 1
MAX-HEAPIFY(A, 1, n)

5. Heapify (Max Heap)


MAX-HEAPIFY(A, i, n): /* i is the element which is not properly in the
heap */
left ← 2 * i
right ← 2 * i + 1
largest ← i

if left ≤ n and A[left] > A[largest]:


largest ← left

if right ≤ n and A[right] > A[largest]:


largest ← right

if largest ≠ i:
swap A[i], A[largest]
MAX-HEAPIFY(A, largest, n)

6. Heap Generation (Build Max Heap)


It looks like there was a small error due to a missing font family in
the PDF generation step. Let me fix that and regenerate the PDF for
you.

The error happened because some characters like ← and ≠ aren’t


supported by the default encoding in the PDF generator. I’ll replace
those symbols with plain-text equivalents like:

BUILD-MAX-HEAP(A, n):
for i ← ⌊n / 2⌋ downto 1: /* as there are n/2 leaf nodes so they’re
already a heap */
MAX-HEAPIFY(A, i, n)

7. AVL Rotations
RIGHT-ROTATE(y):
x ← y.left
T2 ← x.right

x.right ← y
y.left ← T2
y.height ← 1 + MAX(HEIGHT(y.left), HEIGHT(y.right))
x.height ← 1 + MAX(HEIGHT(x.left), HEIGHT(x.right))
return x

LEFT-ROTATE(x):
y ← x.right
T2 ← y.left
y.left ← x
x.right ← T2

x.height ← 1 + MAX(HEIGHT(x.left), HEIGHT(x.right))


y.height ← 1 + MAX(HEIGHT(y.left), HEIGHT(y.right))

return y

LEFT-RIGHT-ROTATE(z):
z.left ← LEFT-ROTATE(z.left)
return RIGHT-ROTATE(z)

RIGHT-LEFT-ROTATE(z):
z.right ← RIGHT-ROTATE(z.right)
return LEFT-ROTATE(z)

8. AVL Insertion
AVL-INSERTION(root, key):
if root = NULL:
return NEWNODE(key)

if key < root.key:


root.left ← AVL-INSERTION(root.left, key)
else if key > root.key:
root.right ← AVL-INSERTION(root.right, key)
else:
return root

root.height ← 1 + MAX(HEIGHT(root.left), HEIGHT(root.right))


balance ← HEIGHT(root.left) - HEIGHT(root.right)

if balance > 1 and key < root.left.key:


return RIGHT-ROTATE(root)
if balance < -1 and key > root.right.key:
return LEFT-ROTATE(root)
if balance > 1 and key > root.left.key:
root.left ← LEFT-ROTATE(root.left)
return RIGHT-ROTATE(root)
if balance < -1 and key < root.right.key:
root.right ← RIGHT-ROTATE(root.right)
return LEFT-ROTATE(root)

return root

9. AVL Deletion
AVL-DELETION(root, key):
if root = NULL:
return NULL

if key < root.key:


root.left ← AVL-DELETION(root.left, key)
else if key > root.key:
root.right ← AVL-DELETION(root.right, key)
else:
if root.left = NULL:
return root.right
else if root.right = NULL:
return root.left

temp ← MIN-VALUE(root.right)
root.key ← temp.key
root.right ← AVL-DELETION(root.right, temp.key)

root.height ← 1 + MAX(HEIGHT(root.left), HEIGHT(root.right))


balance ← HEIGHT(root.left) - HEIGHT(root.right)

if balance > 1 and HEIGHT(root.left.left) ≥


HEIGHT(root.left.right):
return RIGHT-ROTATE(root)
if balance > 1 and HEIGHT(root.left.left) <
HEIGHT(root.left.right):
root.left ← LEFT-ROTATE(root.left)
return RIGHT-ROTATE(root)
if balance < -1 and HEIGHT(root.right.right) ≥
HEIGHT(root.right.left):
return LEFT-ROTATE(root)
if balance < -1 and HEIGHT(root.right.right) <
HEIGHT(root.right.left):
root.right ← RIGHT-ROTATE(root.right)
return LEFT-ROTATE(root)

return root
10. Heapsort
HEAPSORT(A, n):
BUILD-MAX-HEAP(A, n)
for i ← n downto 2:
swap A[1], A[i]
n ← n - 1
MAX-HEAPIFY(A, 1, n)

11. Merge Sort


MERGE-SORT(A, left, right):
if left < right:
mid = (left + right) // 2
MERGE-SORT(A, left, mid)
MERGE-SORT(A, mid + 1, right)
MERGE(A, left, mid, right)

MERGE(A, left, mid, right):


n1 = mid - left + 1
n2 = right – mid
L = A[left : mid + 1]
R = A[mid + 1 : right + 1]
i = j = 0
k = left

while i < n1 and j < n2:


if L[i] <= R[j]:
A[k] = L[i]
i += 1
else:
A[k] = R[j]
j += 1
k += 1

while i < n1:


A[k] = L[i]
i += 1
k += 1

while j < n2:


A[k] = R[j]
j += 1
k += 1
12. Quick Sort
QUICK-SORT(A, low, high):
if low < high:
pi = PARTITION(A, low, high)
QUICK-SORT(A, low, pi – 1)
QUICK-SORT(A, pi + 1, high)

PARTITION(A, low, high):


pivot = A[high]
i = low – 1

for j = low to high – 1:


if A[j] <= pivot:
i += 1
swap A[i], A[j]

swap A[i + 1], A[high]


return i + 1

10. BFS
BFS(G, start):
create a queue Q
mark start as visited and enqueue it into Q
while Q is not empty:
v = Q.dequeue()
for all neighbors u of v:
if u is not visited:
mark u as visited
Q.enqueue(u)

13. DFS
DFS(G, v):
mark v as visited
for each neighbor u of v:
if u is not visited:
DFS(G, u)
14. Kruskal’s Algorithm
KRUSKAL(G):
A = empty set
for each vertex v in G:
MAKE-SET(v)

sort all edges in G by increasing weight

for each edge (u, v) in sorted edges:


if FIND-SET(u) != FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)

return A

15. Prim’s Algorithm


PRIM(G, start):
for each vertex v in G:
key[v] = ∞
parent[v] = NULL

key[start] = 0
Q = all vertices in G

while Q is not empty:


u = vertex in Q with minimum key[u]
remove u from Q
for each neighbor v of u:
if v in Q and weight(u, v) < key[v]:
parent[v] = u
key[v] = weight(u, v)
16. Dijkstra Algorithm
DIJKSTRA(G, source):

for each vertex v in G:


dist[v] = ∞
prev[v] = NULL

dist[source] = 0
Q = all vertices in G

while Q is not empty:


u = vertex in Q with minimum dist[u]
remove u from Q

for each neighbor v of u:


alt = dist[u] + weight(u, v)
if alt < dist[v]:
dist[v] = alt
prev[v] = u

return dist, prev

17. Topological Sorting


TOPOLOGICAL-SORT(G):

visited = empty set


stack = empty stack

for each vertex v in G:


if v not in visited:
DFS-VISIT(v, visited, stack)

return stack in reverse order

DFS-VISIT(v, visited, stack):


mark v as visited

for each neighbor u of v:


if u not in visited:
DFS-VISIT(u, visited, stack)

stack.push(v)
18. Huffmann Tree
HUFFMAN(C):
create a priority queue Q with characters and their frequencies

while Q.size > 1:


x = Q.extractMin()
y = Q.extractMin()
z = NEWNODE()
z.left = x
z.right = y
z.freq = x.freq + y.freq
Q.insert(z)

return Q.extractMin() (root of Huffman Tree)

You might also like