DFS_9_Tree - Knowledge

Download Report

Transcript DFS_9_Tree - Knowledge

Types of Binary Trees
Expression Tree
Traversals?
(A+B)
Operator
+
Operands
A
B
InOrder
A+B
Infix
PreOrder
+AB
Prefix
PostOrder
AB+
Postfix
A binary tree can store anything where the number ‘2’ comes into picture.
In an arithmetic expression:
Operators normally have maximum of 2 operands.
So an arithmetic expression can be easily represented using Binary Tree.
Types of Binary Trees
•Expression Tree:
–A binary tree which stores an,
•Arithmetic expression where,
•Operands are stored as,
–Leaves / Leaf nodes / External nodes of an expression tree
and,
•Operators are stored as,
–Internal nodes of an expression tree.
–Traversals:
•InOrder:
–Gives Infix notation of arithmetic expression.
•PreOrder:
–Gives Prefix notation.
•PostOrder:
–Gives Postfix notation of arithmetic expression.
Expression Tree
Draw the expression tree for expression ‘(A + B)’.
Infix Expression: ( A + B )
Step-1
Postfix
Evaluate it for A = 5, B = 6.
Postfix Expression: A B +
A B +
1) A (Operand)
2) B (Operand)
3) + (Operator)
11
+
2000
A
A
B
2000
2000
4000
4000
2000
5 6000 6
6000
A
B
2000
4000
Expression Tree
Draw the expression tree for expression ‘(A + B)’ and evaluate it for A = 5, B = 6.
Infix Expression: ( A + B )
Step-1
Postfix
Postfix Expression: A B +
A B +
1) A
Evaluation?
2) B
3) +
11
A
A
B
+
5
A
6
B
Expression Tree
((A+B)*(C–D))
Postfix
A B + C D – * [A = 1, B = 2, C = 4, D = 2]
A B + C D – *
A
B
A
A
+
B
+
A
-
C
+
B
A
D
C
+
B
A
C
B
*
6
*
*
+
A
+
B
C
D
A
C
D
2
-
3
+
=
B
D
1
A
2
B
4
C
2
D
Expression Tree
((a*(b+c))/d)
a*(b+c)/d
Postfix
abc+*d/
abc+d/*
(a*((b+c)/d))
abc+*d/
a
c
b
a
a
d
a
b
b
c
a
a
b
4
*
d
2
a
+
c
*
+
*
/
/ 2
*
+
2
d
2
+
1
b
1
c
b
c
a
+
b
c
Evaluate:
a = 2, b = 1, c = 1, d = 2
Expression Tree
Draw the expression tree for following expression:
(a+b)*c–d^e
Evaluate the following expression tree for A = 4, B = 15, C = 5, D = 2, E = 3
+
/
B
*
C
A
^
D
E
Types of Binary Trees
Linked Representation of a Binary Tree
A
B
C
D
Main Disadvantage?
7 nodes
8 null links
E
G
F
Types of Binary Trees
•Threaded Binary Tree:
–If there are n (n>0) nodes in a binary tree,
•No. of null link fields in linked representation would be:
–n+1
–More than 50% of link fields are with null values
and hence,
•Lot of memory space is wasted.
–One way to use these null link fields has been
given by Perlis & Thornton.
•Store pointers / links of some nodes in these null link
fields.
•These extra pointers / links are called:
–Threads
Threaded Binary Tree
Inorder Threading
Inorder traversal: B A G E D F C
NULL
A
NULL
B
C
D
E
G
F
Threaded Binary Tree
Preorder Threading
Preorder traversal: A B C D E G F
A
B
C
D
E
G
NULL
F
Types of Binary Trees
•Threaded Binary Tree:
–3 ways to thread a binary tree:
•Inorder threading:
–Left null link points to Inorder predecessor.
–Right null link points to Inorder successor.
•Preorder threading:
–Left null link points to Preorder predecessor.
–Right null link points to Preorder successor.
•Postorder threading:
–Left null link points to Postorder predecessor.
–Right null link points to Postorder successor.
Types of Binary Trees
•Threaded Binary Tree:
–Advantages:
•Traversal operation is faster.
–Non-recursive traversal can be easily implemented
and,
–Non-recursive algorithm will work faster than
recursive algorithm.
•Predecessor and Successor (Inorder / Preorder
/ Postorder) can be easily determined for any
node.
Types of Binary Trees
•Threaded Binary Tree:
–Disadvantage:
•How to distinguish / identify a link from a
thread?
–How to tell which is a link and which is a thread as
both will contain pointers to nodes.
•2 ways to overcome this disadvantage:
–1] Node structure with 2 extra fields.
LTAG
LC
DATA
RC
RTAG
»LTAG = 1: Pointer of LC is a Thread.
»LTAG = 0: Pointer of LC is a Link.
–Disadvantage:
»2 extra bytes for TAG’s are required in each
node.
Types of Binary Trees
•Threaded Binary Tree:
–Disadvantage:
•How to distinguish / identify a link from a
thread?
–How to tell which is a link and which is a thread as
both will contain pointers to nodes.
•2 ways to overcome this disadvantage:
–2] Store +ve value for Link and –ve value for Thread.
»Get the actual pointer value by taking the
magnitude / mod of the pointer.
–Disadvantage:
»Not possible in a programming language that
does not allow –ve valued pointers.
Types of Binary Trees
Heap Tree
50
30
20
30
40
50
10
Tree T1
20
50
40
10
Tree T2
30
20
40
10
Tree T3
Types of Binary Trees
•Heap Tree:
–Suppose H is a complete binary tree.
–It will be called a heap tree if it satisfies
following property:
•For each and every node in H,
–Value at N is greater than or equal to the value of
each of children of N.
OR
•For each and every node in H,
–N has a value which is greater than or equal to the
value of every successor of N.
Heap Tree
Which is a heap tree?
50
20
30
50
40
10
Tree T1
No
Condition not satisfied
for node 20.
50
30
20
40
10
50
20
40
10
Tree T2
Tree T3
No
Yes
Not a complete binary tree.
Heap Tree
Which is a heap tree?
50
30
20
Max Value
10
40
10
30
50
Min Value
20
40
Tree T1
Yes
Tree T2
Yes
Max Heap
Min Heap
Types of Binary Trees
•Heap Tree:
–Suppose H is a complete binary tree.
–It will be called a heap tree if it satisfies
following property:
•For each node in H,
–Value at N is greater (or smaller) than or equal to the
value of each of children of N.
OR
•For each node in H,
–N has a value which is greater (or smaller) than or
equal to the value of every successor of N.
Heap Tree
•Representation of Heap Tree:
–Any binary tree can be represented in the form of:
•Array (Sequential Representation)
•Linked List (Linked Representation)
–Which representation is better for a Heap tree?
•‘Array representation’ has certain advantages over
‘Linked representation’ in case of Heap tree.
–There will be no / less memory wastage because,
»A heap tree is a complete binary tree.
–No empty space between 2 non-null entries.
»If there are NULL entries, they will be at the tail of the
array.
Heap Tree
Array representation of Heap Tree
Max Heap
Min Heap
50
10
30
20
40
30
10
50
20
40
5
6
7
Index
1
5
6
7
50 30 40 20 10
-
-
Value
10 30 20 50 40
-
-
1
2
3
4
2
3
4
Heap Tree
Insertion into a Heap Tree
Max Heap
95
Max Value
85
45
75
55
25
65
Insert value 19
19
35
15
Heap Tree
Insertion into a Heap Tree
Max Heap
111
111
85
95
Max Value
95
Insert value 111
45
111
85
75
55
65
19
25
35
111
15
25
Heap Tree
Insertion into a Heap Tree
Max Heap
95
Max Value
85
45
75
55
25
65
Insert value 111
19
35
111
15
Heap Tree
•Insertion into a Heap Tree:
–Exercise:
•Form / Build a Max Heap with the following
data:
–19, 55, 44, 98, 67, 48, 95, 66, 70, 69, 30, 24, 99, 82
Types of Binary Trees
Deletion from a Heap Tree
Max Heap
99
Max Value
45
63
35
27
29
12
24
57
26
42
Delete a value
Any node can be
deleted, but deleting
root node has some
special importance.
Types of Binary Trees
•Deletion from a Heap Tree:
–Importance of deletion of root node:
•Deleting root node continuously from max heap
gives:
–Data sorted in descending order.
•Deleting root node continuously from min heap
gives:
–Data sorted in ascending order.
–This sorting of data using a heap tree is
called:
•Heap Sort
Heap Sort
•Heap Sort:
–Works on the basic concept of:
•Heap Tree
–It is always a complete binary tree.
–Stored in the form of array.
–2 types:
»Max Heap
»Min Heap
–Basic steps in a heap sort (using max heap) are:
•Create heap from the array which is to be sorted.
•Repeat the following steps till heap tree is empty:
–Delete root node from heap tree.
–Rebuild heap after deletion.
–Place the deleted node in output.
Array A
Array B
33
14
65
02
76
1
2
3
4
5
1. Create Heap
33
33
1
2
3
33
14
1
2
3
33
14
65
1
2
65
1
4
5
33
14
4
5
3
4
5
14
33
02
2
3
4
33
14
65
65
14
5
02
65
14
33
02
76
1
2
3
4
5
65
14
Output
Array A
76
65
33
02
14
1
2
3
4
5
02
33
76
33
2. Sort the Heap
76
Heap
Array A
76
65
33
02
14
1
2
3
4
5
65
02
Array B would not be required
Directly swap the root with the last element in the heap tree.
Array B
1
2
3
4
5
33
14
2. Sort the Heap
Heap
Array A
76
76
65
33
02
14
1
2
3
4
5
65
02
33
14
N
76
Iteration-1
76
65
33
02
14
1
2
3
4
5
65
02
33
14
i
j
65
Iteration-2
65
14
33
02
76
1
2
3
4
5
14
02
j
i
33
2. Sort the Heap
Heap
Array A
76
76
65
33
02
14
1
2
3
4
5
65
02
33
14
N
33
Iteration-3
33
14
02
65
76
1
2
3
4
5
14
02
i
j
14
Iteration-4
14
02
33
65
76
1
2
3
4
5
j
i
OUTPUT
02
Heap Sort
Create Max Heap
BuildMaxHeap(A)
For i = N down to 2
Swap( A[ 1 ], A[ i ] )
For j = 1 to i – 1
1st iteration:
Swap 1st with
last.
2nd iteration:
Swap 1st with
second last.
And so on...
lchild = 2 * j
rchild = 2 * j + 1
If( A[ j ] < A[ lchild ]) and (A[ lchild ] > A[ rchild ] )
Swap(A[ j ], A[ lchild ])
j = lchild
Check the
position of newly
placed element
with leftchild and
rightchild.
Else
If( A[ j ] < A[ rchild ]) and (A[ rchild ] > A[ lchild] )
Swap(A[ j ], A[ rchild ])
j = rchild
Else
Rebuild the heap
until the newly
placed element
comes to proper
position.
Break;
EndIf
EndFor
EndFor
Heap Sort
•Algorithm:
–HeapSort
•Input:
–Array A which is to be sorted.
•Output:
–Array A with elements sorted in ascending
order.
•Data Structure:
–Array.
Heap Sort
Steps
BuildMaxHeap(A)
For i = N down to 2
Swap( A[ 1 ], A[ i ] )
For j = 1 to i – 1
lchild = 2 * j
rchild = 2 * j + 1
If( A[ j ] < A[ lchild ]) and (A[ lchild ] > A[ rchild ] )
Swap(A[ j ], A[ lchild ])
j = lchild
Else
If( A[ j ] < A[ rchild ]) and (A[ rchild ] > A[ lchild] )
Swap(A[ j ], A[ rchild ])
j = rchild
Else
Break;
EndIf
EndFor
EndFor
Types of Binary Trees
Binary Search Tree (BST)
50
30
20
50
40
10
Tree T1
Heap Tree
30
10
50
60
55
Tree T2
30
80
10
60
40
Tree T3
80
Types of Binary Trees
•Binary Search Tree (BST):
–A binary tree is called a Binary Search Tree
if,
•Each node N satisfies following property:
•The value at N is,
–Greater than every value in the left sub-tree of N and
is,
–Less than every value in the right sub-tree of N.
Binary Search Tree (BST)
Which is a BST?
10
20
50
30
40
50
50
30
30
55
60
10
35
80
Tree T1
Tree T2
Tree T3
No
No
Yes
Heap Tree
(Min Heap)
Not satisfied for
node with value 30.
Binary Search Tree (BST)
Which is a BST?
Jan
Dec
Yes.
Mar
Lexicographical ordering.
Aug
Feb
Jul
Nov
Jun
(Not numerical ordering)
Binary Search Tree (BST)
Applications of BST
65
19
15
74
28
25
69
57
54
88
80
1. Smallest / Minimum value?
15
2. Largest / Maximum value?
88
3. Search Value 54?
Found.
4. Search Value 90?
Not
Found.
5. InOrder Traversal:
15 19 25 28 54 57 65 69 74 80 88
Data sorted in Ascending Order.
Hence also known as:
Binary Sorted Tree.
Types of Binary Trees
Creation of BST / Insertion into BST
20
20
20, 30, 10
20, 10, 30
10
10
30
10, 20, 30
10
20
30
30
Conclusion:
For a different sequence of the same set of data, there will be a different BST.
10
30
10, 30, 20
30, 10, 20
30
20
30
20
10
20
10
30, 20, 10
Binary Search Tree (BST)
•Applications of BST:
–To find out the minimum / maximum value
from a set of data.
–To search a particular data from a set of
data.
–To sort the data and hence a BST is also
called,
•Binary Sorted Tree.
–To remove duplicate values from set of
data.
Binary Search Tree (BST)
Create a BST for data '57, 28, 25, 69, 88, 81, 74, 65, 19, 15'.
57
28
25
69
57
57
57
57
28
28
25
28
25
69
Types of Binary Trees
•Binary Search Tree (BST):
–Consider the following set of data:
•10, 20, 30, 40, 50, 60
–No. of ways to arrange this data:
•6! = 720
•Some ways to arrange the data are:
–10, 20, 30, 40, 50, 60
–60, 50, 40, 30, 20, 10
–30, 20, 40, 10, 50, 60
–20, 40, 30, 10, 50, 60
–So number of Binary Search Trees (BST) that can
be constructed are:
•720.
Binary Search Tree (BST)
10, 20, 30, 40, 50, 60
60, 50, 40, 30, 20, 10
60
10
50
20
30
40
40
30
50
20
60
10
Binary Search Tree (BST)
30, 20, 40, 10, 50, 60
30, 20, 50, 10, 40, 60
30
20
10
30
40
20
50
10
60
50
40
60
10
20
60
Binary Search Tree (BST)
50
Question:
30 In which tree, searching operation will be faster? 40
40
1
2
50
30
20
60
10
Looks more
balanced.
30
30
20
10
40
3
20
4
50
60
10
50
40
60
Binary Search Tree (BST)
When can we say
that a tree is balanced or not?
Depends on the
balance factor of any node.
10
30
20
50
40
60
What is this balance factor of a node?
Balance Factor (bf) = Height of left sub-tree (hL) – Height of right sub-tree (hR)
Types of Binary Trees
Height Balanced Tree
Calculating Balance Factor of every node.
2–2=0
30
hL = 2
1–0=1
20
hL = 1
0
0–0=0
10
0
1 -1 = 0
1
hR = 0
50
0–0=0
40
0
hR = 2
0
0–0=0
60
0
Types of Binary Trees
•Height Balanced Tree:
–A binary search tree is said to be a height
balanced tree if,
•All its nodes have a balance factor of 1, 0 or -1.
•That is:
–| bf | = | hL – hR | <= 1 for every node in the tree.
Types of Binary Trees
Which is a Height Balanced Tree?
T1
6
T2
1-2= -1
4
3
-1
8
1-0 = 0
1
6
1-0 = 1
2
0-0 = 0
3-1 = 2
3-2 = 1
Yes
8
0
0
0-0 = 0
7
0-0 = 0
2
0
1
4
3
0
5
0
No
Balance Factor not satisfied for root node (6)
Types of Binary Trees
•Height Balanced Tree:
–Properties:
•Every Height Balanced Tree is always a Binary
Search Tree (BST) whereas,
–Every Binary Search Tree (BST) might not be a
Height Balanced Tree.
•Complete Binary Search Tree (BST) is always a
Height Balanced Tree.
–Note:
•It is also called AVL tree after the name of the
scientists:
–Adelson-Velskii, Lendis.
Height Balanced Tree
Creation of BST / Insertion into BST
Create different BSTs for set of data 10, 20, 30.
Arrangement of 3 elements can be done in: 3! = 6 ways
1) 20, 10, 30
2) 20, 30, 10
3) 10, 20, 30
4) 30, 20, 10
5) 10, 30, 20
6) 30, 10, 20
Height Balanced Tree
20, 10, 30
0
20
0
10
0
30
Height Balanced Tree
Height Balanced Tree
20, 30, 10
0
20
0
10
0
30
Height Balanced Tree
Height Balanced Tree
Left Rotation
10, 20, 30
-2
10
20
Left Rotation
20
10
30
30
Not Height Balanced
Height Balanced Tree
Height Balanced Tree
Right Rotation
30, 20, 10
2
30
20
Right Rotation
20
10
30
10
Not Height Balanced
Height Balanced Tree
Height Balanced Tree
10, 30, 20
-2
10
30
Left Rotation
30
10
20
20
Balanced
Not a Binary Search Tree
Not Height Balanced
Not a Height Balanced Tree
Height Balanced Tree
Left-Right Rotation
10, 30, 20
-2
-2
10
10
20
Right Rotation
30
Left Rotation
20
10
30
30
20
Not Height Balanced
Not Height Balanced
Binary Search Tree
Height
Balanced
Tree
Height Balanced Tree
30, 10, 20
2
30
10
Right Rotation
10
20
20
Not Height Balanced
30
Balanced
Not a Binary Search Tree
Not a Height Balanced Tree
Height Balanced Tree
Right-Left Rotation
30, 10, 20
2
2
30
30
20
Right Rotation
Left Rotation
10
20
10
20
Not Height Balanced
30
10
Not Height Balanced
Binary Search Tree
Height
Balanced
Tree
Height Balanced Tree
•AVL Rotations:
–4 types:
•Left Rotation:
–Also known as ‘LL Rotation’.
–Involves only a single Left Rotation.
•Right Rotation:
–Also known as ‘RR Rotation’.
–Involves only a single Right Rotation.
•Left Right Rotation:
–Also known as ‘LR Rotation’.
–Involves 2 rotations:
»First a Right, then a Left.
•Right Left Rotation:
–Also known as ‘RL Rotation’.
–Involves 2 rotations:
»First a Left, then a Right.
Tree
General Tree & Not a Binary Tree
Mono
Sumo
Hari
Prem
Kapil
Liza
Karan
Azu
Peter
Ravi
Mary
Dick
John
Tom
Tree
•Representation of Tree:
–A tree can be represented in a computer
using:
•Array
•Linked List
Tree
Array representation of Tree
Mono
Sumo
Kapil
Ravi
Dick
Hari
Liza
Azu
Mary
John
Prem
Karan
…..
1
2
3
4
5
6
7
8
9
10
11
12
13
Will not work
Mono
Sumo
Kapil
Ravi
Dick
Hari
Liza
-
-
Azu
-
-
-
1
2
3
4
5
6
7
8
9
10
11
12
13
Works
Tree
Array representation of Tree
LEFT CHILD Array SIBLING Array
INDEX
DATA Array
1
Mono
2
0
2
Sumo
6
3
3
Kapil
8
4
4
Ravi
9
5
5
Dick
0
0
6
Hari
0
7
7
Liza
11
0
8
Azu
0
0
9
Mary
14
10
10
John
0
0
11
Prem
0
12
12
Karan
0
13
13
Peter
0
0
14
Tom
0
0
Tree
Linked representation of Tree
Fixed Size Node (Version 1)
Advantage:
Easy Algorithm
Disadvantage:
Null Links
Mono
Sumo
Hari
Liza
Prem
Karan
Kapil
Azu
Peter
Ravi
Mary
Tom
Dick
John
Tree
Linked representation of Tree
Variable Size Node (Version 2)
Advantage:
Memory Saved
Disadvantage:
Complex Algorithm
Mono 4
Sumo 2
Kapil
Hari
0
Liza
3
Prem
0
Karan 0
1
Ravi
Azu
Peter
0
0
2
Mary
1
Tom
0
Dick
0
John
0
Tree
General Tree using a Linked Representation of Binary Tree
Left Child
Mono
Sumo
Hari
Prem
Kapil
Liza
Karan
Sibling
Ravi
Azu
Dick
Mary
Peter
John
Tom
Tree
Binary Tree representation of Tree
Mono
Mono
Sumo
Hari
Kapil
Liza
Azu
Prem Karan Peter
Ravi
Mary
Dick
Sumo
Kapil
Ravi
Dick
John
Hari
Liza
Prem
Karan
Azu
Mary
Tom
Peter
Tom
John
Forest
W
T2
A
X
B
E
C
Y
Z
D
P
F
T3
T1
Q
R
S
Forest
•Forest:
–Forest is a collection of disjoint trees.
•It can also be represented in the form of:
–Linked Representation of Binary Tree.
Forest
Conversion of Forest to a Binary Tree
A
T1
B
E
C
A
D
F
B
E
C
T1’
D
F
W
W
T2
T2’
X
Y
Z
X
P
Y
Z
P
T3’
T3
Q
R
S
Q
R
S
Forest
Conversion of Forest to a Binary Tree
A
B
E
C
T1’
D
F
W
T2’
X
Y
Z
P
Q
R
T3’
S
Trees
B Tree
Binary Search Trees / 2-way Search Trees
30
40
15
05
F
F
F
F
40
10
F
25
10
25
Height Balanced Trees
45
F
15
05
F
F
Failure Nodes are not on same level.
F
F
F
F
30
F
F
45
F
F
B Tree of Order 2
All Failure nodes are on same level.
B Tree
Is it a B Tree?
3-way Search Tree.
10
F
15
F
F
F
Not a B Tree.
20
40
25
30
F
All Failure nodes are
not on the same level.
45
35
F
X
F
F
50
F
F
B Tree
Is it a B Tree?
3-way Search Tree.
20
30
Yes
All Failure nodes are
on the same level.
X
X
40
X
B Tree of Order 3
10
F
15
F
F
25
F
X
F
35
F
X
F
45
F
50
F
F
B Tree
General Node Structure of B Tree of Order m.
P1
K1
P2
K2
P3
Where:
Ki: Key values in the node.
Pi: Pointers to sub-trees.
K3
P4
….
Km-1
Pm
B Tree
•B Tree:
–A ‘B Tree’ of order m is an,
•m-way search tree where,
•All failure nodes are at the same level where,
–A failure node is,
»A node which can be reached during a search only if,
»The value being searched for is not in the tree.
–Applications:
•Used in index creation in database.
–Note:
•Every B Tree will always be a Search Tree however,
–Every Search Tree might not be a B Tree.
B Tree
Construct a B Tree of order 3 for following set of data:
10, 20, 30, 40, 50, 60, 70
20
10
10
30
10
X
20
20
40
20
X
10
X
30
X
X
70
10
X
30
40
40
20
10
X
X
X
30
60
X
50
X
X
70
X