CC104 Handout 5 Tree 2023
CC104 Handout 5 Tree 2023
Data Structures
Prayer
As we rejoice in the gift of this new
day,
so may the light of your presence,
O God, set our hearts on fire with love
for you and passion for study. Amen.
Saint John Baptist de La Salle … pray for us
Live Jesus in our hearts…forever
Tree Structure
• One of the most important non-linear
information structures because of its wide
applicability in various fields like business,
sciences and computing.
• In computer science, a tree is a widely used
abstract data type that represents a hierarchical
tree structure with a set of connected nodes.
Each node in the tree can be connected to
many children, but must be connected to
exactly one parent, except for the root node,
which has no parent.
Do Use
• Trees allow an excellent overview of different
objects that are related to one another. This
means many objects are displayed, but each
individual object only has a small amount of
information.
• Trees allow the modeling of graphical
relationships between objects. Illustrations of
this type can help promote orientation and
comprehension of the relationships for users
charged with performing a task.
Do Use
• Trees represent a central access point for
navigation between different objects. The user
can avoid frequent screen changes and having
to remember where specific information is
located.
• Trees allow fast access with direct manipulation
(via drag-and-drop) of objects within the
structure. As a result, trees are particularly well-
suited to activities that require users to re-sort or
move objects.
Do not Use
In some situations, the use of trees can cause
more problems than it solves. For example, you
should not use trees to:
• Model existing hierarchical menu and navigation
structures in the system without prior revision. Simply
copying nested area menus into a tree structure does
not help the user.
• Display complex information in trees, as well as interface
elements like pushbuttons or checkboxes. This is not
recommended due to the complexity of the resulting tree
and the limited space available.
General Rule
XML Document
HTML Document
Other Common Applications
• Compiler Design: Syntax trees are used in compiler design to represent the
structure of programming language statements, aiding in the process of parsing
and generating executable code.
Other Common Applications
• Network Routing: Trees are used in networking to represent the hierarchical
routing of data packets from source to destination, helping in efficient data
transfer. Suppose we have the topology below:
Other Common Applications
• Decision Trees: used in machine learning and data mining for classification and regression
tasks, where the data is split based on various features to make decisions.
Other Common Applications
• Huffman Coding: a popular algorithm for lossless
data compression, where variable-length codes are
assigned to different characters based on their
frequency of occurrence.
• Game Trees: used in game theory and artificial
intelligence for modeling decision-making processes
in games such as chess, tic-tac-toe, and other board
games.
• Database Indexing: Tree data structures such as B-
trees and B+ trees are widely used in database
indexing to provide efficient searching, insertion, and
deletion operations.
Other Common Applications
• Spanning Trees: Spanning trees are used in network design
and optimization to find the minimum subset of edges that
connects all the vertices in a graph without forming any cycles.
• 12, 6, 2, 4, 18
• 2, 18, 12, 4, 6
• 5, 23, 74, 11, 19
Tree Operations
• Remove:
• Removes something from the tree (how the tree is
reorganized after a removal depends on the kind of tree).
For example, Remove(tree, h) might give:
j <-- root
/ \
f k
/ \ \
a i z
Insert: 65, 17, 89, 33, 12, 44, 36, 79, 14, 55
Delete: 33, 79, 36
Binary Search Tree
• The binary search tree provides us with a structure
that allows quicker access to any node in the list.
• In computing, a binary search tree (BST), has the
following properties:
– The left subtree of a node contains only nodes with
keys less than the node's key.
– The right subtree of a node contains only nodes with
keys greater than the node's key.
– Both the left and right subtrees must also be binary
search trees.
Search Operations
• Operations on a binary search tree require comparisons
between nodes. The algorithm of searching is:
– Searching a binary search tree begins by examining the root
node.
– If the tree is null, the value we are searching for does not exist in
the tree. Otherwise, if the value equals the root, the search is
successful.
– If the value is less than the root, search the left subtree.
Similarly, if it is greater than the root, search the right subtree.
– This process is repeated until the value is found or the indicated
subtree is null.
– If the searched value is not found before a null subtree is
reached, then the item must not be present in the tree.
Tree Traversals
Once the binary search tree has been
created, its elements can be retrieved in-
order by traversing the left subtree of the
root node, accessing the node itself, then
traversing the right subtree of the node,
continuing this pattern with each node in
the tree as it is repeatedly accessed.
Types of Tree Traversals
• Preorder Traversal
• Postorder Traversal
• Inorder Traversal
Preorder Traversal
• Preorder traversal gets its name from the
fact that it visits the root first. In the case of
a binary tree, the algorithm becomes:
1. Visit the root first; and then
2. Traverse the left subtree; and then
3. Traverse the right subtree.
Postorder Traversal
• In contrast with preorder traversal, which
visits the root first, postorder traversal
visits the root last. To do a postorder
traversal of a binary tree:
1. Traverse the left subtree; and then
2. Traverse the right subtree; and then
3. Visit the root.
Inorder Traversal
• Inorder traversal only makes sense for
binary trees. Whereas preorder traversal
visits the root first and postorder traversal
visits the root last, inorder traversal visits
the root in between visiting the left and
right subtrees:
1. Traverse the left subtree; and then
2. Visit the root; and then
3. Traverse the right subtree.
Examples:
a / b + (c - d) * e
/ *
a b - e
c d
Example Tree Explained…
• The terminal nodes (leaves) of an expression tree
are the variables or constants in the expression (a,
b, c, d, and e).
• The non-terminal nodes of an expression tree are
the operators ( /, +, - ).
• The parentheses which appear in equation do not
appear in the tree.
• Nevertheless, the tree representation has captured
the intent of the parentheses since the subtraction is
lower in the tree than the multiplication.
Example:
a^2*b–(c/d)
Preorder:
Inorder:
Postorder:
Example:
(2 * ( a – 1 ) + ( 3 * b ) )
Preorder:
Inorder:
Postorder:
The End