Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 68

TREE DATA

STRUCTURE
Tree
• Tree is a non-linear data structure where the relations are hierarchical.
• Some objects are "above" and some are "below" others.

• The main terminologies used for tree are "parent", "child", "ancestor" and
"descendant“.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 2


Tree
Definition : A tree is an abstract data type that stores elements hierarchically.
With the exception of the top element, each element in a tree has a parent
element and zero or more children elements.

The top element of the tree is called the root of the tree but is drawn as the
highest element. The other elements are connected below.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 3


Formal Tree Definition
Formally, we define tree T to be a set of nodes storing elements in a parent-child
relationship with the following properties:
• If T is nonempty, it has a special node, called the root of T, that has no parent.
• Each node v of T different from the root has a unique parent node w; every
node with parent w is a child of w.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 4


Tree Example

Figure: Tree representing a part of the file system

2/12/2020 PREPARED BY TASNEEA HOSSAIN 5


Tree Terminologies
• Root : node without parent

• Internal Node: node with at least one child

• External Node (Leaf) : node without children

• Ancestors of a node: parent, grandparent, etc.

• Path :path is a sequence of nodes (a0, a1, ..., an) where ak+ 1is a child of ak

2/12/2020 PREPARED BY TASNEEA HOSSAIN 6


Tree Terminologies
• Descendants of a node: child, grandchild etc

• Depth of a node: number of ancestors excluding the node itself

• Height of a tree: maximum depth of any node

• Siblings: two node having the same parent

• Degree: The degree of a node is defined as the number of its children.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 7


A

B C D

E F G H

I J K Root = A

2/12/2020 PREPARED BY TASNEEA HOSSAIN 8


A

B C D

E F G H

I J K Leaves?
E, I, J, K, G, H, D

2/12/2020 PREPARED BY TASNEEA HOSSAIN 9


A

B C D

E F G H

Siblings?
K (E,F)
I J
(G , H)
(I , J , K)
2/12/2020 PREPARED BY TASNEEA HOSSAIN 10
A

B C D

E F G H

Depth of F ?
I J K
2

2/12/2020 PREPARED BY TASNEEA HOSSAIN 11


A

B C D

E F G H

Height of the tree = ?


I J K
3

2/12/2020 PREPARED BY TASNEEA HOSSAIN 12


A

B C D

E F G H

Degree of C?
I J K
2

2/12/2020 PREPARED BY TASNEEA HOSSAIN 13


Depth and Height
• Let p be a node of a tree T. The depth of p is the number of ancestors of p,
excluding the p itself.

• The depth of p’s node can be recursively defined as follows:


If p is the root, then the depth of p is 0
Otherwise, the depth of p is one plus the depth of parent of p

2/12/2020 PREPARED BY TASNEEA HOSSAIN 14


Depth
Algorithm
Depth(T,p):
If p.isRoot() then
return 0
Else
return 1 + depth(T,p.parent())

2/12/2020 PREPARED BY TASNEEA HOSSAIN 15


Height
Algorithm height(T):
h=0
for each p ∈ T.positions() do
if p.isExternal() then
h = max(h,depth(T,p))
return h

2/12/2020 PREPARED BY TASNEEA HOSSAIN 16


Ordered Trees
• A tree is ordered if there is a linear ordering defined for each child of each
node.

• A binary tree is an ordered tree in which every node has at most two children.

• If each node of a tree has either zero or two children, the tree is called a
proper (strictly) binary tree.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 17


Binary Tree

A A

B C B C

D E F
E F G H

G Strictly binary tree

Binary tree

2/12/2020 PREPARED BY TASNEEA HOSSAIN 18


Definition
• A binary tree is an ordered tree in which every node has at most two children.
1. Every node has at most two children
2. Each child node is labeled as being either left child or right child.
3. A left child preceded a right child in the ordering of children of a node.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 19


Definition
A full node is a node where both the left and right sub-trees are non-empty
trees.
A

B C
Full node

D E F

2/12/2020 PREPARED BY TASNEEA HOSSAIN 20


Definition
An empty node or a null sub tree is any location where a new leaf node could be
appended.
A Here, new leaf nodes could be added at
B,D,E,F,G.
B C

D E F

