9 views

Uploaded by ashokatmellon

Description of Binary Search trees in easiest way

- ssaasst
- Comer Btree Survey
- FD35.docx
- ExtentReport Review
- cszg623 (12)
- Types of Binary Tree
- CS1602-Data Structures and Algorithms-Anna University-Question Papers
- ocaml-3.08-refman
- An Engineering Approach to Evolutionary Art (Paper)
- SourceQL Paper 1: Final Report for EECS 433 (Database Systems)
- Class 21jj
- rec05
- Index and Hashing
- Indexing and Hashing
- L5_AVLTrees
- Bins
- Tournament Trees
- Consistency in Database Systems
- Syntax Tools
- Birch

You are on page 1of 39

Anish Mathuria

Faculty Block 1, 1105

Binary Search Trees

A binary search tree

is a binary tree

if a node has value N, all values in its left sub-

tree are less than N, and all values in its right

sub-tree are greater than N. This property

called binary search tree property holds for

each and every node in the tree

5

4 8

1

3

7

11

BINARY SEARCH TREE

Example

17

6 19

11

15

NOT A

BINARY SEARCH TREE Why?

Example

Operations

Search for an element

Minimum, Maximum

Predecessor, Successor

Insert, Delete

BST Implementation

9

5

10

96 99

94

97

key

left right

9

5 94

10 97

96 99

root

BST Operations - Search

Given a Binary Search Tree B, we search for a

key k, first at the root, then either in the left

subtree or the right subtree

Search (B, k)

v := root(B)

if v = NULL or k = v.key then

return v

if k < v.key then

Search (v.left, k)

else

Search (v.right, k)

BST Operations Search (2)

An Iterative search can also be defined

Search (B, k)

v := root(B)

while v null and k v.key do

if k < v.key then

v := v.left

else

v := v.right

return v

Example

Search for 13

BST Operations Maximum

The Binary Search Tree property guarantees

that the maximum is the right most child of the

tree

Maximum (B)

v := root(B)

while v.right NULL do

v := v.right

return v

Minimum can similarly be defined

Example

Minimum

Maximum

BST Operations Insertion

The insertion of an item with a new key k into a

Binary Search Tree requires three steps

Create a node to hold the item

Determine the place in the BST where the new node is to

be inserted. To do this:

Search for the key k in the BST. If the key is found, false is

returned and the operation terminated, otherwise,

the search will encounter a null value in one of the leaf nodes

of the BST

Attach the new node to this leaf node, either as a left or

right child

Example: insertion

12

5

9

18

19

15

17

2

13 Insert 13 in the tree

u

Insert Psuedo-code

Assuming node u is created and initialised

Insert (B, u)

v := root(B)

if v = NULL then // an empty tree

root (B) := u

while v NULL do

if u.key = v.key then

// item with key already exists

return false

p := v

if u.key < v.key then

v := v.left

else

v := v.right

if u.key < p.key then

p.left := u

else

p.right := u

return true

Exercise

Insert 95 into the following tree

10

96

99

94

97

Exercise

Insert the following numbers into a Binary

Search Tree:

50, 16, 89, 7, 45, 70, 66, 10 and 95

Exercise

50

16 89

95 70

66

45 7

10

Insert the following numbers into a Binary

Search Tree:

50, 16, 89, 7, 45, 70, 66, 10 and 95

BST Operations - Delete

When we delete a node, we need to consider

how we take care of the children of the deleted

node

This has to be done without violating the

search tree property

Delete Three cases...

Node to be deleted is a leaf node case 0

Node to be deleted has one child case 1

Node to be deleted has two children case 2

Find the node to be deleted

If the node to be deleted is a leaf node then set

the parents link to null

A

D

B G

C E I

F H J

Delete A

A

Node to be deleted is a leaf node

Exercise

Delete 5 from the following tree

94

10 97

5 24

11

17

5

94

10 97

24

11

17

Delete node has one child

find the node to be deleted

The orphan is adopted by the grandparent

D

B G

C E I

F H J

Exercise

Delete 24 from the following tree

94

10 97

5 24

11

17

24

94

10 97

5

11

17

Delete node has two children

Find the node to be deleted

Node to be deleted is

replaced by the left most

node (i.e. the last left child

in the path) in the right sub-

