SlideShare a Scribd company logo
TreesTrees
Kalyani Neve
Asst.Prof.
GHRIBM,Jalgaon
Trees :
A tree is non-linear data structure in which items are arranged in
a sorted sequence. It is used to represent hierarchical relationship
existing amongst several data items.
1. There is a special data item called the root of the tree.
2. Its remaining data items are partitioned into number of
mutually exclusive subset, each of which is itself a tree. And
they are called sub trees.
Degree of node :
It is the number of sub trees of a node in a given tree.
Degree of a tree :
It is the maximum degree of nodes in a given tree.
Terminal Node :
A node with degree 0 is called a terminal node or leaf.
Siblings :
The children nodes of a given parent node are called
siblings. They are also called brothers.
Level:
The entire tree structure is leveled in such a way that the
root is always at level 1. then, its immediate children are
at level 2 and their immediate children are at level 3 so
on.
Path :
It is a sequence of consecutive edges from the source node
to the destination node.
Depth:
It is the maximum level of any node in a given tree.
Forest:
It is set of disjoint trees.
Binary Trees :
A binary tree T is defined as a finite set element, called nodes,
such that
a) T is empty or
b) T contains a distinguished node R, called the root T,
and the remaining nodes of T form an ordered pair of
disjoint binary trees T1 and T2.
A
B
D E
C
G H
J K
L
F
Binary Trees
Properties of Binary Trees :
1. The maximum number of nodes on level i of a binary tree
is 2i-1
, i>=1.
2. The maximum number of nodes in a binary tree of depth k
is 2k
-1, k>=1.
Representation of binary tree in memory
There are two ways of representing T in memory,
1.Static or Array or Sequential Representation
2.Dynamic or Linked Representation
1. Static or Sequential Representation:
This representation uses a single array TREE as follows:
a. The root R of T is stores in TREE[1].
b. If a node N occupies TREE[k], then its left child is stored in
TREE[2*k] and its right child is stored in TREE[2*k+1].
The sequential representation of the binary tree T in fig. (a)
appears in fig. (b).
38
14 56
8 23
82
45
18
70
Fig. (a)
38
14
56
8
23
45
82
18
70
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Tree
Fig. (b)
2. Dynamic or Linked Representation:
Each node N of T will divided into three parts:
INFO[K] contains the data at the node N.
LEFT[K] contains the location of the left child of node N
RIGHT[K] contains the location of the right child of node N
LEFT INFO RIGHT
38
14 56
8 23
18
45 82
70
Representation of binary tree in memory
Full Binary Tree :
A full binary tree of depth k is a binary tree of depth k having
exactly 2k
-1 nodes, k >= 0;
Figure shows a full binary tree of depth 4.
A
B
D E
C
F G
NJH I K L M
Full Binary Trees
O
Complete Binary Trees:
Each node of T can have at most two children. According, one
can show that level r of T can have at most 2r
nodes.
The tree T is said to be Complete binary tree if all its level,
except possibly the last, have the maximum number of
possible nodes, and if all the nodes at the last level appear as
far left as possible.
A
B
D E
C
G H
JFF F F J J
Complete Binary Trees
Extended Binary Trees :
A binary tree T is said to be a 2-tree or an extended binary tree if
each node N has either 0 or 2 children.
An extended binary tree is a binary tree in which all empty binary
sub trees have been replaced by a square node.
The nodes with 2 children are called internal nodes, and the
nodes with 0 children are called external nodes.
Circles are used to represent internal nodes and square are used
represents a external nodes.
A
B
D
C
F
K
G
J
ML
E
IH
Extended Binary TreeBinary Tree
A
B
D
C
E
F
Traversing Binary Trees :
There are three standard ways of traversing a binary tree T with
root R.
1. Preorder
1. Process the root R.
2. Traverse the left sub tree of R in Preorder
3. Traverse the right sub tree of R in Preorder.
2. Inorder
1. Traverse the left sub tree of R in Preorder
2. Process the root R.
3. Traverse the right sub tree of R in Preorder.
3. Postorder
1. Traverse the left sub tree of R in Preorder
2. Traverse the right sub tree of R in Preorder.
3. Process the root R.
Pre order Traversal
Procedure PREORDER (T)
In given binary tree root node address is a pointer variable T
1.[Process the root node]
If T!= NULL
then write (INFO (T))
ELSE
write (“Empty Tree”)
Return
2. [Process the Left sub tree]
If LPTR(T) != NULL
then call PREORDER(LPTR(T))
3. . [Process the Right sub tree]
If RPTR(T) != NULL
then call PREORDER(RPTR(T))
4. [FINISH]
Return
IN order Traversal
Procedure INORDER (T)
In given binary tree root node address is a pointer variable T
1.[Check for empty tree]
If T== NULL
then write (Empty Tree)
Return
2. [Process the Left sub tree]
If LPTR(T) != NULL
then call INORDER(LPTR(T))
3. [Process the root Note]
write (Data (T))
4. [Process the Right sub tree]
If RPTR(T) != NULL
then call INORDER(RPTR(T))
4. [FINISH]
Return
Post order Traversal
Procedure POSTORDER (T)
In given binary tree root node address is a pointer variable T
1.[Check for empty tree]
If T== NULL
then write (Empty Tree)
Return
2. [Process the Left sub tree]
If LPTR(T) != NULL
then call POSTORDER(LPTR(T))
3.[Process the Right sub tree]
If RPTR(T) != NULL
then call POSTORDER(RPTR(T))
4. [Process the root Note]
write (Data (T))
5. [FINISH]
Return
A
B
D E
C
G H
J K
L
F
Preorder Traversal :
ABDEFCGHJLK
Inorder Traversal :
DBFEAGCLJHK
Postorder Traversal :
DFEBGLJKHCA
Binary Search Trees :
Suppose T is binary tree. Then T is called a binary search tree
if each node N of T has the following property:
1. Every element has a key and no two elements have the
same
key i.e. the keys are distinct.
2. The keys in the left sub tree are smaller than the key in the
root.
2. The keys in the right sub tree are larger than the key in the
root.
3. The left and right sub trees are also binary search trees.
38
14
8 23
56
45 82
7018
BST for months : JAN, FEB, MAR, APR, MAY, JUNE, JULY,
AUG, SEPT, OCT, NOV, DEC
JAN
FEB MAR
APR
AUG
DEC
JUNE MAY
JULY SEPT
OCT
NOV
Searching in BST:
FIND ( INFO , LEFT, RIGHT,ROOT, ITEM, LOC,PAR)
1. [Tree empty?]
If ROOT = NULL, then : Set LOC := NULL and PAR :=
NULL, and Return.
2. [ITEM at root?]
IF ITEM = INFO[ROOT], then: Set LOC := ROOT and
PAR := NULL and Return
3. [Initialize pointers PTR and SAVE]
IF ITEM < INFO [ ROOT], then :
Set PTR := LEFT[ROOT] and SAVE := ROOT.
Else
Set PTR := RIGHT[ROOT] and SAVE := ROOT.
4. Repeat steps 5 and 6 while PTR != NULL
5. [Item found?]
IF ITEM = INFO [PTR], then : Set LOC := PTR and
PAR := SAVE and Return
5. IF ITEM < INFO[PTR] then:
Set SAVE := PTR and PTR := LEFT[PTR]
Else:
Set SAVE := PTR and PTR := RIGHT[PTR]
[End of step 4 loop]
5. [Search unsuccessful?]
Set LOC := NULL and PAR := SAVE
5. Exit
Inserting in BST:
INSBST(INFO,LEFT,RIGHT,ROOT,AVAIL,ITEM,LOC)
1. Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
2. If LOC != NULL then Exit.
3. [Copy ITEM into new node in AVAIL list]
1. If AVAIL = NULL then: Write : OVERFLOW, and
Exit
2. Set NEW:=AVAIL, AVAIL := LEFT[AVAIL] and
INFO[NEW] := ITEM
3. Set LOC : =NEW, LEFT[NEW] := NULL and
RIGHT[NEW] := NULL
4. [Add ITEM to tree.]
If PAR = NULL then: Set ROOT := NEW.
Else if ITEM < INFO[PAR], then
Set LEFT[PAR] := New
Else
Set RIGHT[PAR] := NEW
5. Exit
Example :
Suppose the following six numbers are inserted in order into an
empty BST. 40, 60, 50, 33, 55, 11
40
60
50
33
55
11
Deleting node in BST:
Example :
Suppose T is a BST, There are three cases :
1. N has no children. Then N is deleted from T by simply replacing
the location of N in the parent node P(N) by the null pointer.
2. N has exactly one child. Then N is deleted from T by simply
replacing the location of N in P(N) by the location of the only
child N.
3. N has two children. Let S(N) denote the inorder successor of N.
Then N id deleted from T by first deleting S(N) from T & then
replacing node N in T by the node S(N).
60
25
15 50
75
66
33
44
Delete node 44
Delete node 75
Delete node 25
60
25
15 50
75
66
33
60
25
15 50
66
33
60
33
15 50
66
CASEA(INFO,LEFT,RIGHT,ROOT,LOC,PAR)
1. [Initialize CHILD.]
If LEFT[LOC] = NULL and RIGHT[LOC] = NULL, then:
Set CHILD := NULL
Else if LEFT[LOC] != NULL, then:
Set CHILD := LEFT[LOC]
Else
Set CHILD := RIGHT[LOC]
[End of If structure]
2. If PAR != NULL, then:
If LOC = LEFT[PAR] then:
Set LEFT[PAR] := CHILD
Else :
set RIGHT[PAR] := CHILD
[End of If]
Else
Set Root := CHILD
3. Return
CASEB(INFO,LEFT,RIGHT,ROOT,LOC,PAR)
1. [Find SUC and PARSUC]
1. Set PTR := RIGHT[LOC] and SAVE := LOC
2. Repeat while LEFT[PTR] != NULL
Set SAVE := PTR and PTR := LEFT[PTR]
[End of loop]
3. Set SUC : =PTR and PARSUC := SAVE
2. [Delete inorder successor]
Call CASEA(INFO,LEFT,RIGHT,ROOT,SUC,PARSUC)
3. [Replace node N by its inorder successor]
1. If PAR != NULL, then:
If LOC = LEFT[PAR], then:
Set LEFT[PAR] := SUC
Else
Set RIGHT[PAR] := SUC
[End of If]
Else
Set ROOT := SUC
2. Set LEFT[SUC] := LEFT[LOC] &
RIGHT[SUC] := RIGHT[LOC]
4. Return
DEL(INFO,LEFT,RIGHT,ROOT,AVAIL,ITEM)
1. [ Find locations of ITEM and its parent]
Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
2. [Item in tree?]
If LOC = NULL then : Write : ITEM not in tree and Exit
3. [Delete node containing ITEM]
If RIGHT[LOC] != NULL and LEFT[LOC] != NULL, then
Call CASEB(INFO,LEFT,RIGHT,ROOT,LOC,PAR)
Else :
Call CASEA(INFO,LEFT,RIGHT,ROOT,LOC,PAR)
4. [Return deleted node to the AVAIL list]
Set LEFT[LOC] := AVAIL and AVAIL := LOC
5. Exit
Heap Trees :
Suppose H is a complete binary tree with n elements. Then H
is called a heap.
Maxheap
The value at N is greater than or equal to the value at
each of the children of N.
Minheap
The value of N less than or equal to the value at any
of the children of N.
88
66
66 35
55
48 55
3018 40 26 24
Maxheap Trees
Sequential Representation :
88 66 55 66 40 30 26 2435 48 55 18
TREE[1] 2 3 4 5 6 7 8 9 10 11 12
2
7
10 8
4
6 55
1013 14 11 12
Minheap Trees
Sequential Representation :
2 7 4 10 14 10 11 128 6 55 13
TREE[1] 2 3 4 5 6 7 8 9 10 11 12
Example :
Suppose we want to add ITEM = 70 to H.
First we adjoin 70 as the next element in the complete tree, i.e.
TREE[13] = 70.
88
66
66 35
55
48 55
3018 40 26 24
Compare 70 with
its parent 48, since
70 > 48,
interchange 70
and 48
Compare 70 with
its parent 55, since
70 > 55,
interchange 70
and 55
Compare 70 with
its parent 88, since
70 < 88, ITEM has
been risen to its
appropriate palce
in H.
88
66
66 35
55
48 55
3018 40 26 24 70
88
66
66 35
55
70 55
3018 40 26 24 48
88
66
66 35
70
55 55
3018 40 26 24 48
88
66
66 35
70
55 55
3018 40 26 24 48
Suppose we want to build a heap H from the following list of
numbers : 44, 30 ,50, 22, 60, 55, 77, 55
4444
30
44
44 is a first node
30 is a add as
Left child of
44 node.
4444
30
44
30 50 is a add as
Right child of
44 node. But
50 >44, hence 50
Moves upward to
build Maxheap
4444
30
55
30
44
22 is a add as
left child of
30 node.
50
30
44
22
60 is a add as
right child of
30 node. But
60 > 30 moves
Upward, still
60 > 50, moves
upward
60
50
44
22
30
55 is a add as
left child of
44 node. But
55 > 44 moves
Upward
60
50
55
22
30
44
77 is a add as
right child of
55 node. But
77 > 55 moves
Upward, still 77 >60
Moves upward
77
50
60
22
30
44 55
55 is a add as
left child of
22 node. But
55 > 22 moves
Upward, still 55 >50
Moves upward
77
55
60
50
30
44 55
22
Deleting into a Heap :
Suppose H is a heap with N elements, and suppose we want to
delete the root R of H. This is given below:
1. Assign the root R to some variable ITEM.
2. Replace the deleted node R by the last node L of H is still a
complete tree, but not necessarily a heap.
3. (Reheap) Let L sink to its appropriate place in H so that H is
finally a heap.
Steps and Algorithm for deletion are on next slide :
Consider, the heap H, where R=95 is the root and L=22 is the last
node of the tree. Delete 95 and reheap the tree.
Delete the root 95,
Replace it by last
node of heap H
which is 22.
95
85
55 33
70
30 65
1515 20 22
22
85
55 33
70
30 65
1515 20
Compare 22 with
Its two children,
85 and 70. Since 22
is less than the
larger child 85,
Interchange 22 and
85
85
22
55 33
70
30 65
1515 20
Compare 22 with
Its two new
children,
55 and 33. Since 22
is less than the
larger child 55,
Interchange 22 and
55
85
55
22 33
70
30 65
1515 20
Compare 22 with
Its two new
children,
15 and 20. Since 22
is greater than the
both children, node
22 has dropped
To its proper place
in H.
85
55
22 33
70
30 65
1515 20
AVL Search Tree :
1. An empty tree is height-balanced.
2. If T is a nonempty binary tree with TL and TR as its left and
right sub trees respectively, then T is height balanced iff
TL and TR are height balanced.
|hL – hR| <= 1 where hL and hR are the heights of TL
and TR resp
3. hL – hR is known as the balance factor (BF) and for an AVL
tree balance factor of a node can be 0, 1, or -1.
An AVL search tree is a binary search tree which is an AVL
tree.
Representation of an AVL Search Tree :
Following shows the representation of an AVL search tree. The
number against each node represents its balance factor.
C
A G
D
(0)
(0)
(1)
(-1)
Insertion into AVL Search Tree
• Insert element into AVL Search Tree in its first phase is
similar to that one used in binary search tree.
• If after insertion of element, the balance factor of any node in
the tree is affected, then binary search tree unbalanced, we
resort to techniques called Rotations to restore the balance of
the search tree.
• There are four types of rotations based on the position of the
inserted node with reference to A.
1. LL Rotation : Inserted node is in the left sub tree of left
sub tree of node A. Single rotation
2. RR Rotation : Inserted node is in the right sub tree of the right
sub tree of node A. Single rotation
3. LR Rotation: Inserted node is in the right sub tree of left
sub tree of node A. Double rotation, LR can be
RR followed by LL rotation.
4. RL Rotation : Inserted node is in the left sub tree of right sub
tree of node A. Double rotation, RL can be LL
followed by RR rotation.
1. LL Rotation :
• The new element X is inserted in the left sub tree of left sub tree
of A, the closest node whose BF(A) becomes +2 after
insertion.
• To Rebalance the search tree, it is rotated so as to allow B to be
the root with BL and A to be its left sub tree and right child. And
BR and AR to be the left and right sub trees of A.
BL : Left child of B
BR : Right child of B
AR : Right child of A
Insert 36 into Fig. a AVL search tree. Fig. b shows before insertion
and Fig. c shows after insertion
96
85 110
64 90
(0)(0)
(0)(0)
(1)
Fig. a
96
85 110
64 90
(0)B (1)
(0)(1)
A (2)
Fig. b
85
64 96
36 110
A (0)(1)
(0)(0)
B (0)
Fig. c
36
(0)
LL Rotation
90
(0)
2. RR Rotation :
• The new element X is inserted in the right sub tree of right sub
tree of A, the closest node whose BF(A) becomes -2 after
insertion.
• To Rebalance the search tree, it is rotated so as to allow B to be
the root with A is left child and BR to be its right sub tree and
AL and BL as the left and right sub trees of A.
BL : Left child of B
BR : Right child of B
AR : Right child of A
Insert 65 into Fig. a AVL search tree. Fig. b shows before insertion
and Fig. c shows after insertion
34
26 44
40 56
(0)(0)
(0)(0)
(-1)
Fig. a
Fig. b
34
26 44
40 56
B (-1)(0)
(-1)(0)
A (-2)
65
(0)
Fig. c
44
34 56
26 65
(-1)A (0)
(0)(0)
B (0)
40
(0)RR Rotation
3. LR Rotation :
• In LR rotation, RR rotation followed by LL rotation, the BF
values of nodes A and B after balancing are dependent on the
BF values of node C after insertion.
• If BF(C) = 0 after insertion then BF(A) = BF(B) = 0, after
rotation
• If BF(C) = -1 after insertion then BF(A) = 0, BF(B) = 1, after
rotation
• If BF(C) = 1 after insertion the BF(A) = -1, BF(B) = 0, after
rotation.
Insert 37 into Fig. a AVL search tree. Fig. b shows before insertion
and Fig. c shows after insertion
44
30 76
16 39
(0)(0)
(0)(0)
(-1)
Fig. a
44
30 76
16 39
(0)B (-1)
C (1)(0)
A (2)
Fig. b
37
(0)
Fig. c
39
30 44
40 37
A (-1)B (0)
(0)(0)
C (0)
76
(0)
LR Rotation
3. RL Rotation :
• In RL rotation, LL rotation followed by RR rotation, the BF
values of nodes A and B after balancing are dependent on the
BF values of node C after insertion.
44
30 76
60 80
(0)(0)
(0)(0)
(-1)
Fig. a
44
30 76
60 80
(0)(0)
(0)(0)
(-1)
Fig. a
Construct an AVL search tree by inserting the following elements
in the order of their occurrence.
64, 1, 44, 26, 13, 110, 98, 85
LR Rotation
Insert 64, 1
64
1
(0)
(1)
Insert 14
64
1
14
B (-1)
C (0)
A (2)
14
1 64
B (0) A (0)
C (0)
Insert 26, 13, 110, 98, 85
14
1 64
(-1) (-2)
(-2)
13
(0)
110
A (2)
26
(0)
98
B (1)
85
(0)
LL Rotation
14
1 64
(-1) (-1)
(-1)
13
(0)
98
A (0)
26
(0)
85
B (0)
110
(0)
Expression Tree :
Expression tree is a binary tree, because all of the operations are
binary.
It is also possible for a node to have only one child, as is the
case with the unary minus operator.
The leaves of an expression tree are operands, such as constants
or variable names.
The other (non leaf) nodes contain operators.
Construct an expression tree for the arithmetic expression:
(A + B * C) – ((D * E + F) / G)
Solution:
Unit 4   tree

