SlideShare a Scribd company logo
1 of 80
SHEETAL WAGHMARE
M.TECH (Computer Science & Data Processing)
IIT KHARAGPUR
EMAIL-ID: shitu2iitkgp@gmail.com
sheetalw3@gmail.com
TREE
Instruction To Use This Presentation
 Dear user I have kept some animated/executable/video
files, so it will be easy for you to understand but that will
not run/play in slideshow
To see/run that files you have to exit from slideshow then
click on the icon and also install KM Player
For example: click on the below icons one by one( one is
executable file and other is video) you will be able to see
the animation
begin.exe op addatbegin.swf
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Classification Of Data Structure
Linear Data Structure
• Array
• Linked List
• Stack
• Queue
Non-Linear Data Structure
• Tree
• Graph
SHEETAL WAGHMARE FROM IIT KHARAGPUR
What is Tree??????
Recursive definition, a tree
is a collection of n nodes, one
of which is the root, and n - 1
edges.
Having n - 1 edges follows
from the fact that each edge
connects
some node to its parent, and
every node except the root
has one
parent
SHEETAL WAGHMARE FROM IIT KHARAGPUR
General Tree
A tree can be represented
as
Arrows that point
downward are first_child
pointers
 Arrows that go left to
right are next_sibling
pointers
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Trees
 A connected
structure which
• is either empty OR
• has a designated
node called root,
from which
descends 0,1 or more
subtrees which are
also Trees.
A
C
JIHGF
B
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Terminologies
• Finite number of elements called nodes,& finite set of
directed lines called branches, that connect the nodes.
• Branch directed towards a node is an In-degree Branch.
• Branch directed away from the node is Out-degree Branch.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Continued...
• The node with out-degree Zero(0) is known as leaf or a
terminal node.
• Sum of number of In-degree and Out-degree node branches =
degree of node
• If Tree is not empty the first node is called as root.
• Nodes that are neither root nor a leaf are known as Internal
nodes.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
• Node that has subtrees is the parent of the roots of the
subtrees.
• Roots of these subtrees are children of the Node.
• Children of the Same Parent are Siblings.
• Ancestors of a node are all the nodes along the path from the
root to the node.
Continued...
SHEETAL WAGHMARE FROM IIT KHARAGPUR
• Path is a sequence of nodes in which each node is adjacent to
the next one.
• Level of a node is its distance from the root.
• Root is at level Zero(0).
• Height(depth) of the Tree is the level of the leaf in the longest
path from the root.
Continued...
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Example of a Tree
• No. of nodes = 9
• Height = 4
• Highest Level = 3
• Root Node = 8
• Leaves = 1,4,7,13
• Interior Nodes = 3,10,6,14
• Ancestors Of 6 = 3,8
• Descendents Of 10 = 14,13
• Sibling Of 1 = 6
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Binary Trees
 A finite set of vertices that is either
• Empty.
OR
• Consists of a root node together with two binary subtree
which are disjoint from each other, and from the root and are
called the left and right subtree
SHEETAL WAGHMARE FROM IIT KHARAGPUR
 Maximum no.of nodes at any level is 2i
 Maximum no.of nodes at height h is 2h
-1
 Height=level + 1
 log2 (n+1)< height <= 2h
-1
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Full Binary Tree and Complete Binary Tree
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Example
A
B
C D
H
E
L
G
K
J
F
I
SHEETAL WAGHMARE FROM IIT KHARAGPUR
BT Traversal
• Process of visiting each of the nodes in a tree and examine
the value there.
• Two kinds
• Depth-First Tree Traversal.
• Breadth-First Tree Traversal.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Depth First Tree Traversal
• Pre-Order : Root first, Left child, Right child.
• Post-Order : Left child, Right child, Root.
• In-Order : Left child, Root, Right child.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Algorithm(Pre-order)
• preorder(tree↑){
▫ if tree ≠ NULL then
 process tree↑.data ;
 preorder (tree↑.left) ;
 preorder(tree↑.right) ;
▫ return.
}
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLE
A
CB
GED F
Pre-order traversal:
A B D E C F G
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Pre-Order : Root first, Left child, Right child.
Rotated node will
be taken first
Algorithm(Post-order)
• postorder(tree↑){
▫ if tree ≠ NULL then
 postorder (tree↑.left) ;
 postorder(tree↑.right) ;
 process tree↑.data ;
▫ return.
}
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLE
A
B C
ED F
IHG
Post-order traversal:
G D H I E B F C
A
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Post-Order : Left Child, Right child, root.
Rotated node will
be taken first
Algorithm(In-order)
• inorder(tree↑){
▫ if tree ≠ NULL then
 inorder (tree↑.left) ;
 process tree↑.data ;
 inorder(tree↑.right) ;
▫ return.
}
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLE
A
D
CB
E F H
I
J
In-order traversal:
D B I E A F C J H
SHEETAL WAGHMARE FROM IIT KHARAGPUR
In-Order : Left child, Root, Right child.
Rotated node will
be taken first
Algorithm breadth-first-traversal(tree)
 Createqueue(Q)
 walkerptr←tree
 while (walkerptr ≠ NULL)