2/12/2020 PREPARED BY TASNEEA HOSSAIN 21


Application of a binary tree
• An arithmetic expression can be represented by a tree whose external nodes are
associated with variables or constants, and whose internal nodes are associated
with one of the operators +, −, ×, and /. Each node in such a tree has a value
associated with it.
• If a node is external, then its value is that of its variable or constant.
• If a node is internal, then its value is defined by applying its operation to the values
of its children.
• Such an arithmetic-expression tree is a proper binary tree, since each of the
operators
• +, −, ×, and / take exactly two operands. Of course, if we were to allow for unary
operators, like negation (−), as in “−x,” then we could have an improper binary tree.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 22


Application of a binary tree(contd.)
-

/ +

x + x 6

+ 3 - 2 3 -

3 1 9 5 7 4

Expression tree for ((((3+1)×3)/((9−5)+2))−((3×(7−4))+6))


2/12/2020 PREPARED BY TASNEEA HOSSAIN 23
Traversal of Tree
• A traversal of a tree T is a systematic way of accessing or visiting all the nodes
of T.
• Traversal involves the visiting of root and the traversing of its subtrees.

• In a tree traversal, each element of the tree is visited only once.


• There are three types of tree traversals :
 Preorder
 Inorder
 Postorder

2/12/2020 PREPARED BY TASNEEA HOSSAIN 24


Preorder Traversal
• The root is visited first, which is followed by the left subtree and then the right
subtree.
Algorithm:
preOrder(v)
visit(v)
for each child w of v
preorder (w)

2/12/2020 PREPARED BY TASNEEA HOSSAIN 25


Preorder

C
B

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 26


Preorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 27


Preorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 28


Preorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 29


Preorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 30


Preorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 31


Preorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 32


Preorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 33


Preorder

B C

E F I G

Preorder traversal : ABEFCIG

2/12/2020 PREPARED BY TASNEEA HOSSAIN 34


Inorder
• In inorder traversal, a node is visited between the recursive traversals of its left
and right subtrees

Algorithm inOrder(v)
if isInternal (v)
inOrder (leftChild (v))
visit(v)
if isInternal (v)
inOrder (rightChild (v))

2/12/2020 PREPARED BY TASNEEA HOSSAIN 35


Inorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 36


Inorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 37


Inorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 38


Inorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 39


Inorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 40


Inorder

B C

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 41


Inorder

B C

E F I G

2/12/2020
Inorder traversal: EBFAICG
PREPARED BY TASNEEA HOSSAIN 42
Postorder
• In postorder traversal, a node is visited after its descendants

Algorithm
postOrder(v)
for each child w of v
postOrder (w)
visit(v)

2/12/2020 PREPARED BY TASNEEA HOSSAIN 43


Postorder

C
B

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 44


Postorder

C
B

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 45


Postorder

C
B

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 46


Postorder

C
B

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 47


Postorder

C
B

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 48


Postorder

C
B

E F I G

2/12/2020 PREPARED BY TASNEEA HOSSAIN 49


Postorder

C
B

E F I G

Postorder traversal: EFBIGCA

2/12/2020 PREPARED BY TASNEEA HOSSAIN 50


What is the preorder, inorder and postorder traversal of the following tree?

Preorder: PFBHGSRYTWZ

Inorder: BFGHPRSTWYZ

Postorder: BGHFRWTZYSP

2/12/2020 PREPARED BY TASNEEA HOSSAIN 51


Level order traversal
• In a level order traversal, every node on a level is visited before going to a
lower level. A

B C

D E F G

H I

Here, level order traversal is ABCDEFGHI

2/12/2020 PREPARED BY TASNEEA HOSSAIN 52


Proper Binary Tree
A
• A proper binary tree is a tree where:
Each internal node has two children
B C
The children of a node are an ordered pair
D E F G

H I

• Alternative recursive definition: a (proper) binary tree is either


– a tree consisting of a single node, or
– a tree whose root has an ordered pair of children, each of which is a
proper binary tree

2/12/2020 PREPARED BY TASNEEA HOSSAIN 53


Properties of Binary Trees
• Let T be a binary tree with n nodes, and let h denote the height of T. Then T
has the following properties.
– The number of external (leaf) nodes in T is at least 1 and at most 2h .