More Related Content

PPTX
Binary Search Tree
PPTX
Red black trees
PPTX
Tree Traversal
PPTX
Multi ways trees
PDF
Binary tree
PPTX
Binary trees1
PPTX
Multistage graph unit 4 of algorithm.ppt
PPTX
Heap tree
Binary Search Tree
Red black trees
Tree Traversal
Multi ways trees
Binary tree
Binary trees1
Multistage graph unit 4 of algorithm.ppt
Heap tree

What's hot (20)

PPTX
Data Structure and Algorithms Merge Sort
PPTX
Searching and sorting
PPTX
Binary Search Tree in Data Structure
PPTX
Breadth First Search & Depth First Search
PPTX
Abstract Data Types
PPT
Doubly linked list
PPTX
PDF
Binary Search - Design & Analysis of Algorithms
PPTX
DBMS Keys
PPTX
AVL Tree in Data Structure
PDF
Data Structure & Algorithms - Operations
PPTX
Trees in data structures
PPTX
Circular link list.ppt
PPT
Binary Search
PPTX
10.m way search tree
PDF
03 Linear Arrays Memory Representations .pdf
PPTX
PPTX
Depth-First Search
PDF
Searching and Sorting Techniques in Data Structure
Data Structure and Algorithms Merge Sort
Searching and sorting
Binary Search Tree in Data Structure
Breadth First Search & Depth First Search
Abstract Data Types
Doubly linked list
Binary Search - Design & Analysis of Algorithms
DBMS Keys
AVL Tree in Data Structure
Data Structure & Algorithms - Operations
Trees in data structures
Circular link list.ppt
Binary Search
10.m way search tree
03 Linear Arrays Memory Representations .pdf
Depth-First Search
Searching and Sorting Techniques in Data Structure
Ad