process walkerptr
if (walkerptr ↑.left ≠ NULL)
Enqueue (Q,walkerptr ↑.left)
if(walkerptr ↑.right ≠ NULL)
Enqueue (Q,walkerptr ↑.right)
if (Not (empty(Q))
Dequeue (Q,walkerptr)
else walkerptr = NULL
• return
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLE
A
C
B
F GED
Breadth First Traversal:
A B C D E F G
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Rotated node will
be taken first
Binary Search Tree(Motivation)
• Insertion and Deletion in the middle of the array is very inefficient.
• Search Operation in an ordered linked lists is inefficient.
• To get the best of both, Binary Search Tree(BST) was introduced.
• The binary search tree is widely used data-structure for
information storage and retrieval as it supports many dynamic-set
operations including Search, Minimum, Maximum, Predecessor,
Successor, Insert and Delete.*
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Definition(BST)
A binary search tree T for a set of keys from a total order is a
binary tree in which each node has a key value and all the keys of
the left subtree are less than the key at the root and all the keys of
the right subtree are greater than the key at the root. This property
holding recursively for the left and right subtrees of the tree T.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Example:
All<kA All≥kk
k
20
10 30
3525128
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Operations on BST
• Traversal
• Searching
• Insertion
• Deletion
SHEETAL WAGHMARE FROM IIT KHARAGPUR
BST Operation (Searching)
 Find Smallest element
 Find Largest element
 Find Requested element
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Algorithm findsmallest (bst)
1. if (bst ↑.left = NULL)
then return (bst ↑.data)
1. else
return (findsmallest ( bst ↑.left))
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLE
50
40 60
20 55 6545
10
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Algorithm findlargest (bst)
1. if (bst ↑.right = NULL)
then return (bst ↑.data)
2. else
return (findlargest (bst ↑.right))
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLE
50
45
40
42
75
80
90
85
30
70
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Algorithm search(bst, data)
1. if bst = NULL
then return NULL
2. else
if (data < bst ↑.data)
search (bst ↑.left , data)
elseif (data > bst ↑.data)
search ( bst ↑.right , data)
3. else return (bst ↑.data)
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLE
50
40
70554530
60
45<50
45>40
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Search 45
BST Operation-Insertion
• method insert(key)
▫ Based on comparisons of new data and the values of nodes
in BST.
▫ To insert follow the branches to an empty subtree and then
insert the new node.
▫ all inserts take place at a leaf or at a leaf like node – a node
that has only one null subtree.
• Elements in nodes must be comparable!
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Algorithm
Algorithm insert_bst(bst,data);
1) if bst = NULL;
A) then
a) new(P) ;
b) P↑.data←data ;
c) P↑.left←NULL ;
d) P↑.right←NULL ;
e) bst←P ;
f) return bst .
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Continued…
B) Else
a) if (data < bst ↑.data)
then insert_bst (bst ↑.left,data)
b) if (data ≥ bst ↑.data)
then insert_bst(bst ↑.right,data)
C) return.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
INSERTION IN BST
50
40
70554530
60
42<50
42>40
42
42<45
&
45->left==NULL
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Insert 42
BST Operation-Deletion
 removes a specified item from the BST and adjusts the tree.
 uses a binary search to locate the target items.
 starting at the root it probes down the tree till it finds the
target or reaches a leaf node (target not in the tree)
 removal of a node must not leave a ‘gap’ in the tree.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Algorithm Deletion
• Algorithm delete_bst (bst , data)
1) if bst = NULL
then return NULL
2) if (data < bst ↑.data)
then return delete_bst (bst ↑.left, data)
3) elseif (data > bst ↑.data)
then return delete_bst (bst ↑.right, data)
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Continued…
4) Else
// found the node to be deleted & test whether it’s a leaf
node.
a) if (bst ↑.left = NULL)
a.1) dataptr←bst
a.2) bst←bst ↑.right
a.3) free(dataptr)
a.4) return ;
b) elseif ( bst ↑.right =NULL)
b.1) dataptr←bst
b.2) bst←bst ↑.left
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Continued…
b.3) free(dataptr)
b.4) return
c) else
∕ ∕ Node to be deleted is not a leaf or leaf like
∕ ∕ node. Find the largest node in the left subtree
c.1) dataptr←bst ↑.left
c.2) while (dataptr ↑.right ≠ NULL)
c.2.1) dataptr← dataptr ↑.right
∕ ∕ Node found.Move the Data
c.3) bst ↑.data←dataptr ↑.data
c.4) return delete_bst ( bst ↑.left, dataptr ↑.data)
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Node to be deleted has no child
Node to be deleted has one child
Node to be deleted has two child
(Find the Inorder successor of the
node to be deleted)
case1.exe
case3.exe
case2.exe
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Reconstruction of Binary Tree
From given inorder and preorder we have to construct a tree
From given inorder and postorder we have to construct a tree
reconstruction.exe
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Expression Tree
( a- b*c ) / ( d+ e/f )
/
+-
/d*a
b c fe
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Evaluate postfix expression using stack
evaluation.exe
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Infix to Prefix using Stack infix2prefix1.swf
infix2prefix2.swf
infix2prefix3.swf
infix2prefix4.swf
infix2prefix5.swf
infix2prefix6.swf
infix2prefix7.swf
infix2prefix8.swf
infix2prefix9.swf
in fix2prefix10.sw f
infix2prefix11.swf
infix2prefix12.swf
infix2prefix20.swf
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Infix to Postfix using Stack
infixtopostfix1.swf
infixtopostfix2.swf
infixtopostfix3.swf
infixtopostfix4.swf
infixtopostfix5.swf
infixtopostfix6.swf
infixtopostfix7.swf
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Threaded Binary Tree
 A binary tree with n nodes has 2n pointers out of which n+1 are always NULL,
So about half the space allocated for pointers is wasted
 We utilize this wasted space to contain some useful information
 A left NULL pointer can be used to store the address of inorder predecessor
of the node
 A right NULL pointer can be used to store the address of inorder successor
of the node.
 These pointers are called threads and a binary tree which implements these
pointers is called a Threaded Binary Tree
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Example
N F N
N D N
B
N C N
A
E
N G N
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Right in-Threaded Binary Tree
N F
N D
B
N C N
A
E
N G
SHEETAL WAGHMARE FROM IIT KHARAGPUR
LEFT in-Threaded Binary Tree
F N
N D N
B
C N
A
E
G N
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Fully in-Threaded Binary Tree
F
N D
B
C N
A
E
G
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Drawbacks of Binary Search Tree
• The Disadvantage of a Binary Search Tree is that its
Height can be as large as N.
• This means that the time needed to perform Insertion
and deletion and many other operations can be O(N)
in the worst case.
• Can be removed using Height Balanced Binary Tree
AVL tree
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL-TREE
 An AVL tree is a binary search tree with a balance condition.
 AVL is named for its inventors: Adel’son-Vel’skii and Landis
 AVL tree approximates the ideal tree (completely balanced tree).
 AVL Tree maintains a height close to the minimum.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL-TREE NOT AN AVL-TREE
Definition:
An AVL tree is a binary search tree such that for any node in the tree,
the height of the left and right subtree can differ by at most 1
Balance factor = Height of left subtree – Height of right subtree
If balance factor of all node is (-1 or 0 or 1) then the tree is said to be a
Balanced Tree ( AVL-TREE)
SHEETAL WAGHMARE FROM IIT KHARAGPUR
50
40
70554530
60
420
000 1
-1 0
1
AVL TREE
EXAMPLE
SHEETAL WAGHMARE FROM IIT KHARAGPUR
EXAMPLES
10
7062
6555
60
50
23
45
50
5
2
1
0
0
0
0 0
0
-1
-1
1
-2
2
0
-3
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Rebalancing
 Suppose the node to be rebalanced is X. There are
4 cases that we might have to fix (two are the
mirror images of the other two):
1. An insertion in the left subtree of the left child of X,
2. An insertion in the right subtree of the left child of X,
3. An insertion in the left subtree of the right child of X,
or
4. An insertion in the right subtree of the right child of X.
 Balance is restored by tree rotations.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Single Rotation
A single rotation switches the roles of the parent and child
while maintaining the search order
Single rotation handles the outside cases (i.e. 1 and 4)
We rotate between a node and its child.
◦ Child becomes parent
◦ Parent becomes right child in case 1 and left child in case 4
The result is a binary search tree that satisfies the AVL
property
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Single Rotation To Fix Case 1: Rotate Right
SHEETAL WAGHMARE FROM IIT KHARAGPUR
Single Rotation To Fix Case 4: Rotate Left
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
insert 14, 15, 16, 13, 12, 11, 10
14
15
• First insert 14 and
15:
• Now insert 16.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
14
15
• Inserting 16 causes AVL violation:
• Need to rotate.
16
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
14
15
• Inserting 16 causes AVL violation:
• Need to rotate.
16
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
14
15
• Rotation type:
16
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
14
15
• Rotation restores AVL balance:
16
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
14
15
16
• Now insert 13 and 12:
13
12
AVL violation - need to rotate
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
• Rotation type:
14
15
16
13
12
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
13
15
16
• Now insert 11
12 14
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
13
15
16
• AVL violation – need to rotate
12 14
11
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
13
15
16
• AVL violation – need to rotate
12 14
11
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
• Rotation type:
13
15
16
12 14
11
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
13
15
16
12
1411
• Now insert 10
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
13
15
16
12
1411
10
AVL violation – need to rotate
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
13
15
16
12
1411
10
• Rotation type:
SHEETAL WAGHMARE FROM IIT KHARAGPUR
AVL Tree Rotations
Single rotations:
13
15
16
11
1410 12
• AVL balance restored.
SHEETAL WAGHMARE FROM IIT KHARAGPUR
SHEETAL WAGHMARE FROM IIT KHARAGPUR

More Related Content

What's hot (20)

B tree
B treeB tree
B tree
 
Binary search tree in data structures
Binary search tree in  data structuresBinary search tree in  data structures
Binary search tree in data structures
 
UNIT III NON LINEAR DATA STRUCTURES – TREES
UNIT III 	NON LINEAR DATA STRUCTURES – TREESUNIT III 	NON LINEAR DATA STRUCTURES – TREES
UNIT III NON LINEAR DATA STRUCTURES – TREES
 
Red black tree
Red black treeRed black tree
Red black tree
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
B tree
B treeB tree
B tree
 
B trees
B treesB trees
B trees
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Transaction Properties in database | ACID Properties
Transaction Properties in database | ACID PropertiesTransaction Properties in database | ACID Properties
Transaction Properties in database | ACID Properties
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Tree Traversal
Tree TraversalTree Traversal
Tree Traversal
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Tree
TreeTree
Tree
 
Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure Binary Heap Tree, Data Structure
Binary Heap Tree, Data Structure
 

Similar to Tree

Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examplesrupanaveen24
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNratnapatil14
 
Unit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfUnit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfKanchanPatil34
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxasimshahzad8611
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docxsowmya koneru
 
Advanced c c++
Advanced c c++Advanced c c++
Advanced c c++muilevan
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESvanithasivdc
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printAbdii Rashid
 
Paris data-geeks-2013-03-28
Paris data-geeks-2013-03-28Paris data-geeks-2013-03-28
Paris data-geeks-2013-03-28Ted Dunning
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms CourseHunt
 

Similar to Tree (20)

Binary tree
Binary treeBinary tree
Binary tree
 
Trees second part in data structures with examples
Trees second part in data structures with examplesTrees second part in data structures with examples
Trees second part in data structures with examples
 
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNNsplaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
splaytree-171227043127.pptx NNNNNNNNNNNNNNNNNNNNNNN
 
Splay tree
Splay treeSplay tree
Splay tree
 
Unit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdfUnit 2_3 Binary Tree Traversals.pdf
Unit 2_3 Binary Tree Traversals.pdf
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Chapter 15
Chapter 15Chapter 15
Chapter 15
 
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptxTREE DATA STRUCTURE SLIDES dsa dsa .pptx
TREE DATA STRUCTURE SLIDES dsa dsa .pptx
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
 
DA_02_algorithms.pptx
DA_02_algorithms.pptxDA_02_algorithms.pptx
DA_02_algorithms.pptx
 
Trees
TreesTrees
Trees
 
Biary search Tree.docx
Biary search Tree.docxBiary search Tree.docx
Biary search Tree.docx
 
Advanced c c++
Advanced c c++Advanced c c++
Advanced c c++
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUESPYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
 
Data Structures 5
Data Structures 5Data Structures 5
Data Structures 5
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
Paris data-geeks-2013-03-28
Paris data-geeks-2013-03-28Paris data-geeks-2013-03-28
Paris data-geeks-2013-03-28
 
Binary Tree - Algorithms
Binary Tree - Algorithms Binary Tree - Algorithms
Binary Tree - Algorithms
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 
s11_bin_search_trees.ppt
s11_bin_search_trees.ppts11_bin_search_trees.ppt
s11_bin_search_trees.ppt
 

More from SHEETAL WAGHMARE

More from SHEETAL WAGHMARE (7)

Heaps
HeapsHeaps
Heaps
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
 
Sales Force Automation
Sales Force AutomationSales Force Automation
Sales Force Automation
 
Enterprise Relationship Management
Enterprise Relationship ManagementEnterprise Relationship Management
Enterprise Relationship Management
 
Call Center
Call CenterCall Center
Call Center
 
Introduction To CRM
Introduction To CRMIntroduction To CRM
Introduction To CRM
 
Linklist
LinklistLinklist
Linklist
 

Recently uploaded

ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Sumit Tiwari
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the lifeNitinDeodare
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxneillewis46
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...Nguyen Thanh Tu Collection
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Mohamed Rizk Khodair
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Celine George
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatmentsaipooja36
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxheathfieldcps1
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhleson0603
 

Recently uploaded (20)

ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
Chapter 7 Pharmacosy Traditional System of Medicine & Ayurvedic Preparations ...
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
Graduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptxGraduate Outcomes Presentation Slides - English (v3).pptx
Graduate Outcomes Presentation Slides - English (v3).pptx
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
“O BEIJO” EM ARTE .
“O BEIJO” EM ARTE                       .“O BEIJO” EM ARTE                       .
“O BEIJO” EM ARTE .
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
The basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptxThe basics of sentences session 4pptx.pptx
The basics of sentences session 4pptx.pptx
 
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinhĐề tieng anh thpt 2024 danh cho cac ban hoc sinh
Đề tieng anh thpt 2024 danh cho cac ban hoc sinh
 

