0% found this document useful (0 votes)
11 views35 pages

DS Trees

Uploaded by

Sailesh Sailesh
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)
11 views35 pages

DS Trees

Uploaded by

Sailesh Sailesh
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/ 35

Course Title:

Discrete Structure
Tribhuvan University (TU)
B.Sc. CSIT 2nd semester

PREPARED BY
UJJWAL RIJAL
[email protected]

Prepared by Ujjwal Rijal


1

Unit – 6
Trees

PREPARED BY
UJJWAL RIJAL
[email protected]

Prepared by Ujjwal Rijal


Introduction to Trees 2
o A tree (T) is a non-linear data structure and is generally defined as
a non-empty finite set of elements which consists of set of nodes
called vertices and set of edges which links vertices such that:
 T contains a distinguished node called a root of the tree.
 The remaining elements of the tree form an ordered
collection of zero or more disjoint subsets called sub-trees.

o Here, A is the root node and it does not have parent.


o A is the parent of B, C, and D nodes. Also, we can say that B, C,
and D nodes are the children of node A, and so on.
Prepared by Ujjwal Rijal
Characteristics of Trees 3
 Some of the characteristics of trees are as
follows:
o The top item in the hierarchy of a tree is referred as the
root of the tree.
o The remaining data elements are partitioned into a
number of mutually exclusive subsets and they itself a
tree and are known as the sub-tree.
o Unlike natural trees, trees in data structure always grow
in length towards the bottom.
o It is a non-linear data structure.
o It combines advantages of an ordered array.
o Searching is as fast as in ordered array.
o Insertion and deletion is as fast as in the linked list, etc.

Prepared by Ujjwal Rijal


Some Key Terminologies used in
4
o Root node: The starting node Trees
of a tree is called the root node
of that tree.
o Terminal node: The node which has no children is called
terminal node or leaf node. Terminal node or leaf node is also
known as external node. A node which is not a leaf node is
called an internal node.
o Non-terminal nodes: The nodes which have children are
called non-terminal nodes. They are also known as internal
nodes.
o Siblings: The children of the same parent are called siblings.
o Path: Path is the sequence of consecutive edges from source
node to the destination node. There is a single unique path
from root to any node.
o Ancestors and Descendants: The ancestors of a node are all
the nodes along the path from the root to the node. For
example: If there is a path from node A to node B, then A is
called an ancestor of B and B is called a descendent of A.
o Degree of a node: The degree of a node is the number of
children (or sub-trees) of that node.
o Degree of a Tree: The degreePrepared by Ujjwal
of a tree Rijal maximum
is the
Some Key Terminologies used in
5
o Trees
Level of a node: The level of a node represents the generation of
a node. If the root node is at level 0, then its next child is at level
1, its grandchild is at level 2, and so on.
o Depth of a node: The depth of a node is the length of the path
from the root to that node. Only the root node has got a depth of
0.
o Height of a node: The height of a node is the length of the path
from the node to the deepest leaf. A leaf node has got the height
of 0.
o Depth of a tree: The length of the largest path from root to the
terminals is said to be depth of tree. Simply, we can say that depth
of a tree is equal to the longest path from root to any leaf. In other
words, depth of a tree is the height of the tree.
o Height of a tree: The height of a tree is the height of the root. In
other words, height of a tree is the depth of the tree.
o Key: Key represents a value of a node based on which a search
operation is to be carried out for a node.
o Sub-tree: Sub-tree represents all the descendants of a node.
o Traversing: Traversing means passing through nodes in a specific
order.
Prepared by Ujjwal Rijal
Some Key Terminologies used in
6
Trees

Prepared by Ujjwal Rijal


Introduction to Binary Trees
7
o A binary tree is a finite set of elements that are either empty or is
partitioned into three disjoint subsets. The first subset contains a
single element called the root of the tree. The other two subsets
are themselves binary trees called the left and right subtrees of
the original tree. A left or right subtree can be empty. Each
element of a binary tree is called a node of the tree.
o In general, tree nodes can have any number of children. But, in a
binary tree, each node can have at most two children. A binary
tree is either empty or consists of a node called the root together
with two binary trees called the left subtrees and the right
subtrees.
o A tree with no nodes is called as a null tree. A binary tree
with 9 nodes where A is the root is shown in the below
figure:

Prepared by Ujjwal Rijal


Types of Binary Trees
8
o Binary trees can generally be categorized into the
following three types:
 Strictly binary tree
 Complete binary tree
 Almost complete binary tree.
o Now, these are discussed below:

Strictly Binary Tree


o If every non-leaf node in a binary tree has non-empty left and right
subtrees, then the tree is termed as strictly binary tree.

Prepared by Ujjwal Rijal


Types of Binary Trees
9
Complete Binary Tree
o A complete binary tree of depth “d” is called strictly
binary tree if all of whose leaves are at level “d”.
o A complete binary tree with depth “d” has 2d leaves, (2d -
1) non-leaf (or internal) nodes and (2d+1 -1) total number
of nodes.

Prepared by Ujjwal Rijal


Types of Binary Trees
10
Almost Complete Binary Tree
o A binary tree of depth d is an almost complete binary tree
if:
 Each leaf in the tree is either at level d or level d-1.
 For any node nd in the tree with a right descendant at level d, all
the left descendants of nd that are leaves are also at level d. It
means nodes should be present in left to right at any level; there
should not be any missing node in the traversal.

Prepared by Ujjwal Rijal


Binary Tree Traversal Techniques
11
o There are following three popular methods of
binary tree traversal:
 Pre-order traversal
 In-order traversal
 Post-order traversal