Similar to Unit 4 tree (20)

PPT
ds 10-Binary Tree.ppt
PPTX
PPT
PDF
Lecture notes data structures tree
PPT
Binary Search Tree
PPTX
Binary Search Tree.pptx
PPTX
Binary Trees.pptx module 122img 787554yau
PPT
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
PPT
Data Structure And Algorithms for Computer Science
PPTX
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
PPTX
Tree structure and its definitions with an example
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPTX
UNIT III Non Linear Data Structures - Trees.pptx
PPT
Data Structure and Algorithms Binary Tree
DOCX
Chapter 4
PPTX
Tree.pptx
PPT
Algorithm and Data Structure - Binary Trees
PPTX
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
PPTX
NON-LINEAR DATA STRUCTURE-TREES.pptx
PPT
BINARY SEARCH TREE
ds 10-Binary Tree.ppt
Lecture notes data structures tree
Binary Search Tree
Binary Search Tree.pptx
Binary Trees.pptx module 122img 787554yau
bst-class-220902051152-cdddddddddddddddddd5e6c70f.ppt
Data Structure And Algorithms for Computer Science
UNIT 2 TREES & GRAPH COMPLETE NOTES OF DATA STRUCTURE
Tree structure and its definitions with an example
UNIT III Non Linear Data Structures - Trees.pptx
UNIT III Non Linear Data Structures - Trees.pptx
Data Structure and Algorithms Binary Tree
Chapter 4
Tree.pptx
Algorithm and Data Structure - Binary Trees
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
NON-LINEAR DATA STRUCTURE-TREES.pptx
BINARY SEARCH TREE
Ad