– The number of internal nodes in T is at least h and at most 2h-1.

– The number of nodes in T is at least h+1 and at most 2h+1-1.

– The height h of T satisfies log(n+1)-1 ≤ h ≤ (n-1)

2/12/2020 PREPARED BY TASNEEA HOSSAIN 54


Properties of Proper Binary Trees
• Let T be a (proper) binary tree with n nodes, and let h denote the height of T.
Then T has the following properties.
– The number of external (leaf) nodes in T is at least h+1 and at most 2h.

– The number of internal nodes in T is at least h and at most 2h-1.

– The number of nodes in T is at least 2h+1 and at most 2h+1-1.

– The height h of T satisfies log(n+1)-1 ≤ h ≤ (n-1)/2.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 55


Perfect Binary Trees
A perfect binary tree of height h is a binary tree where
• All leaf nodes have the same depth h
• All other nodes are full

Alternative Recursive definition:


• A binary tree of height h= 0 is perfect
• A binary tree with height h> 0 is a perfect if both sub-trees are prefect binary
trees of height h–1
2/12/2020 PREPARED BY TASNEEA HOSSAIN 56
Theorems
Four theorems that describe the properties of perfect binary trees:
• A perfect tree has 2h+ 1–1nodes
• The height is Θ (ln(n))
• There are 2h leaf nodes
• The average depth of a node is Θ (ln(n))

2/12/2020 PREPARED BY TASNEEA HOSSAIN 57


Complete Binary Trees
A complete binary tree is filled at each depth from left to right. The order is identical
to that of breadth first traversal.

Recursive definition:
A binary tree with a single node is a complete binary tree of height h = 0 and a
complete binary tree of height h is a tree where either:
• The left sub-tree is a complete tree of height h–1 and the right sub-tree is a perfect
tree of height h–2, or
• The left sub-tree is perfect tree with height h–1 and the right sub-tree is complete
tree with height h–1

2/12/2020 PREPARED BY TASNEEA HOSSAIN 58


Complete Binary Trees
Insertion into a complete binary tree looks like the following:

2/12/2020 PREPARED BY TASNEEA HOSSAIN 59


Complete Binary Trees
Insertion into a complete binary tree looks like the following:

2/12/2020 PREPARED BY TASNEEA HOSSAIN 60


Storing a complete binary tree
In order to store the elements in the tree, what sequence can we follow?
We can go for the level order traversal.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 61


Storing a complete binary tree
In order to store the
tree, we can use an
12 array. The array can be
filled according to the
1 5
level order traversal.

6 4 7
3

9 0 1 2 3 4 5 6 7 8 9

12 1 5 6 4 3 7 9

2/12/2020 PREPARED BY TASNEEA HOSSAIN 62


Adding and deleting
In order to add an element to the tree, the array’s next available location will be
chosen.

Similarly, in case of deletion, the last element in the array has to be deleted if we
want to maintain the complete-tree structure.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 63


In the previous slide the first index or the 0 index of he array was kept empty.
This is because, it makes it easier to track the children and parent of any specific
node.
Parent
If a node has an index j, the parent will be obtained by j/2.
Children
Left child of parent with index j is given by 2j
Right child of parent with index j is given by 2j+1

2/12/2020 PREPARED BY TASNEEA HOSSAIN 64


Linked Structure for binary tree
A node is represented by an object storing 
B
Element
Left child
Right child  
A D
Parent
B

A D    
C K

C K

65

2/12/2020 PREPARED BY TASNEEA HOSSAIN 65


Array Vs Linked Data Structure
If a tree is not complete then using level order traversal to store a tree in an array
might lead to significant memory wastage.
In the following tree, there are 12 nodes but the size of the array is 32.

G
C I

A D H J

B F L

E K

G C I A D H J B F L E K

2/12/2020 PREPARED BY TASNEEA HOSSAIN 66


Array vs Linked Data Structure
If a child node is to be added to node i, then twice the amount of memory will
be required.
In the worst case exponential amount of memory may be required.

2/12/2020 PREPARED BY TASNEEA HOSSAIN 67


The end

2/12/2020 PREPARED BY TASNEEA HOSSAIN 68

You might also like