0% found this document useful (0 votes)
14 views12 pages

Lect 5 - Sliit

The document discusses trees in computing, specifically focusing on Binary Search Trees (BST) and their operations such as finding and inserting nodes. It outlines tree height calculation and provides methods for traversing trees, including in-order, pre-order, and post-order traversals. Additionally, it includes links to simulation tools and activities for drawing BSTs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Lect 5 - Sliit

The document discusses trees in computing, specifically focusing on Binary Search Trees (BST) and their operations such as finding and inserting nodes. It outlines tree height calculation and provides methods for traversing trees, including in-order, pre-order, and post-order traversals. Additionally, it includes links to simulation tools and activities for drawing BSTs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

COMP 1002 – Data

Structures and
Algorithms
Lecture 05

Umanga Pilapitiya: | COMP 1002: | 2025 1


Trees in Computing ?

• Simulation Links to try


https://www.cs.usfca.edu/~galles/
visualization/BST.html

https://yongdanielliang.github.io/
animation/web/BST.html
Binary Search Tree - BST
Find
Insert

https://futurecoder.io/course/#ide
Activity
Draw the following BST s.

• 40,20,30,60,10,50,70

• 70,60,50,40,30,20,10

Umanga Pilapitiya: | COMP 1002: | 2024 6


Tree Height – How it Works
heightRec(10) – LEVL 1
10 heightRec(5) – LEVEL 2
heightRec(3) -> null, it returns -1. – LEVEL 3
5
15
htSoFar = (max of -1, -1) + 1 = 0
heightRec(8) -> null, it returns -1. – LEVEL 3
3 8 20
htSoFar = (max of -1, -1) + 1 = 0
htSoFar = (max of 0, 0) + 1 = 1 – LEVEL 2

heightRec(15) – LEVEL 2
Slide 69,70,71 heightRec(20) -> null, it returns -1. – LEVEL3
htSoFar = (max of -1, -1) + 1 = 0
htSoFar = (max of 0, -1) + 1 = 1 – LEVEL2
htSoFar = (max of 1, 1) + 1 = 2 – LEVEL1
Traversing – In order
• Recursively Print left subtree
• Print current node
• Recursively Print right sub tree

if CurrentNode != NULL
inOrder(currNode.getLeft())
print (currNode.getKey())
inOrder(currNode.getRight())

Umanga Pilapitiya: | COMP 1002: | 2025 8


Traversing – Pre order
• Print current node
• Recursively Print left subtree
• Recursively Print right sub tree

if CurrentNode != NULL

print (currNode.getKey())
inOrder(currNode.getLeft())
inOrder(currNode.getRight())

Umanga Pilapitiya: | COMP 1002: | 2025 9


Traversing – Post order
• Recursively Print left subtree
• Recursively Print right sub tree
• Print current node

if CurrentNode != NULL
inOrder(currNode.getLeft())
inOrder(currNode.getRight())
print (currNode.getKey())

Umanga Pilapitiya: | COMP 1002: | 2024 10


Tree Traversing
• https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-
postorder/

Write Pre-order, Inorder


and post order traversing
Thank You.. 
Let's meet on next week

Umanga Pilapitiya: | COMP 1002: | 2024 12

You might also like