More from kalyanineve (7)

PPSX
Sorting and searching
PPT
Unit 7 sorting
PPT
Unit 5 graphs minimum spanning trees
PPT
Unit 3 stack
PPT
Unit 2 linked list and queues
PPT
Unit 1 introduction to data structure
PPT
Linux command ppt
Sorting and searching
Unit 7 sorting
Unit 5 graphs minimum spanning trees
Unit 3 stack
Unit 2 linked list and queues
Unit 1 introduction to data structure
Linux command ppt

Recently uploaded (20)

PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
737-MAX_SRG.pdf student reference guides
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Artificial Intelligence
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPT
Project quality management in manufacturing
PDF
Categorization of Factors Affecting Classification Algorithms Selection
PPTX
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
PPT
Total quality management ppt for engineering students
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PPT
introduction to datamining and warehousing
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
Current and future trends in Computer Vision.pptx
PPTX
Fundamentals of Mechanical Engineering.pptx
PDF
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems
Foundation to blockchain - A guide to Blockchain Tech
Internet of Things (IOT) - A guide to understanding
737-MAX_SRG.pdf student reference guides
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Artificial Intelligence
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Project quality management in manufacturing
Categorization of Factors Affecting Classification Algorithms Selection
6ME3A-Unit-II-Sensors and Actuators_Handouts.pptx
Total quality management ppt for engineering students
III.4.1.2_The_Space_Environment.p pdffdf
introduction to datamining and warehousing
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
Current and future trends in Computer Vision.pptx
Fundamentals of Mechanical Engineering.pptx
Level 2 – IBM Data and AI Fundamentals (1)_v1.1.PDF
Human-AI Collaboration: Balancing Agentic AI and Autonomy in Hybrid Systems

