0% found this document useful (0 votes)
42 views101 pages

Unit I

Uploaded by

Sai Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views101 pages

Unit I

Uploaded by

Sai Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 101

UNIT I

TREES

SCSA 1304 - Unit I School of Computing 1


COURSE
OBJECTIVES

SCSA 1304 - Unit I School of Computing 2


Course Contents
Trees
•Ordinary and Binary trees terminology
•Properties of Binary trees
•Implementation using Array and Linked list

Binary tree
ADT representations
Recursive and Non Recursive Traversals
Binary Search tree
Insertion
Deletion

SCSA 1304 - Unit I School of Computing 3


Recap Of Linear
Data structures

SCSA 1304 - Unit I School of Computing 4


SCSA 1304 - Unit I School of Computing 5
Non
Linear
Data
structures

SCSA 1304 - Unit I School of Computing 6


Introduction to Trees

SCSA 1304 - Unit I School of Computing 7


Nature Tree leaves

branches

root

SCSA 1304 - Unit I School of Computing 8


Technical Tree
root leaves

branches

nodes

SCSA 1304 - Unit I School of Computing 9


Linear List and Trees
• Linear lists are useful for serially ordered data.
– (x0, x1, x2, …, xn)
– Movies released in a year
– Goals scored by a player
• [(match1, goals), (match2, goals) …]
– Months in a year.
– Students in this class.

SCSA 1304 - Unit I School of Computing 10


Trees
• Trees are useful for hierarchically ordered
data.
– Academic Structure
• Chancellor, Vice Chancellor, Registrar, CoE, Dean, Head,
Year Coordinator, Counsellor

– Employees of a corporation.
• President, vice presidents, managers, and so on.

SCSA 1304 - Unit I School of Computing 11


Hierarchy Data

Hierarchy Data
• The element at the top of the hierarchy is the
root.
• Elements next in the hierarchy are the
children of the root.
• Elements next in the hierarchy are the
grandchildren of the root, and so on.
• Elements that have no children are leaves.
SCSA 1304 - Unit I School of Computing 12
Example

(root) (children)
great grand children

(grand children)

SCSA 1304 - Unit I School of Computing 13


Definition
• A tree ‘t’ is a finite nonempty set of elements.
• One of these elements is called the root.
• The remaining elements, if any, are
partitioned into trees, which are called the
subtrees of ‘t’.

SCSA 1304 - Unit I School of Computing 14


Terminologies
• Root
• Sub Trees
• Leaves
• Parent
• Grand Parent
• Siblings
• Ancestors
• Descendants

SCSA 1304 - Unit I School of Computing 15


Root

SCSA 1304 - Unit I School of Computing 16


Subtrees

SCSA 1304 - Unit I School of Computing 17


Leaves

SCSA 1304 - Unit I School of Computing 18


Siblings

SCSA 1304 - Unit I School of Computing 19


Ancestor and Descendants
Ancestor

SCSA 1304 - Unit I School of Computing 20


Levels
Level 3

Level 1 Level 2
Level 5 Level 6

Level 4

SCSA 1304 - Unit I School of Computing 21


Level
• Root – Level 0
• Children – Level 1
• Grand Children – Level 2
• and so on…

SCSA 1304 - Unit I School of Computing 22


Height = Depth = Number of Levels

Height = Depth = 6

SCSA 1304 - Unit I School of Computing 23


Node Degree = Number of Children
0

3 0
1
1 0

1 2
0

SCSA 1304 - Unit I School of Computing 24


Tree Degree = Maximum Node Degree
0

1 3 0
1 0

1 2

Tree Degree = 3

SCSA 1304 - Unit I School of Computing 25


Representation of Trees
A

B C D

E F G H I J

K L
List M

SCSA 1304 - Unit I School of Computing 26


A
List B C D
A(B(E(K,L),F),C(G),(D(H(M),I,J))) E F G H I J
K L M

DATA CHILD 1 CHILD 2 …. CHILD k

SCSA 1304 - Unit I School of Computing 27


A

List B C D

A(B(E(K,L),F),C(G),(D(H(M),I,J))) E F G H I J
K L M
A 0

D I 0

B C G 0

H M 0
E K L 0

SCSA 1304 - Unit I School of Computing 28


Binary Tree
• Finite collection of elements.
• A nonempty binary tree has a root element.
• The remaining elements are partitioned into
two binary trees.
• These are called the left and right subtrees of
the binary tree.

SCSA 1304 - Unit I School of Computing 29


Binary Tree Vs Tree
Binary Tree Tree

No node in a binary tree may have a No limit on the degree of a node in a


degree more than 2 tree

A binary tree may be empty A tree cannot be empty

The subtrees are ordered The subtrees are not ordered


a a

b b

Left & Right Binary Trees No difference. Treated in a similar


manner
SCSA 1304 - Unit I School of Computing 30
A
Binary Tree
B C

D E F G

H I J K

SCSA 1304 - Unit I School of Computing 31


Special Binary Tree
• Skewed Tree
– Left Skewed
– Right Skewed
• Complete Binary Tree
• Full Binary Tree

SCSA 1304 - Unit I School of Computing 32


Left Skewed Tree A

SCSA 1304 - Unit I School of Computing 33


A Right Skewed Tree
B

SCSA 1304 - Unit I School of Computing 34


Complete Binary Tree
• A complete binary tree is a tree that is
completely filled, with the possible exception of
the bottom level.
• The bottom level is filled from left to right.
• A binary tree T with n levels is complete if
all levels except possibly the last are completely
full, and the last level has all its nodes to the left
side.
SCSA 1304 - Unit I School of Computing 35
Complete Binary Tree
A

B C

D E F G

H I J K

SCSA 1304 - Unit I School of Computing 36


Full Binary Tree
• A Full Binary Tree of depth k is a binary tree of
depth k having 2k-1 nodes, k  0;

SCSA 1304 - Unit I School of Computing 37


Full Binary Tree
A

B C

D E F G

H I J K L M N O

SCSA 1304 - Unit I School of Computing 38


Properties
• Maximum Number of Nodes
• Relation between number of leaf nodes and
degree-2 nodes

SCSA 1304 - Unit I School of Computing 39


Maximum number of nodes
• The maximum number of nodes on level i of a
binary tree is 2i-1, i  1.

• The maximum number of nodes in a binary


tree of depth k is 2k-1, k  1.

SCSA 1304 - Unit I School of Computing 40


Proof (1)
• Proof by induction on i
• Root is the only node on level
i=1
• Maximum number of nodes on level
i=1
Therefore, 2i-1 = 20 = 1

SCSA 1304 - Unit I School of Computing 41


Contd…
• Induction Hypothesis:
Let i be an arbitrary positive integer greater than 1.
Assume that the maximum number of nodes on
level i - 1 is 2i-2.

SCSA 1304 - Unit I School of Computing 42


Contd…
• Induction Step:
• The maximum number of nodes on level i – 1 is 2i
- 2 by the induction hypothesis.
• Since each node in the binary tree has a
maximum degree of 2,
Max. no. of nodes on level i =
2 X max. no. of nodes
on level i – 1 or 2i-1.
SCSA 1304 - Unit I School of Computing 43
Proof (2)

SCSA 1304 - Unit I School of Computing 44


Leaf Nodes and degree-2 nodes

For any nonempty binary tree, T, if n0 is the


number of leaf nodes and n2 the number of
nodes with degree 2, then n0 = n2+1.

SCSA 1304 - Unit I School of Computing 45


A Example 1

B C
n2 = 5
no = 6

D E F G

H I J K

SCSA 1304 - Unit I School of Computing 46


A A
Example 2
B B

n2 = 0 C
C
no = 1
D
D
E

SCSA 1304 - Unit I School of Computing 47


Proof
• Number of nodes of degree 1 = n1
• Total number of nodes = n
• All nodes in T are at most of degree 2, then
n = n 0 + n 1 + n2  (1)
• On counting the number of branches in a
binary tree, it is observed that every node
except the root has a branch leading to it
SCSA 1304 - Unit I School of Computing 48
Contd…
• Number of branches = B, then
n=B+1
• All branches stem from a node of degree 1 or 2.
Thus
B = n1 + 2 n2
• Hence,
n=B+1
= n1 + 2 n 2 + 1  (2)
SCSA 1304 - Unit I School of Computing 49
Contd…
Sub. Equation (1) & (2)

n 0 = n2 + 1

Hence Proved.

SCSA 1304 - Unit I School of Computing 50


Binary Tree Representations

• Array Representation
• Linked Representation

SCSA 1304 - Unit I School of Computing 51


Array Representation
• If a complete binary tree with n nodes is
represented sequentially, then for any node with
index i, 1  i  n,
– parent(i) is at  i / 2  if i > 1.
• If i = 1, i is at the root and has no parent.
– leftchild(i) is at 2i if 2i  n,
• if 2i > n, i has no left child
– rightchild(i) is at 2i + 1 if 2i +1  n,
• if 2i +1 > n, i has no right child

SCSA 1304 - Unit I School of Computing 52


1
Example 1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

SCSA 1304 - Unit I School of Computing 53


