SlideShare a Scribd company logo
1 of 39
Data Structure and
Algorithm (CS-102)
Ashok K Turuk
Binary Search Tree (BST)
Suppose T is a binary tree

Then T is called binary search tree if
each node N of T has the 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
x

For any node y
in this subtree
key(y) < key(x)

For any node z
in this subtree
key(z) > key(x)
Binary Search Trees

A binary search tree

Not a binary search tree
Binary search trees

Two binary search trees representing the
same set:

Average depth of a node is O(log N);
maximum depth of a node is O(N)
Searching and Inserting in BST

Algorithm to find the location of ITEM in
the BST T or insert ITEM as a new
node in its appropriate place in the tree
[a] Compare ITEM with the root node N
of the tree
(i) If ITEM < N, proceed to the left
child of N
(ii) If ITEM > N, proceed to the right
child of N
Searching and Inserting in BST

Algorithm to find the location of ITEM in
the BST T or insert ITEM as a new node
in its appropriate place in the tree

[b] Repeat Step (a) until one of the following
occurs
(i) We meet a node N such that ITEM = N.
In this case search is successful
(ii) We meet an empty sub-tree, which
indicates that search is unsuccessful and
we insert ITEM in place of empty subtree
Searching BST
• If we are searching for 15, then we are done.
• If we are searching for a key < 15, then we
should search in the left subtree.
• If we are searching for a key > 15, then we
should search in the right subtree.
Insert 40, 60, 50, 33, 55, 11 into an empty
BST
40

40

40

60

60

1. ITEM = 40 2. ITEM = 60

40

50
3. ITEM = 50

60

33

50
4. ITEM = 33
Insert 40, 60, 50, 33, 55, 11 into an empty
BST
40

40
60

33

11

50
55
5. ITEM = 55

60

33

50
55

6. ITEM = 11
Locating an ITEM
A binary search tree T is in memory and
an ITEM of information is given. This
procedure finds the location LOC of
ITEM in T and also the location of the
parent PAR of ITEM.
Locating an ITEM

Three special cases:
[1] LOC == NULL and PAR == NULL, tree is
empty
[2] LOC and PAR == NULL, ITEM is the
root of T
[3] LOC == NULL and PAR NULL, ITEM
is not in T and can be added to T as
child of the node N with location PAR.
FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR)

[1] [Tree empty ?]
If ROOT == NULL, then
Set LOC = NULL, PAR = NULL,
Exit
[2] [ITEM at root ?]
If ROOT->INFO == ITEM, then
Set LOC = ROOT, PAR = NULL, Exit
[3] [Initialize pointer PTR and SAVE]
If ITEM < ROOT->INFO then
Set PTR = ROOT->LEFT, SAVE = ROOT
Else
Set PTR = ROOT->RIGHT, SAVE =ROOT
[4] Repeat Step 5 and 6 while PTR

NULL

[5] [ITEM Found ?]
If ITEM == PTR->INFO, then
Set LOC = PTR, PAR = SAVE, Exit
[6]

If ITEM < PTR->INFO, then
Set SAVE = PTR, PTR = PTR->LEFT
Else
Set SAVE = PTR, PTR = PTR->RIGHT

[7] [Search Unsuccessful]
Set LOC = NULL, PAR = SAVE
[8] Exit
A binary search Tree T is in memory and
an ITEM of information is given.
Algorithm to find the location LOC of
ITEM in T or adds ITEM as a new node
in T at location LOC.
[1] Call FIND(INFO,
LEFT,RIGHT,ROOT,ITEM,LOC,PAR)
[2] If LOC NULL, then Exit
[3] [Copy the ITEM into the node NEW]
(a) Create a node NEW
(b) NEW->INFO = ITEM
( c) Set LOC = NEW, NEW->LEFT =
NULL, NEW->RIGHT = NULL
[4] [Add ITEM to tree]
If PAR = NULL
Set ROOT = NEW
Else
If ITEM < PAR->INFO, then
Set PAR->LEFT = NEW
Else
Set PAR->RIGHT = NEW
[5] Exit
Binary Search Tree
Insert Algorithm

•
•
•