Unit 4 tree

  • 2. Trees : A tree is non-linear data structure in which items are arranged in a sorted sequence. It is used to represent hierarchical relationship existing amongst several data items. 1. There is a special data item called the root of the tree. 2. Its remaining data items are partitioned into number of mutually exclusive subset, each of which is itself a tree. And they are called sub trees.
  • 3. Degree of node : It is the number of sub trees of a node in a given tree. Degree of a tree : It is the maximum degree of nodes in a given tree. Terminal Node : A node with degree 0 is called a terminal node or leaf. Siblings : The children nodes of a given parent node are called siblings. They are also called brothers. Level: The entire tree structure is leveled in such a way that the root is always at level 1. then, its immediate children are at level 2 and their immediate children are at level 3 so on. Path : It is a sequence of consecutive edges from the source node to the destination node.
  • 4. Depth: It is the maximum level of any node in a given tree. Forest: It is set of disjoint trees. Binary Trees : A binary tree T is defined as a finite set element, called nodes, such that a) T is empty or b) T contains a distinguished node R, called the root T, and the remaining nodes of T form an ordered pair of disjoint binary trees T1 and T2.
  • 5. A B D E C G H J K L F Binary Trees
  • 6. Properties of Binary Trees : 1. The maximum number of nodes on level i of a binary tree is 2i-1 , i>=1. 2. The maximum number of nodes in a binary tree of depth k is 2k -1, k>=1.
  • 7. Representation of binary tree in memory There are two ways of representing T in memory, 1.Static or Array or Sequential Representation 2.Dynamic or Linked Representation
  • 8. 1. Static or Sequential Representation: This representation uses a single array TREE as follows: a. The root R of T is stores in TREE[1]. b. If a node N occupies TREE[k], then its left child is stored in TREE[2*k] and its right child is stored in TREE[2*k+1]. The sequential representation of the binary tree T in fig. (a) appears in fig. (b).
  • 9. 38 14 56 8 23 82 45 18 70 Fig. (a) 38 14 56 8 23 45 82 18 70 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Tree Fig. (b)
  • 10. 2. Dynamic or Linked Representation: Each node N of T will divided into three parts: INFO[K] contains the data at the node N. LEFT[K] contains the location of the left child of node N RIGHT[K] contains the location of the right child of node N LEFT INFO RIGHT
  • 11. 38 14 56 8 23 18 45 82 70 Representation of binary tree in memory
  • 12. Full Binary Tree : A full binary tree of depth k is a binary tree of depth k having exactly 2k -1 nodes, k >= 0; Figure shows a full binary tree of depth 4.
  • 13. A B D E C F G NJH I K L M Full Binary Trees O
  • 14. Complete Binary Trees: Each node of T can have at most two children. According, one can show that level r of T can have at most 2r nodes. The tree T is said to be Complete binary tree if all its level, except possibly the last, have the maximum number of possible nodes, and if all the nodes at the last level appear as far left as possible.
  • 15. A B D E C G H JFF F F J J Complete Binary Trees
  • 16. Extended Binary Trees : A binary tree T is said to be a 2-tree or an extended binary tree if each node N has either 0 or 2 children. An extended binary tree is a binary tree in which all empty binary sub trees have been replaced by a square node. The nodes with 2 children are called internal nodes, and the nodes with 0 children are called external nodes. Circles are used to represent internal nodes and square are used represents a external nodes.
  • 18. Traversing Binary Trees : There are three standard ways of traversing a binary tree T with root R. 1. Preorder 1. Process the root R. 2. Traverse the left sub tree of R in Preorder 3. Traverse the right sub tree of R in Preorder. 2. Inorder 1. Traverse the left sub tree of R in Preorder 2. Process the root R. 3. Traverse the right sub tree of R in Preorder. 3. Postorder 1. Traverse the left sub tree of R in Preorder 2. Traverse the right sub tree of R in Preorder. 3. Process the root R.
  • 19. Pre order Traversal Procedure PREORDER (T) In given binary tree root node address is a pointer variable T 1.[Process the root node] If T!= NULL then write (INFO (T)) ELSE write (“Empty Tree”) Return 2. [Process the Left sub tree] If LPTR(T) != NULL then call PREORDER(LPTR(T)) 3. . [Process the Right sub tree] If RPTR(T) != NULL then call PREORDER(RPTR(T)) 4. [FINISH] Return
  • 20. IN order Traversal Procedure INORDER (T) In given binary tree root node address is a pointer variable T 1.[Check for empty tree] If T== NULL then write (Empty Tree) Return 2. [Process the Left sub tree] If LPTR(T) != NULL then call INORDER(LPTR(T)) 3. [Process the root Note] write (Data (T)) 4. [Process the Right sub tree] If RPTR(T) != NULL then call INORDER(RPTR(T)) 4. [FINISH] Return
  • 21. Post order Traversal Procedure POSTORDER (T) In given binary tree root node address is a pointer variable T 1.[Check for empty tree] If T== NULL then write (Empty Tree) Return 2. [Process the Left sub tree] If LPTR(T) != NULL then call POSTORDER(LPTR(T)) 3.[Process the Right sub tree] If RPTR(T) != NULL then call POSTORDER(RPTR(T)) 4. [Process the root Note] write (Data (T)) 5. [FINISH] Return
  • 22. A B D E C G H J K L F Preorder Traversal : ABDEFCGHJLK Inorder Traversal : DBFEAGCLJHK Postorder Traversal : DFEBGLJKHCA
  • 23. Binary Search Trees : Suppose T is binary tree. Then T is called a binary search tree if each node N of T has the following property: 1. Every element has a key and no two elements have the same key i.e. the keys are distinct. 2. The keys in the left sub tree are smaller than the key in the root. 2. The keys in the right sub tree are larger than the key in the root. 3. The left and right sub trees are also binary search trees.
  • 25. BST for months : JAN, FEB, MAR, APR, MAY, JUNE, JULY, AUG, SEPT, OCT, NOV, DEC JAN FEB MAR APR AUG DEC JUNE MAY JULY SEPT OCT NOV
  • 26. Searching in BST: FIND ( INFO , LEFT, RIGHT,ROOT, ITEM, LOC,PAR) 1. [Tree empty?] If ROOT = NULL, then : Set LOC := NULL and PAR := NULL, and Return. 2. [ITEM at root?] IF ITEM = INFO[ROOT], then: Set LOC := ROOT and PAR := NULL and Return 3. [Initialize pointers PTR and SAVE] IF ITEM < INFO [ ROOT], then : Set PTR := LEFT[ROOT] and SAVE := ROOT. Else Set PTR := RIGHT[ROOT] and SAVE := ROOT. 4. Repeat steps 5 and 6 while PTR != NULL
  • 27. 5. [Item found?] IF ITEM = INFO [PTR], then : Set LOC := PTR and PAR := SAVE and Return 5. IF ITEM < INFO[PTR] then: Set SAVE := PTR and PTR := LEFT[PTR] Else: Set SAVE := PTR and PTR := RIGHT[PTR] [End of step 4 loop] 5. [Search unsuccessful?] Set LOC := NULL and PAR := SAVE 5. Exit
  • 28. Inserting in BST: INSBST(INFO,LEFT,RIGHT,ROOT,AVAIL,ITEM,LOC) 1. Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) 2. If LOC != NULL then Exit. 3. [Copy ITEM into new node in AVAIL list] 1. If AVAIL = NULL then: Write : OVERFLOW, and Exit 2. Set NEW:=AVAIL, AVAIL := LEFT[AVAIL] and INFO[NEW] := ITEM 3. Set LOC : =NEW, LEFT[NEW] := NULL and RIGHT[NEW] := NULL
  • 29. 4. [Add ITEM to tree.] If PAR = NULL then: Set ROOT := NEW. Else if ITEM < INFO[PAR], then Set LEFT[PAR] := New Else Set RIGHT[PAR] := NEW 5. Exit
  • 30. Example : Suppose the following six numbers are inserted in order into an empty BST. 40, 60, 50, 33, 55, 11 40 60 50 33 55 11
  • 31. Deleting node in BST: Example : Suppose T is a BST, There are three cases : 1. N has no children. Then N is deleted from T by simply replacing the location of N in the parent node P(N) by the null pointer. 2. N has exactly one child. Then N is deleted from T by simply replacing the location of N in P(N) by the location of the only child N. 3. N has two children. Let S(N) denote the inorder successor of N. Then N id deleted from T by first deleting S(N) from T & then replacing node N in T by the node S(N).
  • 32. 60 25 15 50 75 66 33 44 Delete node 44 Delete node 75 Delete node 25 60 25 15 50 75 66 33 60 25 15 50 66 33 60 33 15 50 66
  • 33. CASEA(INFO,LEFT,RIGHT,ROOT,LOC,PAR) 1. [Initialize CHILD.] If LEFT[LOC] = NULL and RIGHT[LOC] = NULL, then: Set CHILD := NULL Else if LEFT[LOC] != NULL, then: Set CHILD := LEFT[LOC] Else Set CHILD := RIGHT[LOC] [End of If structure] 2. If PAR != NULL, then: If LOC = LEFT[PAR] then: Set LEFT[PAR] := CHILD Else : set RIGHT[PAR] := CHILD [End of If] Else Set Root := CHILD 3. Return
  • 34. CASEB(INFO,LEFT,RIGHT,ROOT,LOC,PAR) 1. [Find SUC and PARSUC] 1. Set PTR := RIGHT[LOC] and SAVE := LOC 2. Repeat while LEFT[PTR] != NULL Set SAVE := PTR and PTR := LEFT[PTR] [End of loop] 3. Set SUC : =PTR and PARSUC := SAVE 2. [Delete inorder successor] Call CASEA(INFO,LEFT,RIGHT,ROOT,SUC,PARSUC)
  • 35. 3. [Replace node N by its inorder successor] 1. If PAR != NULL, then: If LOC = LEFT[PAR], then: Set LEFT[PAR] := SUC Else Set RIGHT[PAR] := SUC [End of If] Else Set ROOT := SUC 2. Set LEFT[SUC] := LEFT[LOC] & RIGHT[SUC] := RIGHT[LOC] 4. Return
  • 36. DEL(INFO,LEFT,RIGHT,ROOT,AVAIL,ITEM) 1. [ Find locations of ITEM and its parent] Call FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) 2. [Item in tree?] If LOC = NULL then : Write : ITEM not in tree and Exit 3. [Delete node containing ITEM] If RIGHT[LOC] != NULL and LEFT[LOC] != NULL, then Call CASEB(INFO,LEFT,RIGHT,ROOT,LOC,PAR)
  • 37. Else : Call CASEA(INFO,LEFT,RIGHT,ROOT,LOC,PAR) 4. [Return deleted node to the AVAIL list] Set LEFT[LOC] := AVAIL and AVAIL := LOC 5. Exit
  • 38. Heap Trees : Suppose H is a complete binary tree with n elements. Then H is called a heap. Maxheap The value at N is greater than or equal to the value at each of the children of N. Minheap The value of N less than or equal to the value at any of the children of N.
  • 39. 88 66 66 35 55 48 55 3018 40 26 24 Maxheap Trees Sequential Representation : 88 66 55 66 40 30 26 2435 48 55 18 TREE[1] 2 3 4 5 6 7 8 9 10 11 12
  • 40. 2 7 10 8 4 6 55 1013 14 11 12 Minheap Trees Sequential Representation : 2 7 4 10 14 10 11 128 6 55 13 TREE[1] 2 3 4 5 6 7 8 9 10 11 12
  • 41. Example : Suppose we want to add ITEM = 70 to H. First we adjoin 70 as the next element in the complete tree, i.e. TREE[13] = 70. 88 66 66 35 55 48 55 3018 40 26 24 Compare 70 with its parent 48, since 70 > 48, interchange 70 and 48 Compare 70 with its parent 55, since 70 > 55, interchange 70 and 55 Compare 70 with its parent 88, since 70 < 88, ITEM has been risen to its appropriate palce in H. 88 66 66 35 55 48 55 3018 40 26 24 70 88 66 66 35 55 70 55 3018 40 26 24 48 88 66 66 35 70 55 55 3018 40 26 24 48 88 66 66 35 70 55 55 3018 40 26 24 48
  • 42. Suppose we want to build a heap H from the following list of numbers : 44, 30 ,50, 22, 60, 55, 77, 55 4444 30 44 44 is a first node 30 is a add as Left child of 44 node. 4444 30 44 30 50 is a add as Right child of 44 node. But 50 >44, hence 50 Moves upward to build Maxheap 4444 30 55 30 44 22 is a add as left child of 30 node. 50 30 44 22 60 is a add as right child of 30 node. But 60 > 30 moves Upward, still 60 > 50, moves upward 60 50 44 22 30 55 is a add as left child of 44 node. But 55 > 44 moves Upward 60 50 55 22 30 44 77 is a add as right child of 55 node. But 77 > 55 moves Upward, still 77 >60 Moves upward 77 50 60 22 30 44 55 55 is a add as left child of 22 node. But 55 > 22 moves Upward, still 55 >50 Moves upward 77 55 60 50 30 44 55 22
  • 43. Deleting into a Heap : Suppose H is a heap with N elements, and suppose we want to delete the root R of H. This is given below: 1. Assign the root R to some variable ITEM. 2. Replace the deleted node R by the last node L of H is still a complete tree, but not necessarily a heap. 3. (Reheap) Let L sink to its appropriate place in H so that H is finally a heap. Steps and Algorithm for deletion are on next slide :
  • 44. Consider, the heap H, where R=95 is the root and L=22 is the last node of the tree. Delete 95 and reheap the tree. Delete the root 95, Replace it by last node of heap H which is 22. 95 85 55 33 70 30 65 1515 20 22 22 85 55 33 70 30 65 1515 20 Compare 22 with Its two children, 85 and 70. Since 22 is less than the larger child 85, Interchange 22 and 85 85 22 55 33 70 30 65 1515 20 Compare 22 with Its two new children, 55 and 33. Since 22 is less than the larger child 55, Interchange 22 and 55 85 55 22 33 70 30 65 1515 20 Compare 22 with Its two new children, 15 and 20. Since 22 is greater than the both children, node 22 has dropped To its proper place in H. 85 55 22 33 70 30 65 1515 20
  • 45. AVL Search Tree : 1. An empty tree is height-balanced. 2. If T is a nonempty binary tree with TL and TR as its left and right sub trees respectively, then T is height balanced iff TL and TR are height balanced. |hL – hR| <= 1 where hL and hR are the heights of TL and TR resp 3. hL – hR is known as the balance factor (BF) and for an AVL tree balance factor of a node can be 0, 1, or -1. An AVL search tree is a binary search tree which is an AVL tree.
  • 46. Representation of an AVL Search Tree : Following shows the representation of an AVL search tree. The number against each node represents its balance factor. C A G D (0) (0) (1) (-1)
  • 47. Insertion into AVL Search Tree • Insert element into AVL Search Tree in its first phase is similar to that one used in binary search tree. • If after insertion of element, the balance factor of any node in the tree is affected, then binary search tree unbalanced, we resort to techniques called Rotations to restore the balance of the search tree. • There are four types of rotations based on the position of the inserted node with reference to A.
  • 48. 1. LL Rotation : Inserted node is in the left sub tree of left sub tree of node A. Single rotation 2. RR Rotation : Inserted node is in the right sub tree of the right sub tree of node A. Single rotation 3. LR Rotation: Inserted node is in the right sub tree of left sub tree of node A. Double rotation, LR can be RR followed by LL rotation. 4. RL Rotation : Inserted node is in the left sub tree of right sub tree of node A. Double rotation, RL can be LL followed by RR rotation.
  • 49. 1. LL Rotation : • The new element X is inserted in the left sub tree of left sub tree of A, the closest node whose BF(A) becomes +2 after insertion. • To Rebalance the search tree, it is rotated so as to allow B to be the root with BL and A to be its left sub tree and right child. And BR and AR to be the left and right sub trees of A. BL : Left child of B BR : Right child of B AR : Right child of A
  • 50. Insert 36 into Fig. a AVL search tree. Fig. b shows before insertion and Fig. c shows after insertion 96 85 110 64 90 (0)(0) (0)(0) (1) Fig. a
  • 51. 96 85 110 64 90 (0)B (1) (0)(1) A (2) Fig. b 85 64 96 36 110 A (0)(1) (0)(0) B (0) Fig. c 36 (0) LL Rotation 90 (0)
  • 52. 2. RR Rotation : • The new element X is inserted in the right sub tree of right sub tree of A, the closest node whose BF(A) becomes -2 after insertion. • To Rebalance the search tree, it is rotated so as to allow B to be the root with A is left child and BR to be its right sub tree and AL and BL as the left and right sub trees of A. BL : Left child of B BR : Right child of B AR : Right child of A
  • 53. Insert 65 into Fig. a AVL search tree. Fig. b shows before insertion and Fig. c shows after insertion 34 26 44 40 56 (0)(0) (0)(0) (-1) Fig. a
  • 54. Fig. b 34 26 44 40 56 B (-1)(0) (-1)(0) A (-2) 65 (0) Fig. c 44 34 56 26 65 (-1)A (0) (0)(0) B (0) 40 (0)RR Rotation
  • 55. 3. LR Rotation : • In LR rotation, RR rotation followed by LL rotation, the BF values of nodes A and B after balancing are dependent on the BF values of node C after insertion. • If BF(C) = 0 after insertion then BF(A) = BF(B) = 0, after rotation • If BF(C) = -1 after insertion then BF(A) = 0, BF(B) = 1, after rotation • If BF(C) = 1 after insertion the BF(A) = -1, BF(B) = 0, after rotation.
  • 56. Insert 37 into Fig. a AVL search tree. Fig. b shows before insertion and Fig. c shows after insertion 44 30 76 16 39 (0)(0) (0)(0) (-1) Fig. a
  • 57. 44 30 76 16 39 (0)B (-1) C (1)(0) A (2) Fig. b 37 (0) Fig. c 39 30 44 40 37 A (-1)B (0) (0)(0) C (0) 76 (0) LR Rotation
  • 58. 3. RL Rotation : • In RL rotation, LL rotation followed by RR rotation, the BF values of nodes A and B after balancing are dependent on the BF values of node C after insertion.
  • 61. Construct an AVL search tree by inserting the following elements in the order of their occurrence. 64, 1, 44, 26, 13, 110, 98, 85 LR Rotation Insert 64, 1 64 1 (0) (1) Insert 14 64 1 14 B (-1) C (0) A (2) 14 1 64 B (0) A (0) C (0)
  • 62. Insert 26, 13, 110, 98, 85 14 1 64 (-1) (-2) (-2) 13 (0) 110 A (2) 26 (0) 98 B (1) 85 (0) LL Rotation 14 1 64 (-1) (-1) (-1) 13 (0) 98 A (0) 26 (0) 85 B (0) 110 (0)
  • 63. Expression Tree : Expression tree is a binary tree, because all of the operations are binary. It is also possible for a node to have only one child, as is the case with the unary minus operator. The leaves of an expression tree are operands, such as constants or variable names. The other (non leaf) nodes contain operators.
  • 64. Construct an expression tree for the arithmetic expression: (A + B * C) – ((D * E + F) / G) Solution: