BST

© All Rights Reserved

11 views

BST

© All Rights Reserved

- Huffman Coding
- rgpv syllabus it111 data structure-i.pdf
- Workflow Management - Models, Methods and Systems
- question bank-plc _012310032535_1
- Comp Sci - IJCSEITR - A Prototype - Vishnu Mishra-ALL 9
- README-1
- Ppt on Binomial Trees
- Oracle Fusion HCM Absence Management-EP, Abs Plans, Geo Hierarchy
- electromagnetic
- 3. Various Data Structure (2)
- Retro Synthesis Tree
- Assignment 8
- Strategy on T, I, F Operators. A Kernel Infrastructure in Neutrosophic Logic
- SYS600_IEC 61850 Master Protocol OPC_756632_ENb
- plan and package
- B+Tree
- Data Structures Objective Questions and Answers _ Set-3 _ Searchcrone
- cs301MCQSBYMOAZ
- TournamentTrees.pdf
- 11-Trees_1pps.pdf

You are on page 1of 27

CS 477/677

Binary Search Trees

Instructor: George Bebis

(Appendix B5.2, Chapter 12)

2

Binary Search Trees

Tree representation:

A linked data structure in which

each node is an object

Node representation:

Key field

Satellite data

Left: pointer to left child

Right: pointer to right child

p: pointer to parent (p [root [T]] =

NIL)

Satisfies the binary-search-tree

property !!

Left child Right child

L R

parent

key data

3

Binary Search Tree Property

Binary search tree property:

If y is in left subtree of x,

then key [y] key [x]

If y is in right subtree of x,

then key [y] key [x]

2

3

5

5

7

9

4

Binary Search Trees

Support many dynamic set operations

SEARCH, MINIMUM, MAXIMUM, PREDECESSOR,

SUCCESSOR, INSERT, DELETE

Running time of basic operations on binary

search trees

On average: (lgn)

The expected height of the tree is lgn

In the worst case: (n)

The tree is a linear chain of n nodes

5

Worst Case

(n)

6

Traversing a Binary Search Tree

Inorder tree walk:

Root is printed between the values of its left and

right subtrees: left, root, right

Keys are printed in sorted order

Preorder tree walk:

root printed first: root, left, right

Postorder tree walk:

root printed last: left, right, root

2

3

5

5

7

9

Preorder: 5 3 2 5 7 9

Inorder: 2 3 5 5 7 9

Postorder: 2 5 3 9 7 5

7

Traversing a Binary Search Tree

Alg: INORDER-TREE-WALK(x)

1. if x NIL

2. then INORDER-TREE-WALK ( left [x] )

3. print key [x]

4. INORDER-TREE-WALK ( right [x] )

E.g.:

Running time:

(n), where n is the size of the tree rooted at x

2

3

5

5

7

9

Output: 2 3 5 5 7 9

8

Searching for a Key

Given a pointer to the root of a tree and a key k:

Return a pointer to a node with key k

if one exists

Otherwise return NIL

Idea

Starting at the root: trace down a path by comparing k with the

key of the current node:

If the keys are equal: we have found the key

If k < key[x] search in the left subtree of x

If k > key[x] search in the right subtree of x

2

3

4

5

7

9

9

Example: TREE-SEARCH

Search for key 13:

15 6 7 13

3

2 4

6

7

13

15

18

17 20

9

10

Searching for a Key

Alg: TREE-SEARCH(x, k)

1. if x = NIL or k = key [x]

2. then return x

3. if k < key [x]

4. then return TREE-SEARCH(left [x], k )

5. else return TREE-SEARCH(right [x], k )

Running Time: O (h),

h the height of the tree

2

3

4

5

7

9

11

Finding the Minimum

in a Binary Search Tree

Goal: find the minimum value in a BST

Following left child pointers from the root,

until a NIL is encountered

Alg: TREE-MINIMUM(x)

1. while left [x] NIL

2. do x left [x]

3. return x

Running time: O(h), h height of tree

3

2 4

6

7

13

15

18

17 20

9

Minimum = 2

12

Finding the Maximum

in a Binary Search Tree

3

2 4

6

7

13

15

18

17 20

9

Maximum = 20

Goal: find the maximum value in a BST

Following right child pointers from the root,

until a NIL is encountered

Alg: TREE-MAXIMUM(x)

1. while right [x] NIL

2. do x right [x]

3. return x

Running time: O(h), h height of tree

13

Successor

Def: successor (x ) = y, such that key [y] is the

smallest key > key [x]

E.g.: successor (15) =

successor (13) =

successor (9) =

Case 1: right (x) is non empty

successor (x ) = the minimum in right (x)

Case 2: right (x) is empty

go up the tree until the current node is a left child:

successor (x ) is the parent of the current node

if you cannot go further (and you reached the root):

x is the largest element

3

2 4

6

7

13

15

18

17 20

9

17

15

13

x

y

14

Finding the Successor

Alg: TREE-SUCCESSOR(x)

1. if right [x] NIL

2. then return TREE-MINIMUM(right [x])

3. y p[x]

4. while y NIL and x = right [y]

5. do x y

6. y p[y]

7. return y

Running time: O (h), h height of the tree

3

2 4

6

7

13

15

18

17 20

9

y

x

15

Predecessor

Def: predecessor (x ) = y, such that key [y] is the

biggest key < key [x]

E.g.: predecessor (15) =

predecessor (9) =

predecessor (7) =

Case 1: left (x) is non empty

predecessor (x ) = the maximum in left (x)

Case 2: left (x) is empty

go up the tree until the current node is a right child:

predecessor (x ) is the parent of the current node

if you cannot go further (and you reached the root):

x is the smallest element

3

2 4

6

7

13

15

18

17 20

9

13

7

6

x

y

16

13

Insertion

Goal:

Insert value v into a binary search tree

Idea:

If key [x] < v move to the right child of x,

else move to the left child of x

When x is NIL, we found the correct position

If v < key [y] insert the new node as ys left child

else insert it as ys right child

Begining at the root, go down the tree and maintain:

Pointer x : traces the downward path (current node)

Pointer y : parent of x (trailing pointer )

2

1 3

5

9

12

18

15 19

17

Insert value 13

17

Example: TREE-INSERT

2

1 3

5

9

12

18

15 19

17

x=root[T], y=NIL

Insert 13:

2

1 3

5

9

12

18

15 19

17

x

2

1 3

5

9

12

18

15 19

17

x

x = NIL

y = 15

13

2

1 3

5

9

12

18

15 19

17

y

y

18

Alg: TREE-INSERT(T, z)

1. y NIL

2. x root [T]

3. while x NIL

4. do y x

5. if key [z] < key [x]

6. then x left [x]

7. else x right [x]

8. p[z] y

9. if y = NIL

10. then root [T] z Tree T was empty

11. else if key [z] < key [y]

12. then left [y] z

13. else right [y] z

2

1 3

5

9

12

18

15 19

17 13

Running time: O(h)

19

Deletion

Goal:

Delete a given node z from a binary search tree

Idea:

Case 1: z has no children

Delete z by making the parent of z point to NIL

15

16

20

18 23

6

5

12

3

7

10 13

delete

15

16

20

18 23

6

5

12

3

7

10

z

20

Deletion

Case 2: z has one child

Delete z by making the parent of z point to zs child,

instead of to z

15

16

20

18 23

6

5

12

3

7

10 13

delete

15

20

18 23

6

5

12

3

7

10

z

21

Deletion

Case 3: z has two children

zs successor (y) is the minimum node in zs right subtree

y has either no children or one right child (but no left child)

Delete y from the tree (via Case 1 or 2)

Replace zs key and satellite data with ys.

15

16

20

18 23

6

5

12

3

7

10 13

delete

z

y

15

16

20

18 23

7

6

12

3

10 13

6

22

TREE-DELETE(T, z)

1. if left[z] = NIL or right[z] = NIL

2. then y z

3. else y TREE-SUCCESSOR(z)

4. if left[y] NIL

5. then x left[y]

6. else x right[y]

7. if x NIL

8. then p[x] p[y]

z has one child

z has 2 children

15

16

20

18 23

6

5

12

3

7

10 13

y

x

23

TREE-DELETE(T, z) cont.

9. if p[y] = NIL

10. then root[T] x

11. else if y = left[p[y]]

12. then left[p[y]] x

13. else right[p[y]] x

14. if y z

15. then key[z] key[y]

16. copy ys satellite data into z

17. return y

15

16

20

18 23

6

5

12

3

7

10 13

y

x

Running time: O(h)

24

Binary Search Trees - Summary

Operations on binary search trees:

SEARCH O(h)

PREDECESSOR O(h)

SUCCESOR O(h)

MINIMUM O(h)

MAXIMUM O(h)

INSERT O(h)

DELETE O(h)

These operations are fast if the height of the tree

is small otherwise their performance is similar

to that of a linked list

25

Problems

Exercise 12.1-2 (page 256) What is the

difference between the MAX-HEAP property and

the binary search tree property?

Exercise 12.1-2 (page 256) Can the min-heap

property be used to print out the keys of an n-

node tree in sorted order in O(n) time?

Can you use the heap property to design an

efficient algorithm that searches for an item in a

binary tree?

26

Problems

Let x be the root node of a binary search tree

(BST). Write an algorithm BSTHeight(x) that

determines the height of the tree. What would be

its running time?

Alg: BSTHeight(x)

if (x==NULL)

return -1;

else

return max (BSTHeight(left[x]),

BSTHeight(right[x]))+1;

27

Problems

(Exercise 12.3-5, page 264) In a binary search tree, are

the insert and delete operations commutative?

Insert:

Try to insert 4 followed by 6, then insert 6 followed by 4

Delete

Delete 5 followed by 6, then 6 followed by 5 in the following tree

4

2 6

5 8

7

4

2 8

7

4

2 7

8

- Huffman CodingUploaded byTanmay Baranwal
- rgpv syllabus it111 data structure-i.pdfUploaded bysatti2628
- Workflow Management - Models, Methods and SystemsUploaded byflyinzesky
- question bank-plc _012310032535_1Uploaded byAnami Thakur
- Comp Sci - IJCSEITR - A Prototype - Vishnu Mishra-ALL 9Uploaded byglobal-marketin8184
- README-1Uploaded bydrackner
- Ppt on Binomial TreesUploaded byapi-3844034
- Oracle Fusion HCM Absence Management-EP, Abs Plans, Geo HierarchyUploaded byvallipoornima
- 3. Various Data Structure (2)Uploaded byNeha Verma
- electromagneticUploaded bylim hy
- Retro Synthesis TreeUploaded byFasi Yej
- Assignment 8Uploaded bySonu Parmani
- Strategy on T, I, F Operators. A Kernel Infrastructure in Neutrosophic LogicUploaded byIlieCraciun
- SYS600_IEC 61850 Master Protocol OPC_756632_ENbUploaded byOrlando Romani
- plan and packageUploaded bysushil@ird
- B+TreeUploaded byBullet Valentine
- Data Structures Objective Questions and Answers _ Set-3 _ SearchcroneUploaded byMultan Singh Bhati
- cs301MCQSBYMOAZUploaded byMuhammad Rameez Khalid
- TournamentTrees.pdfUploaded byИРадојичић
- 11-Trees_1pps.pdfUploaded byClark Fan
- Sh Neiderman 2011 InnovationUploaded byRichard Sapienza
- hw1-sol (1).pdfUploaded byAlex Wang
- GENEREAL QA.odtUploaded byMary Jansi
- Fr MobileuserUploaded byomda4uu
- AIUploaded byKirtika Agrawal
- MELJUN CORTES Instructional Manual Data StructuresUploaded byMELJUN CORTES, MBA,MPA
- C programming question setUploaded byPradip Kr Sharma
- DnDJTree.pdfUploaded byAnonymous lubWipsrW
- Sigal It 2012Uploaded byIon Cimirinschi
- dbmsUploaded bymayur_mech

- The Quintile & Bi-QuintileUploaded bymuucool
- C ArrayUploaded bymuucool
- Nyul Fuzzy SegmentationUploaded bymuucool
- Are a ProcessUploaded byJack Chan
- Kalasarpa Dosham - AstrologyForUUploaded bymuucool
- Lecture 16Uploaded bymuucool
- lec9Uploaded bymuucool
- Lec02 FilterUploaded bymuucool
- Rahu FactsUploaded bymuucool
- C++ Operator Precedence - cppreferenceUploaded bymuucool
- 09 Image Filtering Edge Detection 09Uploaded bycrazyleo74
- Speckle Reducing Anisotropic DiffusionUploaded bymuucool
- Stopwath c SharpUploaded bymuucool
- Stack ADTUploaded bymuucool
- Inputoutput With FilesUploaded bymuucool
- MIT6 096IAP11 ProjectUploaded bySaugat Kar
- Sun Astrowiki EnUploaded bymuucool
- Crop text out of binary ...CV C++ - Stack OverflowUploaded bymuucool
- MIT6_096IAP11_assn02Uploaded bySaugat Kar
- IT Final Upto 4th Year Syllabus 14.03.14Uploaded byBashitwa Roy
- NetUploaded bymuucool
- CodesUploaded bymuucool
- Opencv TutorialUploaded byprabhakargoli
- datastruct03Uploaded bymuucool
- 7tema_T120M013Uploaded bymuucool
- Session 06 GraphUploaded bymuucool
- Astrology and EducationUploaded bymuucool
- GraphUploaded bymuucool

- Grade 4 Lesson PlanUploaded byBea Datay
- intervention program peer review rubricUploaded byapi-278394251
- Coconut Fibre FllerUploaded byAshok Kumar Bharathidasan
- SSCDANN015PAAB5Uploaded bySaulo Cruz
- Lecture5.B. HysteresisUploaded byrahul vaish
- Building a Stone Foundation - Part 2 Batter BoardsUploaded bySilentMan
- Airbus FAST Special Edition Oct2015Uploaded byrenjithaero
- 70_271 - CopyUploaded byNaw Yermaw
- Communications S&SUploaded byML Narasimham
- National Board Inspection Code vs API 510Uploaded bycesquimon
- Example Aircraft Design ProcedureUploaded byVijay Gorfad
- PackagesUploaded byFizzah Zahid
- Mca SyllabusUploaded byNiket Shinde
- CV plant manager and phot.docUploaded byJahidul Islam
- X-Plane Helicopter ManualUploaded byErnani Kern
- annotatedbibliographyfinalUploaded byapi-276064910
- A Two-dimentional Intepolation Function for Irregalarly-spaced DataUploaded byMuhammad Hmmad
- FTM_Issue_2_Rev_4_Nov_2018_.pdfUploaded bys adak
- 123121564Uploaded byasyrafmuhdd
- Case Laws - NoveltyUploaded byKrishna Marakkagari
- Giant objects around the SunUploaded byphil_yates
- Aloka SSD-5500 - Service manual.pdfUploaded byvelasquez diaz
- Zenit-122 ManualUploaded bymushyhsum
- An Oscillating StrawUploaded bycheefsy
- Early_Age Shrinkage of ConcreteUploaded byTim Lin
- Ellipsoid PotentialUploaded byEduardoHenriquez
- HysteresisLoop.pdfUploaded bymedooman
- Corundum, Aluminum Oxide, Alumina, 99Uploaded byDanielle Fernandes
- sauerdanfoss_series_s45_catalogue_en_520l0519.pdfUploaded byedgar_retuerto78
- UsbFix ReportUploaded byLập Duy