If value we want to insert < key of
current node, we have to go to the left
subtree
Otherwise we have to go to the right
subtree
If the current node is empty (not
existing) create a node with the value
we are inserting and place it here.
Binary Search Tree
For example, inserting ’15’ into the BST?
Deletion from BST
T is a binary tree. Delete an ITEM from
the tree T
Deleting a node N from tree depends
primarily on the number of children of
node N
Deletion
There are three cases in deletion
Case 1. N has no children. N is deleted from
the T by replacing the location of N in the
parent node of N by null pointer
40
60

33
11
ITEM = 11

50
55
Deletion
Case 2. N has exactly one children. N is deleted
from the T by simply replacing the location of
N in the parent node of N by the location of
the only child of N
40
60

33
11

50

ITEM = 50

55
Deletion
Case 2.
40
60

33

11

55

ITEM = 50
Deletion

Case 3. N has two children. Let S(N) denote the
in-order successor of N. Then N is deleted
from the T by first deleting S(N) from T and
then replacing node N in T by the node S(N)
40
60

33
11

50

ITEM = 40

55
Deletion

Case 3.
40

60

33

11
Delete 50

50

55

60

33

11

55

Replace 40 by 50
Types of Binary Trees
• Degenerate – only one child
• Balanced – mostly two children
• Complete – always two children

Degenerate
binary tree

Balanced binary
tree

Complete
binary tree
Binary Trees Properties

• Degenerate

• Balanced

– Height = O(n) for n
nodes
– Similar to linear list

– Height = O( log(n) )
for n nodes
– Useful for searches

Degenerate
binary tree

Balanced binary
tree
Binary Search Trees
• Key property
– Value at node
• Smaller values in left subtree
• Larger values in right subtree

– Example

X

• X>Y
• X<Z
Y

Z
Binary Search Trees
• Examples

5

10
2
5

10

45

30

5

45

30

2

25

45

2

10

25

30

25
Binary search
trees

Non-binary search
tree
Example Binary Searches
• Find ( 2 )
5

10

10 > 2, left
5

2

30

25

5 > 2, left
45

5 > 2, left
2

2 = 2, found

45

30
10

25

2 = 2, found
Example Binary Searches
• Find ( 25 )
5

10

10 < 25, right
5

30

30 > 25, left

5 < 25, right
2

45

25 = 25, found
2

25

45 > 25, left
30 > 25, left

30

45

10 < 25, right
10

25 = 25, found
25
Binary Search Properties
• Time of search

– Proportional to height of tree
– Balanced binary tree
• O( log(n) ) time

– Degenerate tree

• O( n ) time
• Like searching linked list / unsorted array

• Requires

– Ability to compare key values
Binary Search Tree Construction
• How to build & maintain binary trees?
– Insertion
– Deletion

• Maintain key property (invariant)
– Smaller values in left subtree
– Larger values in right subtree
Binary Search Tree – Insertion
•

Algorithm

1. Perform search for value X
2. Search will end at node Y (if X not in tree)
3. If X < Y, insert new leaf X as new left
subtree for Y
4. If X > Y, insert new leaf X as new right
subtree for Y

•

Observations

– O( log(n) ) operation for balanced tree
– Insertions may unbalance tree
Example Insertion
• Insert ( 20 )
10
5

10 < 20, right
30 > 20, left

30

25 > 20, left
2

25

45

Insert 20 on left

20
Binary Search Tree – Deletion
•

Algorithm

1. Perform search for value X
2. If X is a leaf, delete X
3. Else // must delete internal node

a) Replace with largest value Y on left subtree
OR smallest value Z on right subtree
b) Delete replacement value (Y or Z) from subtree

Observation

– O( log(n) ) operation for balanced tree
– Deletions may unbalance tree
Example Deletion (Leaf)
• Delete ( 25 )
10

10

10 < 25, right
5

2

30

25

5

30 > 25, left
45

25 = 25,
delete

2

30

45

More Related Content

What's hot

Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queueRajkiran Nadar
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structuresMohd Arif
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing AlgorithmHayi Nukman
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmayTanmay 'Unsinkable'
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5Kumar
 
Linked list
Linked listLinked list
Linked listVONI
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Kuntal Bhowmick
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and PolynomialAroosa Rajput
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like StructuresIntro C# Book
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniyaTutorialsDuniya.com
 

What's hot (20)

Data structure , stack , queue
Data structure , stack , queueData structure , stack , queue
Data structure , stack , queue
 
Array
ArrayArray
Array
 
Arrays and structures
Arrays and structuresArrays and structures
Arrays and structures
 
Arrays
ArraysArrays
Arrays
 
4.4 hashing
4.4 hashing4.4 hashing
4.4 hashing
 
Hashing Algorithm
Hashing AlgorithmHashing Algorithm
Hashing Algorithm
 
Data structure
Data  structureData  structure
Data structure
 
Application of hashing in better alg design tanmay
Application of hashing in better alg design tanmayApplication of hashing in better alg design tanmay
Application of hashing in better alg design tanmay
 
Hash table
Hash tableHash table
Hash table
 
Binary tree
Binary treeBinary tree
Binary tree
 
Unit viii searching and hashing
Unit   viii searching and hashing Unit   viii searching and hashing
Unit viii searching and hashing
 
Data structure lecture 5
Data structure lecture 5Data structure lecture 5
Data structure lecture 5
 
Linked list
Linked listLinked list
Linked list
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Sparse Matrix and Polynomial
Sparse Matrix and PolynomialSparse Matrix and Polynomial
Sparse Matrix and Polynomial
 
LECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdfLECT 10, 11-DSALGO(Hashing).pdf
LECT 10, 11-DSALGO(Hashing).pdf
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Algorithms notes tutorials duniya
Algorithms notes   tutorials duniyaAlgorithms notes   tutorials duniya
Algorithms notes tutorials duniya
 
sort search in C
 sort search in C  sort search in C
sort search in C
 

Viewers also liked

1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search treeKrish_ver2
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsAakash deep Singhal
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddickenBenjamin Dicken
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsAakash deep Singhal
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in JavaMahmoud Alfarra
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREESiddhi Shrivas
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...sethuraman R
 

Viewers also liked (20)

1.5 binary search tree
1.5 binary search tree1.5 binary search tree
1.5 binary search tree
 
Data structures 3
Data structures 3Data structures 3
Data structures 3
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
12111 data structure
12111 data structure12111 data structure
12111 data structure
 
BinarySearchTree-bddicken
BinarySearchTree-bddickenBinarySearchTree-bddicken
BinarySearchTree-bddicken
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithms
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Subsidence in coal mines
Subsidence in coal minesSubsidence in coal mines
Subsidence in coal mines
 
3 searching algorithms in Java
3 searching algorithms in Java3 searching algorithms in Java
3 searching algorithms in Java
 
Red Black Tree
Red Black TreeRed Black Tree
Red Black Tree
 
THREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREETHREADED BINARY TREE AND BINARY SEARCH TREE
THREADED BINARY TREE AND BINARY SEARCH TREE
 
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
Introduction to Data structure & Algorithms - Sethuonline.com | Sathyabama Un...
 
Lecture7 data structure(tree)
Lecture7 data structure(tree)Lecture7 data structure(tree)
Lecture7 data structure(tree)
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Traversals | Data Structures
Traversals | Data StructuresTraversals | Data Structures
Traversals | Data Structures
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Tree
TreeTree
Tree
 
Binary Search Algorithm
Binary Search Algorithm Binary Search Algorithm
Binary Search Algorithm
 

Similar to BST Data Structure

CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfssuser034ce1
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeManishPrajapati78
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bstSSE_AndyLi
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeZafar Ayub
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptxRedHeart11
 
CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3SIMONTHOMAS S
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In ScalaKnoldus Inc.
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scalaMeetu Maltiar
 
BST_algorithm.pptx
BST_algorithm.pptxBST_algorithm.pptx
BST_algorithm.pptxMahdiHasan86
 

Similar to BST Data Structure (20)

8.binry search tree
8.binry search tree8.binry search tree
8.binry search tree
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
BST.pdf
BST.pdfBST.pdf
BST.pdf
 
Bst(Binary Search Tree)
Bst(Binary Search Tree)Bst(Binary Search Tree)
Bst(Binary Search Tree)
 
Data Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search TreeData Structure and Algorithms Binary Search Tree
Data Structure and Algorithms Binary Search Tree
 
8 chapter4 trees_bst
8 chapter4 trees_bst8 chapter4 trees_bst
8 chapter4 trees_bst
 
Unit 4 tree
Unit 4   treeUnit 4   tree
Unit 4 tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
data structure on bca.
data structure on bca.data structure on bca.
data structure on bca.
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Lec8
Lec8Lec8
Lec8
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
Lecture_10 - Revised.pptx
Lecture_10 - Revised.pptxLecture_10 - Revised.pptx
Lecture_10 - Revised.pptx
 
CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3CS8391-Data Structures Unit 3
CS8391-Data Structures Unit 3
 
DS - BST
DS - BSTDS - BST
DS - BST
 
Data Structures In Scala
Data Structures In ScalaData Structures In Scala
Data Structures In Scala
 
Data structures in scala
Data structures in scalaData structures in scala
Data structures in scala
 
BST_algorithm.pptx
BST_algorithm.pptxBST_algorithm.pptx
BST_algorithm.pptx
 

Recently uploaded

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptxSherlyMaeNeri
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 

Recently uploaded (20)

Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
Judging the Relevance and worth of ideas part 2.pptx
Judging the Relevance  and worth of ideas part 2.pptxJudging the Relevance  and worth of ideas part 2.pptx
Judging the Relevance and worth of ideas part 2.pptx
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 

BST Data Structure

  • 1. Data Structure and Algorithm (CS-102) Ashok K Turuk
  • 2. Binary Search Tree (BST) Suppose T is a binary tree Then T is called binary search tree if each node N of T has the 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
  • 3. x For any node y in this subtree key(y) < key(x) For any node z in this subtree key(z) > key(x)
  • 4. Binary Search Trees A binary search tree Not a binary search tree
  • 5. Binary search trees Two binary search trees representing the same set: Average depth of a node is O(log N); maximum depth of a node is O(N)
  • 6. Searching and Inserting in BST Algorithm to find the location of ITEM in the BST T or insert ITEM as a new node in its appropriate place in the tree [a] Compare ITEM with the root node N of the tree (i) If ITEM < N, proceed to the left child of N (ii) If ITEM > N, proceed to the right child of N
  • 7. Searching and Inserting in BST Algorithm to find the location of ITEM in the BST T or insert ITEM as a new node in its appropriate place in the tree [b] Repeat Step (a) until one of the following occurs (i) We meet a node N such that ITEM = N. In this case search is successful (ii) We meet an empty sub-tree, which indicates that search is unsuccessful and we insert ITEM in place of empty subtree
  • 8. Searching BST • If we are searching for 15, then we are done. • If we are searching for a key < 15, then we should search in the left subtree. • If we are searching for a key > 15, then we should search in the right subtree.
  • 9.
  • 10. Insert 40, 60, 50, 33, 55, 11 into an empty BST 40 40 40 60 60 1. ITEM = 40 2. ITEM = 60 40 50 3. ITEM = 50 60 33 50 4. ITEM = 33
  • 11. Insert 40, 60, 50, 33, 55, 11 into an empty BST 40 40 60 33 11 50 55 5. ITEM = 55 60 33 50 55 6. ITEM = 11
  • 12. Locating an ITEM A binary search tree T is in memory and an ITEM of information is given. This procedure finds the location LOC of ITEM in T and also the location of the parent PAR of ITEM.
  • 13. Locating an ITEM Three special cases: [1] LOC == NULL and PAR == NULL, tree is empty [2] LOC and PAR == NULL, ITEM is the root of T [3] LOC == NULL and PAR NULL, ITEM is not in T and can be added to T as child of the node N with location PAR.
  • 14. FIND(INFO,LEFT,RIGHT,ROOT,ITEM,LOC,PAR) [1] [Tree empty ?] If ROOT == NULL, then Set LOC = NULL, PAR = NULL, Exit [2] [ITEM at root ?] If ROOT->INFO == ITEM, then Set LOC = ROOT, PAR = NULL, Exit [3] [Initialize pointer PTR and SAVE] If ITEM < ROOT->INFO then Set PTR = ROOT->LEFT, SAVE = ROOT Else Set PTR = ROOT->RIGHT, SAVE =ROOT
  • 15. [4] Repeat Step 5 and 6 while PTR NULL [5] [ITEM Found ?] If ITEM == PTR->INFO, then Set LOC = PTR, PAR = SAVE, Exit [6] If ITEM < PTR->INFO, then Set SAVE = PTR, PTR = PTR->LEFT Else Set SAVE = PTR, PTR = PTR->RIGHT [7] [Search Unsuccessful] Set LOC = NULL, PAR = SAVE [8] Exit
  • 16. A binary search Tree T is in memory and an ITEM of information is given. Algorithm to find the location LOC of ITEM in T or adds ITEM as a new node in T at location LOC.
  • 17. [1] Call FIND(INFO, LEFT,RIGHT,ROOT,ITEM,LOC,PAR) [2] If LOC NULL, then Exit [3] [Copy the ITEM into the node NEW] (a) Create a node NEW (b) NEW->INFO = ITEM ( c) Set LOC = NEW, NEW->LEFT = NULL, NEW->RIGHT = NULL
  • 18. [4] [Add ITEM to tree] If PAR = NULL Set ROOT = NEW Else If ITEM < PAR->INFO, then Set PAR->LEFT = NEW Else Set PAR->RIGHT = NEW [5] Exit
  • 19. Binary Search Tree Insert Algorithm • • • If value we want to insert < key of current node, we have to go to the left subtree Otherwise we have to go to the right subtree If the current node is empty (not existing) create a node with the value we are inserting and place it here.
  • 20. Binary Search Tree For example, inserting ’15’ into the BST?
  • 21.
  • 22. Deletion from BST T is a binary tree. Delete an ITEM from the tree T Deleting a node N from tree depends primarily on the number of children of node N
  • 23. Deletion There are three cases in deletion Case 1. N has no children. N is deleted from the T by replacing the location of N in the parent node of N by null pointer 40 60 33 11 ITEM = 11 50 55
  • 24. Deletion Case 2. N has exactly one children. N is deleted from the T by simply replacing the location of N in the parent node of N by the location of the only child of N 40 60 33 11 50 ITEM = 50 55
  • 26. Deletion Case 3. N has two children. Let S(N) denote the in-order successor of N. Then N is deleted from the T by first deleting S(N) from T and then replacing node N in T by the node S(N) 40 60 33 11 50 ITEM = 40 55
  • 28. Types of Binary Trees • Degenerate – only one child • Balanced – mostly two children • Complete – always two children Degenerate binary tree Balanced binary tree Complete binary tree
  • 29. Binary Trees Properties • Degenerate • Balanced – Height = O(n) for n nodes – Similar to linear list – Height = O( log(n) ) for n nodes – Useful for searches Degenerate binary tree Balanced binary tree
  • 30. Binary Search Trees • Key property – Value at node • Smaller values in left subtree • Larger values in right subtree – Example X • X>Y • X<Z Y Z
  • 31. Binary Search Trees • Examples 5 10 2 5 10 45 30 5 45 30 2 25 45 2 10 25 30 25 Binary search trees Non-binary search tree
  • 32. Example Binary Searches • Find ( 2 ) 5 10 10 > 2, left 5 2 30 25 5 > 2, left 45 5 > 2, left 2 2 = 2, found 45 30 10 25 2 = 2, found
  • 33. Example Binary Searches • Find ( 25 ) 5 10 10 < 25, right 5 30 30 > 25, left 5 < 25, right 2 45 25 = 25, found 2 25 45 > 25, left 30 > 25, left 30 45 10 < 25, right 10 25 = 25, found 25
  • 34. Binary Search Properties • Time of search – Proportional to height of tree – Balanced binary tree • O( log(n) ) time – Degenerate tree • O( n ) time • Like searching linked list / unsorted array • Requires – Ability to compare key values
  • 35. Binary Search Tree Construction • How to build & maintain binary trees? – Insertion – Deletion • Maintain key property (invariant) – Smaller values in left subtree – Larger values in right subtree
  • 36. Binary Search Tree – Insertion • Algorithm 1. Perform search for value X 2. Search will end at node Y (if X not in tree) 3. If X < Y, insert new leaf X as new left subtree for Y 4. If X > Y, insert new leaf X as new right subtree for Y • Observations – O( log(n) ) operation for balanced tree – Insertions may unbalance tree
  • 37. Example Insertion • Insert ( 20 ) 10 5 10 < 20, right 30 > 20, left 30 25 > 20, left 2 25 45 Insert 20 on left 20
  • 38. Binary Search Tree – Deletion • Algorithm 1. Perform search for value X 2. If X is a leaf, delete X 3. Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree Observation – O( log(n) ) operation for balanced tree – Deletions may unbalance tree
  • 39. Example Deletion (Leaf) • Delete ( 25 ) 10 10 10 < 25, right 5 2 30 25 5 30 > 25, left 45 25 = 25, delete 2 30 45