• No results found

Binary Search Trees. Ric Glassey

N/A
N/A
Protected

Academic year: 2021

Share "Binary Search Trees. Ric Glassey"

Copied!
30
0
0

Loading.... (view fulltext now)

Full text

(1)

Binary Search Trees

Ric Glassey

glassey@kth.se

(2)

Outline

Binary Search Trees

Aim: Demonstrate how a BST can maintain order

and fast performance relative to its height

Properties

Operations

Min/Max

Search

Insertion

*Deletion

Summary of performance

2

(3)

PROPERTIES

(4)

Binary Search Tree

Ordered binary tree

Binary search tree property

Keys in left sub-tree of P are < k

Keys in right sub-tree of P are > k

4

k

> k < k

(5)

Example Binary Search Tree

5 44 17 88 65 97 8 32 28 41

(6)

Applications of BST

Store a set of ordered keys

Fast operations and maintain order

Priority Queues

Ordered Maps/Dictionaries

e.g. Java’s TreeMap

n.b. if order is not important, regular hash tables still offer

better average time complexity O(1)

(7)

MIN/MAX

(8)

Finding Min/Max in a BST

What is the fastest path to find the

minimum

and

maximum

values in a BST?

8 44 17 88 65 97 8 32 28 41

(9)

Finding Min/Max in a BST

9

min(node)  

 while  node.left  !=  null:  

 

 node  =  node.left  

 return  node  

 

 

max(node)  

 while  node.right  !=  null:  

 

 node  =  node.right  

 return  node  

(10)

Pathological BSTs

10

Order  of  inserts  cannot  be  predicted  in  advance  

Pathological  cases  can  emerge  where  some  opera5ons  will  take  O(n)   Solu5ons  include  Randomised  BSTs/Treaps  and  Balancing  Procedures  

44 46 54 67 71 88 insert(54) insert(46) insert(67) insert(71) insert(88)

(11)

SEARCH

(12)

Finding x within a BST

x = 41

12 44 17 88 65 97 8 32 28 41

(13)

Finding x within a BST

x = 41

13 44 17 88 65 97 8 32 28 41 x < 44 x > 17 x > 32 x = 41

(14)

BST Search Algorithms

Recursive approach:

Iterative option (

see homework

)

Why is this the better option?

14

search(node,  x)  

 if  node  ==  null  or  x  ==  node.key:      return  node      if  x  <  node.key:      return  search(node.left,  x)        else:      return  search(node.right,  x)  

(15)

BST Search Algorithms

Further operations use

search(n,x)

as a

sub-routine

insert(k, v)

remove(k)

As a consequence, all primary operations on a

BST are considered

fast

, proportional to the

height of the tree

An exception is in-order-traversal which should be

expected to be O(n) as all nodes ‘must’ be visited

(16)

INSERTION

(17)

Insertion into a BST

Search for key

Two cases:

Key exists

, update value

Key does not exist

, extend leaf at end of failed

search with new node (key, value)

(18)

Insertion into a BST: insert(68, value)

18 44 17 88 80 65 97 8 32 54 82 93 28 76 21 29

(19)

Insertion into a BST: Find position

19 44 17 88 80 65 97 8 32 54 82 93 28 76 21 29 ??? 68 > 44 68 < 88 68 > 65 68 < 76 68 < 82

(20)

Insertion into a BST: Extend Leaf

20 44 17 88 80 65 97 8 32 54 82 93 28 76 21 29 68 > 44 68 < 88 68 > 65 68 < 76 68 < 82 68

(21)

BST Insertion Algorithm

We can use search(x) as a

subroutine

to

simplify insertion into a BST

n.b. homework demands iterative solution!

21

insert(key,  value)  

 

leaf  =  

search(root,  key)  

 if  key  ==  leaf.key:  

 

 leaf.value  =  value  

 else  if  key  <  leaf.key:  

 

 leaf.left  =  node(key,  value)  

 else:  

(22)

*DELETION

(23)

*Deletion for a BST

Insertions always occur at a leaf (trivial)

Deletions can occur anywhere within a tree

Three cases to consider for

remove(z)

:

Case 1: z has no children

Case 2: z has one child

Case 3: z has two children

(24)

*Case 1: z has no children

Simply remove from tree by setting z’s parent’s

pointer to null

24 z

(25)

*Case 2: z has one child

25 Z LC R LC R Z RC R RC R

(26)

*Case 3: z has two children (i)

26 R Z LC X Y R X LC Y

(27)

*Case 3: z has two children (ii)

27

We  transplant  Y  with  X  first,  and  relink  W  to  X  to  maintain  the  BST-­‐property   Finally,  we  link  Z’s  LC  to  Y  and  Y  to  the  root  R  

Hint:  subs5tute  valid  integers  to  show  why  this  works  

R Z LC X Y R Z LC W Y W X R Y LC X W

(28)

Summary of BST Performance

28 Data   Structure         Opera5on   Array   Linked  

List†     Hash  Table   Binary  Search  Tree  

average   worst   expected   worst  

Search*   O(1)   O(n)   O(1)   O(n)   O(h)   O(n)   Insert   O(n)   O(1)   O(1)   O(n)   O(h)   O(n)   Delete   O(n)   O(1)   O(1)   O(n)   O(h)   O(n)  

*  Index  or  Key  based  search   †  Assume  doubly  linked  list    

(29)

Readings

• 

Algorithms and Data Structures

*required*

Stefan Nilsson’s text on the Binary Search Tree

http://www.nada.kth.se/~snilsson/algoritmer/trad/

• 

Introduction to Algorithms, 3

rd

Edition

Chapter 12: Binary Search Trees

Full text available via KTH Library

•  http://kth-primo.hosted.exlibrisgroup.com/

KTH:KTH_SFX2560000000068328

• 

Data Structures and Algorithms in Java, 6

th

Edition

Goodrich et al.

Chapter 11: Search Trees

Full text available via KTH Library

•  http://kth-primo.hosted.exlibrisgroup.com/

KTH:KTH_SFX3710000000333147

(30)

Feedback!

This week, I will mostly be asking about:

Topics of today’s lecture

Analysis of algorithms

Tipjar

Survey will appear at this link

http://bit.ly/1EfJE91

Also on the course web page after this lecture

n.b. I will switch survey service for P4 :-)

References

Related documents

(Convince yourself that a left subtree with a balance factor equal to 0 at its root cannot occur in this case.) The necessary rebalancing operations

• In order to insert a new node in a binary tree, we have to be at a node with a vacant left or right child. • This is performed in the same way

• In order to insert a new node in a binary tree, we have to be at a node with a vacant left or right child. • This is performed in the same way

∗ Sorted Set: binary search trees, red-black trees, B-trees, sorted arrays or linked lists. – Unordered Set:

Customer check-up see Maintenance schedules chapter Change of descaling filter see menu service date Descaling see service routines menu.. Menu PIN 

Binary search trees provide an excellent structure for searching Binary search trees provide an excellent structure for searching a list and at the same time for inserting

Mutating binary trees arise naturally in our application (unlike classical binary search) because each level in the binary search has multiple entries stored in a hash table, as

Theorem 5 (Static Finger Theorem) Consider a sequence of m accesses in an n-node splay tree. Let a[j] denote the jth