index tree
[0] --
[1] A
Example 1 A
[2] B
[3] C
[4] D
B C [5] E
[6] F
[7] G
[8] H
D E F G [9] I
[10] J
[11] K
[12] L
H I J K L M N O
[13] M
[14] N
SCSA 1304 - Unit I School of Computing 54
[15] O
index tree index tree

Example 2[1] [0] --


A
[17]
[18]
--
--
[2] B [19] --
A
[3] -- [20] --
[4] C [21] --
B [5] -- [22] --
[6] -- [23] --
[7] -- [24] --
C [8] D [25] --
[9] -- [26] --
[10] -- [27] --
D [11] -- [28] --
[12] -- [29] --
[13] -- [30] --
[14] -- [31] --
E
[15] -- [32] --
SCSA 1304 - Unit I School of Computing 55
[16] E
index tree

Example 3 [0]
[1]
--
A
[2] --
A
[3] B
[4] --
B [5] --
[6] --
[7] C
C
[8] --
[9] --
D … --
[15] D
… --
E
[32] E

SCSA 1304 - Unit I School of Computing 56


Linked Representation
leftChild data rightChild

data

leftChild rightChild

SCSA 1304 - Unit I School of Computing 57


A 0
Example 1 A

B 0
B

C 0
C

D 0
D

0 E 0
E

SCSA 1304 - Unit I School of Computing 58


0 A

A
Example 2
0 B
B

C 0 C

D
0 D

E
0 E 0

SCSA 1304 - Unit I School of Computing 59


Example 3 A

B C

D E F G

0 H 0 0 I 0 0 J 0 0 K 0 0 L 0 0 M 0 0 N 0 0 O 0

SCSA 1304 - Unit I School of Computing 60


ADT – Binary Tree
Class BinaryTree {
•Objects:
– A finite set of nodes either empty or consisting of a root node, left
BinaryTree and right BinaryTree
•Methods:
– BinaryTree() // Empty Binary Tree
– isEmpty() // Is Binary Tree empty?
– leftSubTree() // returns the left subtree
– rightSubTree() // returns the right subtree
– rootData() // returns data in the root node
}

SCSA 1304 - Unit I School of Computing 61


Traversal

• Visit each node in the tree exactly once.


• When a node is visited, an operation is
performed on it.
• A full traversal produces a linear order for the
nodes in a tree

SCSA 1304 - Unit I School of Computing 62


Traversal contd..
• L – Moving Left
• V – Visiting Node
• R – Moving Right

• Six Possible combinations:


• LVR, VLR, LRV, RVL, VRL, RLV

SCSA 1304 - Unit I School of Computing 63


Traversal Contd..
• Inorder
– LVR, RVL
• Preorder
– VLR, VRL
• Postorder
– LRV, RLV
SCSA 1304 - Unit I School of Computing 64
ADT – Binary Tree
Class BinaryTree {
•Objects:
– A finite set of nodes either empty or consisting of a root node,
left BinaryTree and right BinaryTree
•Methods:
– inOrder();
– preOrder();
– postOrder();
– levelOrder();
}

SCSA 1304 - Unit I School of Computing 65


InOrder Traversal (LVR)
– L - Process the entire left sub-tree
– V - Process the current node
– R - Process the entire right sub-tree

(Traverse Left, Current, Traverse Right)

SCSA 1304 - Unit I School of Computing 66


PreOrder Traversal (VLR)
– V - Process the current node
– L - Process the entire left sub-tree
– R - Process the entire right sub-tree

(Current, Traverse Left, Traverse Right)

SCSA 1304 - Unit I School of Computing 67


PostOrder Traversal (LRV)
– L - Process the entire left sub-tree
– R - Process the entire right sub-tree
– V - Process the current node

(Traverse Left, Traverse Right, Current)

SCSA 1304 - Unit I School of Computing 68


Inorder Traversal
a

b c

bac

SCSA 1304 - Unit I School of Computing 69


bt re e
Example - Inorder t Su
12.
Visi
Lef t Rig
Traversal isit ht Sub
1. V . tree
c k
a
rs e B e: a
r a ve valu
T
e
7.
1 1. t the 18
e V in e .V
ubtr is
it Pr t re is
ft S k. R
i S ub it
R
Le c g ht t ig
si t Ba e : b ef

e: k.
i S L ht

l u ac
V s e al u ub
sit Su

c
2.

va e B
e r t i
v v r bt
ra the ee .V

he r s
T . 3 re

t t ve
1 e.
6. int

in ra
r

Pr 7. T
P

