7.0 Tree
7.0 Tree
Algorithm (IT-209)
Instructor: Abdullah Javed
([email protected])
Lecturer,
Govt. Postgraduate College, Jhelum
Lecture 7.0
Fall 2020
Agenda
• Tree Introduction
• Definition
• Representation
• Applications
• Terminologies
• Tree Traversal
• Binary Tree
• Implementation
Array Based
Linked List Based
• Expression Tree
2
Tree
• Nature view of tree
leave
s
branche
s roo
t
3
Tree
• Computer scientist’s view of tree
roo
t
leave
s
branche
s nodes
4
Tree (Definition)
• Tree represents the nodes connected by edges
• Each node can have 0 or more children
• A node can have at most one parent
5
Formal Definition
• A tree is a collection of nodes with the following properties:
The collection can be empty.
Otherwise, a tree consists of a distinguished node r, called root, and
zero or more nonempty sub-trees T1, T2, … , Tk, each of whose roots are
connected by a directed edge from r.
• The root of each sub-tree is said to be child of r, and r is the
parent of each sub-tree root.
• If a tree is a collection of N nodes, then it has N-1 edges.
root
...
T1 T2 Tk 6
Tree (Representation)
7
Tree Applications
• Table of contents of a book
• Organization hierarchy
• Unix file system
• Representing sorted lists of data
• Routing algorithms
10
Hierarchical Structure of a
University
11
Important Terminologies
• Path – Path refers to the sequence of nodes along the edges of a tree
A path from node n1 to nk is defined as a sequence of nodes n1, n2, …, nk such that ni is parent of ni+1
(1 ≤i < k)
The length of a path is the number of edges on that path.
There is a path of length zero from every node to itself.
There is exactly one path from the root to each node.
12
Important Terms
• Ancestor – A node reachable by repeated proceedings from child to parent
If there is a path from n1 to n2, then n1 is ancestor of n2, and n2 is descendent of n1.
If n1 ≠ n2 then n1 is proper ancestor of n2, and n2 is proper descendent of n1
• Height – Height of a node is the length of the longest path from that node to any leaf
The height of node ni is the length of longest path from node ni to a leaf.
B C D E F G
H I J K L M N
P Q
Node A has 6 children: B, C, D, E, F, G.
B, C, H, I, P, Q, K, L, M, N are leaves in the tree above.
K, L, M are siblings since F is parent of all of them.
14
Terminologies
A tree, with height and depth information
15
Not a Tree
• Structures that are not trees.
B C
D E F
G H I
16
Not a Tree
• Structures that are not trees.
B C
D E F
G H I
17
Not a Tree
• Structures that are not trees.
B C
D E F
G H I
18
Tree Traversal
• Traversal
is a process to visit all the nodes of a
tree and print their values. We can’t access a
random node in a tree instead we start from root
node. We use the following three ways to traverse
tree nodes:
Pre-Order Traversal
post-Order Traversal
In-Order Traversal
19
In-Order Traversal
• In
this method, the left subtree is visited first, then
the root and later the right subtree
21
Pre-Order Traversal
• Inthis method, the root node is visited first, then the
left subtree and finally the right subtree
23
Post-Order Traversal
• In
this method, the left subtree is traversed first,
then the right subtree, and finally the root node
25
Exercise
• Write the in-order, pre-order, and post-order traversal of the following
tree:
26
Binary Tree
• A binary tree is a tree in which no node can have more than two
children
• A binary tree is a finite set of elements that is 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.
• Each element of a binary tree is called a node of the tree.
27
Binary Tree
• Binary tree with 9 nodes.
B C
D E F
G H I
28
Binary Tree root
B C
D E F
G H I
29
Binary Tree
• Recursive definition
A
root
B C
D E F
Left subtree G H I
Right subtree
30
Binary Tree
• Recursive definition
A
B C
root
D E F
G H I
Left subtree
31
Binary Tree
• Recursive definition
A
B C
D E F
root
G H I
32
Binary Tree
• Recursive definition
A
root
B C
D E F
G H I
Right subtree
33
Binary Tree
• Recursive definition
A
B C
root
D E F
G H I
34
A Pointer-Based Implementation
of Binary Trees
struct BinaryNode {
Object element;
struct BinaryNode *left;
struct BinaryNode *right;
};
35
Linked Structure for Binary Trees
• A node is represented by an object storing
Element
Parent node
36
Linked Structure for Binary Trees
37
An Array-Based Representation
• Nodes are stored in an array A
• Node v is stored at A [ rank ( v ) ]
Rank ( root ) = 1
Left in even: if node is the left child of parent ( node ),
Rank ( node ) = 2 * rank ( parent ( node ) )
Right in odd: if node is the right child of parent ( node ),
Rank ( node ) = 2 * rank ( parent ( node ) ) + 1
• A [ 0 ] is always empty
• A [ i ] is empty if there is no node in the i th position
• The array size N is 2 (h+1)
38
An Array-Based Representation
39
Tree ADT
• Data: any type of objects/data can be stored in a
tree
• Operations / Methods:
root() – return the root of the tree
children(p) – returns the children of a node
size() – returns the number of nodes in the tree
isEmpty() - returns true if the tree is empty
isRoot(p), isInternal(p), isExternal(p)
Insert(data)
Delete()
Traverse()
Search(data)
40
Expression Tree
• Expression tree is a special kind of binary tree used to represent expressions
• Each internal node correspond to operator and each external node corresponds to
operand. Example: (2x(a–1)+(3xb))
41
Example
42
Easy to Generate Infix, Prefix Postfix Expressions
• Infix: ((8–5)*((4+2)/3)
• Prefix: *-85/+423
• Postfix: 85–42+3/*
43