Tree

  • 1. SHEETAL WAGHMARE M.TECH (Computer Science & Data Processing) IIT KHARAGPUR EMAIL-ID: shitu2iitkgp@gmail.com sheetalw3@gmail.com TREE
  • 2. Instruction To Use This Presentation  Dear user I have kept some animated/executable/video files, so it will be easy for you to understand but that will not run/play in slideshow To see/run that files you have to exit from slideshow then click on the icon and also install KM Player For example: click on the below icons one by one( one is executable file and other is video) you will be able to see the animation begin.exe op addatbegin.swf SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 3. Classification Of Data Structure Linear Data Structure • Array • Linked List • Stack • Queue Non-Linear Data Structure • Tree • Graph SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 4. What is Tree?????? Recursive definition, a tree is a collection of n nodes, one of which is the root, and n - 1 edges. Having n - 1 edges follows from the fact that each edge connects some node to its parent, and every node except the root has one parent SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 5. General Tree A tree can be represented as Arrows that point downward are first_child pointers  Arrows that go left to right are next_sibling pointers SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 6. Trees  A connected structure which • is either empty OR • has a designated node called root, from which descends 0,1 or more subtrees which are also Trees. A C JIHGF B SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 7. Terminologies • Finite number of elements called nodes,& finite set of directed lines called branches, that connect the nodes. • Branch directed towards a node is an In-degree Branch. • Branch directed away from the node is Out-degree Branch. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 8. Continued... • The node with out-degree Zero(0) is known as leaf or a terminal node. • Sum of number of In-degree and Out-degree node branches = degree of node • If Tree is not empty the first node is called as root. • Nodes that are neither root nor a leaf are known as Internal nodes. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 9. • Node that has subtrees is the parent of the roots of the subtrees. • Roots of these subtrees are children of the Node. • Children of the Same Parent are Siblings. • Ancestors of a node are all the nodes along the path from the root to the node. Continued... SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 10. • Path is a sequence of nodes in which each node is adjacent to the next one. • Level of a node is its distance from the root. • Root is at level Zero(0). • Height(depth) of the Tree is the level of the leaf in the longest path from the root. Continued... SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 11. Example of a Tree • No. of nodes = 9 • Height = 4 • Highest Level = 3 • Root Node = 8 • Leaves = 1,4,7,13 • Interior Nodes = 3,10,6,14 • Ancestors Of 6 = 3,8 • Descendents Of 10 = 14,13 • Sibling Of 1 = 6 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 12. Binary Trees  A finite set of vertices that is either • Empty. OR • Consists of a root node together with two binary subtree which are disjoint from each other, and from the root and are called the left and right subtree SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 13.  Maximum no.of nodes at any level is 2i  Maximum no.of nodes at height h is 2h -1  Height=level + 1  log2 (n+1)< height <= 2h -1 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 14. Full Binary Tree and Complete Binary Tree SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 16. BT Traversal • Process of visiting each of the nodes in a tree and examine the value there. • Two kinds • Depth-First Tree Traversal. • Breadth-First Tree Traversal. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 17. Depth First Tree Traversal • Pre-Order : Root first, Left child, Right child. • Post-Order : Left child, Right child, Root. • In-Order : Left child, Root, Right child. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 18. Algorithm(Pre-order) • preorder(tree↑){ ▫ if tree ≠ NULL then  process tree↑.data ;  preorder (tree↑.left) ;  preorder(tree↑.right) ; ▫ return. } SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 19. EXAMPLE A CB GED F Pre-order traversal: A B D E C F G SHEETAL WAGHMARE FROM IIT KHARAGPUR Pre-Order : Root first, Left child, Right child. Rotated node will be taken first
  • 20. Algorithm(Post-order) • postorder(tree↑){ ▫ if tree ≠ NULL then  postorder (tree↑.left) ;  postorder(tree↑.right) ;  process tree↑.data ; ▫ return. } SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 21. EXAMPLE A B C ED F IHG Post-order traversal: G D H I E B F C A SHEETAL WAGHMARE FROM IIT KHARAGPUR Post-Order : Left Child, Right child, root. Rotated node will be taken first
  • 22. Algorithm(In-order) • inorder(tree↑){ ▫ if tree ≠ NULL then  inorder (tree↑.left) ;  process tree↑.data ;  inorder(tree↑.right) ; ▫ return. } SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 23. EXAMPLE A D CB E F H I J In-order traversal: D B I E A F C J H SHEETAL WAGHMARE FROM IIT KHARAGPUR In-Order : Left child, Root, Right child. Rotated node will be taken first
  • 24. Algorithm breadth-first-traversal(tree)  Createqueue(Q)  walkerptr←tree  while (walkerptr ≠ NULL) process walkerptr if (walkerptr ↑.left ≠ NULL) Enqueue (Q,walkerptr ↑.left) if(walkerptr ↑.right ≠ NULL) Enqueue (Q,walkerptr ↑.right) if (Not (empty(Q)) Dequeue (Q,walkerptr) else walkerptr = NULL • return SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 25. EXAMPLE A C B F GED Breadth First Traversal: A B C D E F G SHEETAL WAGHMARE FROM IIT KHARAGPUR Rotated node will be taken first
  • 26. Binary Search Tree(Motivation) • Insertion and Deletion in the middle of the array is very inefficient. • Search Operation in an ordered linked lists is inefficient. • To get the best of both, Binary Search Tree(BST) was introduced. • The binary search tree is widely used data-structure for information storage and retrieval as it supports many dynamic-set operations including Search, Minimum, Maximum, Predecessor, Successor, Insert and Delete.* SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 27. Definition(BST) A binary search tree T for a set of keys from a total order is a binary tree in which each node has a key value and all the keys of the left subtree are less than the key at the root and all the keys of the right subtree are greater than the key at the root. This property holding recursively for the left and right subtrees of the tree T. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 29. Operations on BST • Traversal • Searching • Insertion • Deletion SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 30. BST Operation (Searching)  Find Smallest element  Find Largest element  Find Requested element SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 31. Algorithm findsmallest (bst) 1. if (bst ↑.left = NULL) then return (bst ↑.data) 1. else return (findsmallest ( bst ↑.left)) SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 32. EXAMPLE 50 40 60 20 55 6545 10 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 33. Algorithm findlargest (bst) 1. if (bst ↑.right = NULL) then return (bst ↑.data) 2. else return (findlargest (bst ↑.right)) SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 35. Algorithm search(bst, data) 1. if bst = NULL then return NULL 2. else if (data < bst ↑.data) search (bst ↑.left , data) elseif (data > bst ↑.data) search ( bst ↑.right , data) 3. else return (bst ↑.data) SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 37. BST Operation-Insertion • method insert(key) ▫ Based on comparisons of new data and the values of nodes in BST. ▫ To insert follow the branches to an empty subtree and then insert the new node. ▫ all inserts take place at a leaf or at a leaf like node – a node that has only one null subtree. • Elements in nodes must be comparable! SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 38. Algorithm Algorithm insert_bst(bst,data); 1) if bst = NULL; A) then a) new(P) ; b) P↑.data←data ; c) P↑.left←NULL ; d) P↑.right←NULL ; e) bst←P ; f) return bst . SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 39. Continued… B) Else a) if (data < bst ↑.data) then insert_bst (bst ↑.left,data) b) if (data ≥ bst ↑.data) then insert_bst(bst ↑.right,data) C) return. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 41. BST Operation-Deletion  removes a specified item from the BST and adjusts the tree.  uses a binary search to locate the target items.  starting at the root it probes down the tree till it finds the target or reaches a leaf node (target not in the tree)  removal of a node must not leave a ‘gap’ in the tree. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 42. Algorithm Deletion • Algorithm delete_bst (bst , data) 1) if bst = NULL then return NULL 2) if (data < bst ↑.data) then return delete_bst (bst ↑.left, data) 3) elseif (data > bst ↑.data) then return delete_bst (bst ↑.right, data) SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 43. Continued… 4) Else // found the node to be deleted & test whether it’s a leaf node. a) if (bst ↑.left = NULL) a.1) dataptr←bst a.2) bst←bst ↑.right a.3) free(dataptr) a.4) return ; b) elseif ( bst ↑.right =NULL) b.1) dataptr←bst b.2) bst←bst ↑.left SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 44. Continued… b.3) free(dataptr) b.4) return c) else ∕ ∕ Node to be deleted is not a leaf or leaf like ∕ ∕ node. Find the largest node in the left subtree c.1) dataptr←bst ↑.left c.2) while (dataptr ↑.right ≠ NULL) c.2.1) dataptr← dataptr ↑.right ∕ ∕ Node found.Move the Data c.3) bst ↑.data←dataptr ↑.data c.4) return delete_bst ( bst ↑.left, dataptr ↑.data) SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 45. Node to be deleted has no child Node to be deleted has one child Node to be deleted has two child (Find the Inorder successor of the node to be deleted) case1.exe case3.exe case2.exe SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 46. Reconstruction of Binary Tree From given inorder and preorder we have to construct a tree From given inorder and postorder we have to construct a tree reconstruction.exe SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 47. Expression Tree ( a- b*c ) / ( d+ e/f ) / +- /d*a b c fe SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 48. Evaluate postfix expression using stack evaluation.exe SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 49. Infix to Prefix using Stack infix2prefix1.swf infix2prefix2.swf infix2prefix3.swf infix2prefix4.swf infix2prefix5.swf infix2prefix6.swf infix2prefix7.swf infix2prefix8.swf infix2prefix9.swf in fix2prefix10.sw f infix2prefix11.swf infix2prefix12.swf infix2prefix20.swf SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 50. Infix to Postfix using Stack infixtopostfix1.swf infixtopostfix2.swf infixtopostfix3.swf infixtopostfix4.swf infixtopostfix5.swf infixtopostfix6.swf infixtopostfix7.swf SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 51. Threaded Binary Tree  A binary tree with n nodes has 2n pointers out of which n+1 are always NULL, So about half the space allocated for pointers is wasted  We utilize this wasted space to contain some useful information  A left NULL pointer can be used to store the address of inorder predecessor of the node  A right NULL pointer can be used to store the address of inorder successor of the node.  These pointers are called threads and a binary tree which implements these pointers is called a Threaded Binary Tree SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 52. Example N F N N D N B N C N A E N G N SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 53. Right in-Threaded Binary Tree N F N D B N C N A E N G SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 54. LEFT in-Threaded Binary Tree F N N D N B C N A E G N SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 55. Fully in-Threaded Binary Tree F N D B C N A E G SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 56. Drawbacks of Binary Search Tree • The Disadvantage of a Binary Search Tree is that its Height can be as large as N. • This means that the time needed to perform Insertion and deletion and many other operations can be O(N) in the worst case. • Can be removed using Height Balanced Binary Tree AVL tree SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 57. AVL-TREE  An AVL tree is a binary search tree with a balance condition.  AVL is named for its inventors: Adel’son-Vel’skii and Landis  AVL tree approximates the ideal tree (completely balanced tree).  AVL Tree maintains a height close to the minimum. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 58. AVL-TREE NOT AN AVL-TREE Definition: An AVL tree is a binary search tree such that for any node in the tree, the height of the left and right subtree can differ by at most 1 Balance factor = Height of left subtree – Height of right subtree If balance factor of all node is (-1 or 0 or 1) then the tree is said to be a Balanced Tree ( AVL-TREE) SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 59. 50 40 70554530 60 420 000 1 -1 0 1 AVL TREE EXAMPLE SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 61. Rebalancing  Suppose the node to be rebalanced is X. There are 4 cases that we might have to fix (two are the mirror images of the other two): 1. An insertion in the left subtree of the left child of X, 2. An insertion in the right subtree of the left child of X, 3. An insertion in the left subtree of the right child of X, or 4. An insertion in the right subtree of the right child of X.  Balance is restored by tree rotations. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 62. Single Rotation A single rotation switches the roles of the parent and child while maintaining the search order Single rotation handles the outside cases (i.e. 1 and 4) We rotate between a node and its child. ◦ Child becomes parent ◦ Parent becomes right child in case 1 and left child in case 4 The result is a binary search tree that satisfies the AVL property SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 63. Single Rotation To Fix Case 1: Rotate Right SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 64. Single Rotation To Fix Case 4: Rotate Left SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 65. AVL Tree Rotations Single rotations: insert 14, 15, 16, 13, 12, 11, 10 14 15 • First insert 14 and 15: • Now insert 16. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 66. AVL Tree Rotations Single rotations: 14 15 • Inserting 16 causes AVL violation: • Need to rotate. 16 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 67. AVL Tree Rotations Single rotations: 14 15 • Inserting 16 causes AVL violation: • Need to rotate. 16 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 68. AVL Tree Rotations Single rotations: 14 15 • Rotation type: 16 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 69. AVL Tree Rotations Single rotations: 14 15 • Rotation restores AVL balance: 16 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 70. AVL Tree Rotations Single rotations: 14 15 16 • Now insert 13 and 12: 13 12 AVL violation - need to rotate SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 71. AVL Tree Rotations Single rotations: • Rotation type: 14 15 16 13 12 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 72. AVL Tree Rotations Single rotations: 13 15 16 • Now insert 11 12 14 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 73. AVL Tree Rotations Single rotations: 13 15 16 • AVL violation – need to rotate 12 14 11 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 74. AVL Tree Rotations Single rotations: 13 15 16 • AVL violation – need to rotate 12 14 11 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 75. AVL Tree Rotations Single rotations: • Rotation type: 13 15 16 12 14 11 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 76. AVL Tree Rotations Single rotations: 13 15 16 12 1411 • Now insert 10 SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 77. AVL Tree Rotations Single rotations: 13 15 16 12 1411 10 AVL violation – need to rotate SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 78. AVL Tree Rotations Single rotations: 13 15 16 12 1411 10 • Rotation type: SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 79. AVL Tree Rotations Single rotations: 13 15 16 11 1410 12 • AVL balance restored. SHEETAL WAGHMARE FROM IIT KHARAGPUR
  • 80. SHEETAL WAGHMARE FROM IIT KHARAGPUR