o The most commonly used techniques for binary
tree traversal is in-order traversal.
o Now, they are discussed below:

 Pre-order Traversal
o Visit the root node.
o Traverse the left sub-tree in pre-order.
o Traverse the right sub-tree in pre-order.

Prepared by Ujjwal Rijal


Binary Tree Traversal Techniques
12
o Consider the following figure:

Fig(a): Binary Tree

o The pre-order traversal output of the above


given binary tree in fig(a) is: ABDHIECFG.
o The pre-order traversal is also known as the
depth first order traversal.
Prepared by Ujjwal Rijal
Binary Tree Traversal Techniques
13
 In-order Traversal
o Traverse the left sub-tree in in-order.
o Visit the root node.
o Traverse the right sub-tree in in-order.

Prepared by Ujjwal Rijal


Binary Tree Traversal Techniques
14
o Consider the following figure:

Fig(a): Binary Tree

o The in-order traversal output of the above


given binary tree in fig(a) is: HDIBEAFCG.

Prepared by Ujjwal Rijal


Binary Tree Traversal Techniques
15
 Post-order Traversal
o Traverse the left sub-tree in post-order.
o Traverse the right sub-tree in post-order.
o Visit the root node.

Prepared by Ujjwal Rijal


Binary Tree Traversal Techniques
16
o Consider the following figure:

Fig(a): Binary Tree

o The post-order traversal output of the above


given binary tree in fig(a) is: HIDEBFGCA.

Prepared by Ujjwal Rijal


Applications of Trees
17
 Tree as a model in computer file system

o Files in computer memory can be organized into


directories or folders in which folder contains both
files and sub-folders.
o In file system, root directory or folder contains files
in the system.
o So, a file system can be represented by a rooted
tree, where the root represents main folder, internal
vertices represent sub-folders, and leaf nodes
represent ordinary files or the empty folders.
o For example:

Prepared by Ujjwal Rijal


Applications of Trees
18

Prepared by Ujjwal Rijal


Applications of Trees
19
 Tree as a model for parallel processing

o A parallel processing system consists of many


processing devices (CPU).
o These processors are interconnected with each
other to complete a task in parallel computing
fashion.
o Tree can be used to represent the connection
between these processors.
o Here, a processor is represented by vertex and the
edges between these vertices are used to represent
the connection between them.
o A tree connected network for seven (7)
processors is shown in the below figure:
Prepared by Ujjwal Rijal
Applications of Trees
20

Prepared by Ujjwal Rijal


Applications of Trees
21
 Prefix Codes

o A prefix code is a special type of coding system in


which each code satisfy a prefix property.
o Prefix property means that there is no code in the
system which is prefix of any other code word in
the system.
o For example: A code with coding (code word) {0,
10, 11} is a prefix code. But the coding system with
the code words {0, 01, 011} is not a prefix code
since “0” is the prefix of second code word (01),
and “01” is the prefix of third code word (011).
o A prefix code can be represented by using binary
tree where the symbols are leaf nodes and the
edges are labelled with code words.
Prepared by Ujjwal Rijal
Applications of Trees
22
o The edges of the tree are labelled so that an edge
leading to a left child is assigned a “0” bit and a
right child is assigned a “1” bit.
o Now the bit string used to encode a character is the
sequence of edges in the unique path from the root
to that leaf node with the specific character.

o For example:
o Let’s draw a tree from the following given
characters by maintaining the prefix property as
follows:

Prepared by Ujjwal Rijal


Applications of Trees
23

o Now the prefix for the characters a, e, n, s, and t can be


written as follows:
Characters Code
a 10
e 0
n 1110
s 1111
t 110 Prepared by Ujjwal Rijal
Applications of Trees
21
 Binary Search Tree (BST)

o A Binary Search Tree (BST) is a binary tree that is


either empty or in which every node contains a key
and satisfies the following conditions if it is not
empty:
o Every element has a key (or value) and no two
elements have the same key-value.
o All the keys in the left sub-trees are smaller than
the key in the root node.
o All the keys in the right sub-trees are greater than
the key in the root node.
o The left and the right sub-trees of the root are also
again the binary search trees.
Prepared by Ujjwal Rijal
Applications of Trees
21

Prepared by Ujjwal Rijal


Applications of Trees
21

Prepared by Ujjwal Rijal


Applications of Trees
21
 Game Trees

o The game can normally be represented as a tree


where the nodes represent the current status of the
game and the arcs represent the moves.
o The game tree consists of all the possible moves for
the current player starting at the root and all
possible moves for the next player as the children
of these nodes, and so on.
o The leaves of the game tree represent the terminal
positions as one where the outcome of the game is
clear.
o The outcome of the game is either a win (1),
or a draw (0), or a loss (-1). And, the high scores
are always considered good in a game.
Prepared by Ujjwal Rijal
Applications of Trees
21

Prepared by Ujjwal Rijal


Applications of Trees
21

Prepared by Ujjwal Rijal


Applications of Trees
21

Another Example

Prepared by Ujjwal Rijal


Applications of Trees
21

Another Example: Tic-Tac-Toe

Prepared by Ujjwal Rijal


Applications of Trees
21
 Decision Trees

o The decision tree is a hierarchical tree structure or


a diagram, which helps us to choose between
various actions. It is mostly used for decision-
making purposes. It is a type of rooted tree in which
each internal vertex corresponds to a decision.
These vertices contain a subtree for each possible
outcome of the decision. The paths to leaves vertex
correspond to the possible solutions to the problem.

Prepared by Ujjwal Rijal


Applications of Trees
21

Prepared by Ujjwal Rijal


Applications of Trees
21

Prepared by Ujjwal Rijal

You might also like