tree, called the successor

D

C G

E I

F H J

H

Promote left most node in right sub-tree

delete node

successor

successor takes place

of delete node

successors parent

adopts successors right child.

Before delete

After delete

Exercise

Delete 10 from the following tree

94

10 97

5 24

11

17

94

11 97

5 24

11

17

Correctness of Tree Delete

How do we know case 2 should go to case 0 or case

1 instead of back to case 2?

Because when node to be deleted has 2 children,

its successor is the minimum in its right subtree,

and that successor has no left child (hence 0 or 1

child)

Equivalently, we could swap with predecessor

instead of successor. It might be good to alternate to

avoid creating lopsided tree

Deletion Pseudo-code

You have to write this complex pseudocode

Level and Height

The level of its node is its

distance from the root

The root is at level 0

The children of the root are at

level 1

Their children are at level 2

The height of the tree is the

level of a leaf in the longest

path from the root plus 1

This is not the standard

definition

E

B F

A D H

C G I

Efficiency of BST Operations

The operations search, insert, and delete

require at most h comparisons, where h is the

height of the tree

Maximum height

Searching, insertion, deletion take n comparisons

Minimum height

Searching, insertion, deletion take log n + 1

comparisons

Insert 9

Insert 1

Insert 8

Insert 4

Insert 5

Insert 6

Insert 7

Its a linked list!

Example of maximum height BST

8

1

9

5

4

height = 7

6

7

Insert 6

Insert 4

Insert 8

Insert 1

Insert 5

Insert 7

Insert 9

Example of minimum height BST

8

4

6

9

1

height = 3

5 7

Maximum Height Tree

Given a binary tree with n nodes, what is the

maximum value it could have for its height?

Ans: n

Minimum Height Tree

Given a binary tree with n nodes, what is the

minimum value it could have for its height?

Assume that n = 2

k

1 for some k

For this choice of n, the minimum height tree

will be a Full Tree

All internal (non-leaf) nodes have 2 children

All leaves are on the same, last level of the tree

Example of minimum height tree

The binary tree below is a full tree of height 3, and it

has 2

3

-1 = 7 nodes

So how can we relate n (7) to the height h (3)?

Level 0: 1 node = 2

0

Level 1: 2 nodes = 2

1

Level 2: 4 nodes = 2

2

Level h 1: 2

h-1

nodes

The total number of nodes is the sum of the nodes

at each level

Recall (from the last slide) that

n = 2

0

+ 2

1

+ + 2

h-1

for some h

Note that h is the height of the tree, so if we can solve for h

we are done

Using math, we know that

2

0

+ 2

1

+ + 2

h-1

= 2

h

1 (geometric sum)

Now we have

n = 2

h

1

Now we also know that a Full Tree of height h has 2

h

-1

nodes

Taking the log

2

of both sides we get

h = log

2

(n+1)

Note the formula on previous slide is for a tree

with 2

k

-1 nodes

Binary trees can have any number of nodes

will this change the formula?

Not significantly (omit proof)

The minimum height for any binary tree with n

nodes is

h = 1 + log n

2

Insert 7

Insert 4

Insert 1

Insert 9

Insert 5

Example of minimum height BST

7

4 9

1 5

height = log(n)+1 =

log(5)+1 = 3

Binary Search Trees

Advantage: If the tree has minimum height, then

every operation takes log n + 1 comparisons

Problem: The tree might get very deep. For example,

when inserting ordered numbers to the tree, the

resulting height will be exactly n

Thus, our goal is to keep the height of a binary search

tree shallow

Such trees are called balanced binary search trees.

Examples are AVL tree, red-black tree

- ssaasstUploaded bySantosh Ranjan
- Comer Btree SurveyUploaded bySebastian Sandon
- FD35.docxUploaded bynavuduri
- ExtentReport ReviewUploaded byshaikameermalik
- cszg623 (12)Uploaded byShivam Shukla
- Types of Binary TreeUploaded byPankaj K Arya
- CS1602-Data Structures and Algorithms-Anna University-Question PapersUploaded bybhuvangates
- ocaml-3.08-refmanUploaded byChiritoiu Tiberiu
- An Engineering Approach to Evolutionary Art (Paper)Uploaded byGarrison LeMasters
- SourceQL Paper 1: Final Report for EECS 433 (Database Systems)Uploaded bySteve Johnson
- Class 21jjUploaded bykazi wahex
- rec05Uploaded byPallav Anand
- Index and HashingUploaded byaparna_savalam485
- Indexing and HashingUploaded byKAMAL KANT KUSHWAHA
- L5_AVLTreesUploaded byAziz
- BinsUploaded byAfeefa
- Tournament TreesUploaded byvinitha_reddy
- Consistency in Database SystemsUploaded bychuasharmaine
- Syntax ToolsUploaded bySkyezine Via Kit Fox
- BirchUploaded byEmília Alves Nogueira
- liwenUploaded byrgopi_83
- da_demUploaded byimadhuryya5023
- solutions_shaastra_2010Uploaded byharta01
- 3.pdfUploaded byMehdi Habibi
- Exam2005 BlankUploaded bytanmaya1991
- B_plus_tree_ch14.docUploaded byHeena Sharma
- Sendmail CfgUploaded byVishy Anand
- 3.Shamod 4-3 BST TraversalUploaded byrobinpt
- Backtracking and Branch and Bound FinalUploaded byPradeep Kumar Reddy Reddy
- riga-treUploaded byaravindmc

- L03 Cryptanalysis OverviewUploaded byashokatmellon
- NSLab-2PortScanningUploaded byashokatmellon
- What After VISAUploaded byashokatmellon
- Hostel Vacating FormUploaded byashokatmellon
- CryptoUploaded byAshok Koduru
- TranslationUploaded byashokatmellon
- TheUploaded byBogdan Ciurea
- it468hw1_f14Uploaded byashokatmellon
- L07 Modular ArithmeticUploaded byashokatmellon
- New Barrons 333 Hf wUploaded byMota Angry Khan
- 501 Quantitative Comparison QuestionsUploaded byjnanmol007
- Software Engineering 1Uploaded byashokatmellon
- How to Answer 23 of the Most Common Interview QuestionsUploaded byashokatmellon

- Aircraft MaintenanceUploaded byPeter George
- Programming ReferenceUploaded byNlvc Mellado
- Cisco PoLRE for HHCUploaded byAnthony Lobosco
- ASEAN Connectivity Project Information SheetsUploaded byASEAN
- Asphalt Core Rock Fill DamUploaded bymzds06
- 200399183-TD-57-v30-03.pdfUploaded byAndrei Korotki
- 2014-03-IEEE-Defining-Spectrum-Efficiency.pdfUploaded byabdel
- Analitical model for erosionUploaded byAnonymous T8aah3Jf
- Infineon-ApplicationNote Managing Firmware Integrity in XMC XMC1000 XMC4000-An-V01 00-EnUploaded byshhmmm
- Man Strong Srt5302Uploaded bybejan
- Custom Content v3_0.pdfUploaded byJan Reumateusz
- Customer NeedsUploaded byAshok Dargar
- Cisco HyperFlex 2.5 With Veeam Integration v1 Lab GuideUploaded byoscarlevitorres9719
- Man Td w8910gUploaded byCeceng Cahyadi
- Kenwood Kdc-mp5029, w5031, w5031y, w531yUploaded bypolydeukes
- 12Uploaded byAbhijeet Bhagavatula
- Introduction to ElectronicsUploaded byMajeah Suazo
- Juzt-Reboot System PresentationUploaded byfollwin
- thyristor.pdfUploaded byMihir Hembram
- Engineering Mathematics-Vector Calculus.pptUploaded byNurul Hanifah
- Final_CN_Unit-1Uploaded byAnonymous numRJFR12C
- sn7400Uploaded byWailani Sherzinger
- ECS TrainingUploaded bySherzad Chem
- STS-41C National Space Transportation Systems Program Mission ReportUploaded byBob Andrepont
- Brake Bleeding (All BMW EVO Systems, '04-'07)Uploaded byja
- 11-25-Job Evaluation- Contract Agreement Mr_ William AUploaded bynoblesavage
- CE2_CEI11_BPD_EN_XX.docxUploaded byindiannewton
- NO SQL STUDIESUploaded byadityaa2064
- NTA855-G4Uploaded byacere18
- Eircom 5010Uploaded byWebman