1
3. Left Subtree is NULL 8. Left Subtree is NULL 14. Left Subtree is NULL 19. Left Subtree is NULL
4. Print the Value: d 9. Print the Value: e 15. Print the Value: f 20. Print the Value: g
5. Right Subtree is NULL 10. Right Subtree is NULL 16. Right Subtree is NULL 21. Right Subtree is NULL

SCSA 1304 - Unit I School of Computing 70


Exercise +

* e

*
d

/ c

a
b a / b * c * d +e
SCSA 1304 - Unit I School of Computing 71
Preorder Traversal
a

b c

abc

SCSA 1304 - Unit I School of Computing 72


1. Print the Value: a
2. Visit Left Sub Tree 12. Visit Right Sub Tree
Example -
Preorder
Traversal
3. Print the Value: b 15. Visit Right Sub Tree
8. Visit Right Sub13. Print the Value: c
Tree
4. Visit Left Sub Tree 14. Visit Left Sub Tree

5. Print the Value: d 9. Print the Value: e 15. Print the Value: f 18. Print the Value: g
6. Left Sub Tree is NULL 10. Left Sub Tree is NULL 16. Left Sub Tree is NULL 19. Left Sub Tree is NULL
7. Right Sub Tree is NULL 11. Right Sub Tree is NULL 17. Right Sub Tree is NULL 20. Right Sub Tree is NULL

SCSA 1304 - Unit I School of Computing 73


+
Exercise
* e

*
d

/ c

a
b + * * / a b c d e
SCSA 1304 - Unit I School of Computing 74
Postorder Traversal

b c

bca

SCSA 1304 - Unit I School of Computing 75


Example - 1. Visit Left Sub Tree
20. Visit
11. PrintRight
the Value: a
Sub Tree
PostOrder
Traversal
2. Visit Left Sub Tree 10. Print the Sub12.
Value: Visit
b Left Sub Tree 15.
19. Visit
PrintRight Sub Tree
the Value: c
6. Visit Right Tree

3. Left Sub Tree is NULL 7. Left Sub Tree is NULL 13. Left Sub Tree is NULL 16. Left Sub Tree is NULL
4 Right Sub Tree is NULL 8. Right Sub Tree is NULL 14. Right Sub Tree is NULL 17. Right Sub Tree is NULL
5. Print the Value: d 9. Print the Value: e 15. Print the Value: f 18. Print the Value: g

SCSA 1304 - Unit I School of Computing 76


+

Exercise * e

*
d

/ c

a
b a b / c * d * e +
SCSA 1304 - Unit I School of Computing 77
Level Order Traversing
• Visit the root first
• Visit the roots left child
• Visit the roots right child
• Continue the traverse at each level
• Use Queue Data Structure

SCSA 1304 - Unit I School of Computing 78


Level order Traversal
a

b c

abc

SCSA 1304 - Unit I School of Computing 79


Example queue

root a

Print a b c
Print b c d e
Print c d e f g

Print d e f g

Print e f g

Print f g

Print g

SCSA 1304 - Unit I School of Computing 80


Binary Search Trees (BST)
• A Binary Search Tree is a binary Tree.
• It may be empty.
• If it is not empty then it satisfies the following
properties:
– Every element has a key and no two elements have the
same key
– The keys (if any) in the left subtree are smaller than the
key in the root.
– The keys (if any) in the right subtree are greater than the
key in the root.
– The
SCSA 1304 - Unit Ileft and right subtrees are also binary search trees. 81
School of Computing
Examples

20

10 25

SCSA 1304 - Unit I School of Computing 82


100 60

90 70

80
80

90
70

Examples 100

60

SCSA 1304 - Unit I School of Computing 83


92
Example
90
93

88
91

87 89

SCSA 1304 - Unit I School of Computing 84


200
Example
150 300

100 175 250 350

50 125 160 190 225 275 325 400

SCSA 1304 - Unit I School of Computing 85


Identify:
20

10 15

Not a BST

Not a BST

SCSA 1304 - Unit I School of Computing 86


ADT of •BST
Successor
• Search • Traverse
• Insert – In Order
• Delete – Pre Order
• Minimum – Post Order
• Maximum – Level Order
• Predecessor

SCSA 1304 - Unit I School of Computing 87


Search Algorithm (Recursive)
bftSearch(x, k)
1. if x = NIL or k = key[x]
2. then return x
3. if k < key[x]
4. then return bftSearch(left[x], k)
5. else return bftSearch(right[x], k)

SCSA 1304 - Unit I School of Computing 88


Search Algorithm (Iterative)
bftSearch(x, k)
1. while x  NIL and k  key[x]
2. do if k < key[x]
3. then x  left[x]
4. else x  right[x]
5. return x
SCSA 1304 - Unit I School of Computing 89
Minimum & Maximum
 The binary-search-tree property guarantees
that:
» The minimum is located at the left-most node.
» The maximum is located at the right-most node.
bstMinimum(x) bstMaximum(x)
1. while left[x]  NIL 1. while right[x]  NIL
2. do x  left[x] 2. do x  right[x]
3. return x 3. return x

SCSA 1304 - Unit I School of Computing 90


Successor
• Successor of node x is the node y such that key[y] is the
smallest key greater than key[x].
• The successor of the largest key is NIL.
• Search consists of two cases.
– If node x has a non-empty right subtree, then x’s successor is the
minimum in the right subtree of x.
– If node x has an empty right subtree, then:
• As long as we move to the left up the tree (move up through right
children), we are visiting smaller keys.
• x’s successor y is the node that x is the predecessor of (x is the maximum
in y’s left subtree).
• In other words, x’s successor y, is the lowest ancestor of x whose left child
is also an ancestor of x.

SCSA 1304 - Unit I School of Computing 91


Pseudocode
bstSuccessor(x)
1. if right[x]  NIL
2. then return bstMinimum(right[x])
3. y  p[x]
4. while y  NIL and x = right[y]
5. do x  y
6. y  p[y]
7. return y

SCSA 1304 - Unit I School of Computing 92


Predecessor
• Predecessor of node x is the node y such that key[y] is the largest
key smaller than key[x].
• The Predecessor of the smallest key is NIL.
• Search consists of two cases.
– If node x has a non-empty left subtree, then x’s Predecessor is the
maximum in the right subtree of x.
– If node x has an empty left subtree, then:
• As long as we move to the right down the tree (move down through left
children), we are visiting larger keys.
• x’s Predecessor y is the node that x is the predecessor of (x is the minimum in
y’s right subtree).
• In other words, x’s Predecessor y, is the greatest descendant of x whose right
child is also an descendant of x.
SCSA 1304 - Unit I School of Computing 93
Pseudocode
bstPredecessor(x)
1. if left[x]  NIL
2. then return bstMaximum(left[x])
3. y  p[x]
4. while y  NIL and x = left[y]
5. do x  y
6. y  p[y]
7. return y

SCSA 1304 - Unit I School of Computing 94


BST-Insertion
bstInsert(T, z)
1. y  NIL
2. x  root[T]
3. while x  NIL
4. do y  x
5. if key[z] < key[x]
6. then x  left[x]
7. else x  right[x]
8. p[z]  y
9. if y = NIL
10. then root[t]  z
11. else if key[z] < key[y]
12. then left[y]  z
13.
SCSA 1304 - Unit I
else right[y] School
z of Computing 95
BST-Deletion
Case 0 – Leaf Node
if x has no children
then remove x

X=

SCSA 1304 - Unit I School of Computing 96


BST-Deletion
Case 1 – Node with one Child
if x has one child
then make p[x] point to child

X=
X=

SCSA 1304 - Unit I School of Computing 97


BST-Deletion
Case 2 – Node with Two Child
if x has two children (subtrees)
then
swap x with its successor
perform case 0 or case 1 to delete it

X=

SCSA 1304 - Unit I School of Computing 98


BST Deletion
bstDelete(T, z)
/* Determine which node to splice out: either z or z’s successor. */
 if left[z] = NIL or right[z] = NIL
 then y  z
 else y  bstSuccessor[z]
/* Set x to a non-NIL child of x, or to NIL if y has no children. */
4. if left[y]  NIL
5. then x  left[y]
6. else x  right[y]
/* y is removed from the tree by manipulating pointers of p[y] and x */
7. if x  NIL
8. then p[x]  p[y]

SCSA 1304 - Unit I School of Computing 99


BST Deletion
9. if p[y] = NIL
10. then root[T]  x
11. else if y  left[p[i]]
12. then left[p[y]]  x
13. else right[p[y]]  x
/* If z’s successor was spliced out, copy its data into z */
14. if y  z
15. then key[z]  key[y]
16. copy y’s satellite data into z.
17. return y
SCSA 1304 - Unit I School of Computing 100
Conversions
• Construct binary tree from traversals
1) In order: 2,1, 3
Post order: 2, 3, 1
2) In order: 4, 8, 2, 5, 1, 6, 3, 7
Post order: 8, 4, 5, 2, 6, 7, 3, 1
3) In order: 9, 3, 15, 20, 7
Post order: 9, 15, 7, 20, 3
4) In order: 2, 5, 6, 10, 12, 14, 15
Post order: 2, 6, 5, 12, 15, 14, 10

SCSA 1304 - Unit I School of Computing 101

You might also like