0% found this document useful (0 votes)
2 views

CC104 Handout 5 Tree 2023

The document provides an overview of tree structures, emphasizing their importance in various fields such as computing, biology, and business. It outlines the advantages and disadvantages of using trees, details operations like adding and removing nodes, and describes different types of tree traversals. Additionally, it highlights applications of trees in areas like XML/HTML documents, compiler design, and decision-making processes in machine learning.

Uploaded by

trein7dean
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CC104 Handout 5 Tree 2023

The document provides an overview of tree structures, emphasizing their importance in various fields such as computing, biology, and business. It outlines the advantages and disadvantages of using trees, details operations like adding and removing nodes, and describes different types of tree traversals. Additionally, it highlights applications of trees in areas like XML/HTML documents, compiler design, and decision-making processes in machine learning.

Uploaded by

trein7dean
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

CC104

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

Hierarchical structures are difficult to


comprehend and use. Therefore, they
should be as simple as possible. You
should use every possible option for
reducing hierarchy levels.
Examples of tree structures
• Internet: Yahoo! subject index
• Information management: Dewey Decimal System
• Management: hierarchical organizational structures
• Computing: binary search tree
• Biology: evolutionary tree
• Business: pyramid selling scheme
• Project management: work breakdown structure
Yahoo Subject Index
Dewey Decimal System
Other Common Applications
Both XML (eXtensible Markup Language) and HTML (Hypertext Markup Language) documents
have a tree-like structure. Here are simple representations of XML and HTML documents along
with their tree structures:

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.

• 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.
Abstract idea of a tree
• The size of a binary tree is the number
of nodes in it
• The depth of a node is its distance
from the root
• The depth of a binary tree is the depth
of its deepest node
• The element at the top of the tree is
called the root.
• The elements that are directly under
an element are called its children. The
element directly above something is
called its parent. For example, 1 is a
child of 3 and 3 is the parent of 1.
• Finally, elements with no children are
called leaves.
• Is 4 a leaf? Who is its parent?
Binary Tree
• A tree whose elements have at most 2
children.
• Since each element in a binary tree can
have only 2 children, we typically name
them the left and right child.
Tree Operations
• Add:
• Places an element in the tree.
For example, Add(tree, i) might give:
j<-- root
/ \
f k
/ \ \
a h z
\
i <-- new leaf

• Basic rule: the node to the LEFT contains a value


SMALLER than the node pointing to it, and the node to
the RIGHT contains a LARGER value.
Example

• 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

• Here, i moved up to take its place.


Possible cases to consider
when deleting:
• Deleting a leaf (node with no children):
Deleting a leaf is easy, as we can simply
remove it from the tree.
• Deleting a node with one child: Remove
the node and replace it with its child.
• Deleting a node with two children, choose either
its in-order predecessor node or its in-order
successor node as replacement node.
What is Predecessor and Successor :
• When you do the inorder traversal of a
binary tree, the neighbors of given node
are called Predecessor (the node lies
behind of given node)
and Successor (the node lies ahead of
given node).
17
Tree Operations
• IsMember:
• Reports whether some element(s) is in the
tree.
• For example, IsMember(tree, a) should
give a true value and IsMember(tree, y)
should give a false value.
Example:
Insert: 78, 22, 56, 77, 19, 91, 17, 69, 33, 23
Delete: 19, 69, 78

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:

• 78, 22, 56, 77, 19, 91, 17, 69, 33, 23

• 65, 17, 89, 33, 12, 44, 36, 79, 14, 55

• 25, 11, 39, 20, 8, 45, 12, 44, 28


Expression Trees
Algebraic expressions such as

a / b + (c - d) * e

have an inherent tree-like structure. For


example, the above given is a
representation of the expression in
Equation.
Example
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

You might also like