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

BST Operations Pseudocode

Uploaded by

green21kc
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 views5 pages

BST Operations Pseudocode

Uploaded by

green21kc
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/ 5

Binary Search Tree (BST) Operations -

Pseudocode
7.3.1 Insert - Iterative & Recursive

Iterative Insert

INSERT(root, value)
node = root
parent = null
WHILE node is not null
parent = node
IF value < node.value THEN
node = node.left
ELSE
node = node.right
END WHILE

IF parent is null THEN


root = CREATE_NODE(value) // Tree was empty
ELSE IF value < parent.value THEN
parent.left = CREATE_NODE(value)
ELSE
parent.right = CREATE_NODE(value)
END IF

Recursive Insert

INSERT(root, value)
IF root is null THEN
RETURN CREATE_NODE(value)
ELSE IF value < root.value THEN
root.left = INSERT(root.left, value)
ELSE
root.right = INSERT(root.right, value)
END IF
RETURN root
7.3.2 Search - Iterative & Recursive

Iterative Search

SEARCH(root, target)
node = root
WHILE node is not null
IF node.value == target THEN
RETURN true
ELSE IF target < node.value THEN
node = node.left
ELSE
node = node.right
END WHILE
RETURN false

Recursive Search

SEARCH(root, target)
IF root is null THEN
RETURN false
ELSE IF root.value == target THEN
RETURN true
ELSE IF target < root.value THEN
RETURN SEARCH(root.left, target)
ELSE
RETURN SEARCH(root.right, target)

7.3.3 Find - max(), min(), size(), height(), balanceFactor() - Iterative &


Recursive

Find max()

FIND_MAX(root)
node = root
WHILE node.right is not null
node = node.right
RETURN node.value
Find min()

FIND_MIN(root)
node = root
WHILE node.left is not null
node = node.left
RETURN node.value

Find size() (Recursive)

SIZE(root)
IF root is null THEN
RETURN 0
RETURN 1 + SIZE(root.left) + SIZE(root.right)

Find height() (Recursive)

HEIGHT(root)
IF root is null THEN
RETURN -1
RETURN 1 + MAX(HEIGHT(root.left), HEIGHT(root.right))

balanceFactor()

BALANCE_FACTOR(node)
leftHeight = HEIGHT(node.left)
rightHeight = HEIGHT(node.right)
RETURN leftHeight - rightHeight
7.3.4 Traverse - BFS (Level-Order)

LEVEL_ORDER_TRAVERSAL(root)
IF root is null THEN
RETURN
queue = new Queue
queue.enqueue(root)
WHILE queue is not empty
node = queue.dequeue()
PRINT node.value
IF node.left is not null THEN
queue.enqueue(node.left)
IF node.right is not null THEN
queue.enqueue(node.right)

7.3.5 Traverse - DFS (Pre/In/Post-Order)

Pre-order (Root-Left-Right)

PRE_ORDER(root)
IF root is null THEN
RETURN
PRINT root.value
PRE_ORDER(root.left)
PRE_ORDER(root.right)

In-order (Left-Root-Right)

IN_ORDER(root)
IF root is null THEN
RETURN
IN_ORDER(root.left)
PRINT root.value
IN_ORDER(root.right)
Post-order (Left-Right-Root)

POST_ORDER(root)
IF root is null THEN
RETURN
POST_ORDER(root.left)
POST_ORDER(root.right)
PRINT root.value

7.3.6 Delete (No-Child, One Child, Two Children)

DELETE(root, value)
IF root is null THEN
RETURN null

IF value < root.value THEN


root.left = DELETE(root.left, value)
ELSE IF value > root.value THEN
root.right = DELETE(root.right, value)
ELSE
// Node to be deleted found
IF root.left is null AND root.right is null THEN
RETURN null // No child case
ELSE IF root.left is null THEN
RETURN root.right // One child case (right)
ELSE IF root.right is null THEN
RETURN root.left // One child case (left)
ELSE
// Two children case
successor = FIND_MIN(root.right) //Find in-order successor
root.value = successor.value
root.right = DELETE(root.right, successor.value)
RETURN root

You might also like