binary search trees - brown universitybinary search trees — rotations ‣ we can re-balance...

21
Binary Search Trees CS16: Introduction to Data Structures & Algorithms Spring 2020

Upload: others

Post on 28-Jun-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search TreesCS16: Introduction to Data Structures & Algorithms

Spring 2020

Page 2: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Outline‣ Binary Search Trees

‣ Searching BSTs

‣ Adding to BSTs

‣ Removing from BSTs

‣ BST Analysis

‣ Balancing BSTs

by CynthT http://cyntht.deviantart.com/

Page 3: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Trees‣ Binary trees with special property‣ For each node‣ left descendants have lower value than node

‣ right descendants have higher value than node

‣ In-order traversal gives nodes in order

3

Page 4: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Searching a BST

‣ Find 11‣ Each comparison tells us whether to go left or right

4

13

7 20

15 245 10

8 11

11 < 13

11 > 7

11 > 10

Page 5: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Searching a BST

‣ What if item isn’t in tree?‣ Find 14

5

13

7 20

15 245 10

8 11

14 > 13

14 < 20

reached leaf w/o finding it so not in tree

Page 6: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree — Find( )

6

function find(node, toFind): if node.data == toFind: return node

else if toFind < node.data and node.left != null: return find(node.left, toFind)

else if toFind > node.data and node.right != null: return find(node.right, toFind)

return null

Page 7: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Inserting in a BST

‣ To insert, perform a search and add as new leaf‣ Insert 17

7

13

7 20

15 245 10

8 11

17 > 13

17 < 20

17 > 15

17

Page 8: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree — Insert( )

8

function insert(node, toInsert):if node.data == toInsert: # data already in tree

return

if toInsert < node.data:if node.left == null: # add as left child

node.addLeft(toInsert)else:

insert(node.left, toInsert)else:

if node.right == null: # add as right childnode.addRight(toInsert)

else:insert(node.right, toInsert)

Page 9: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Removing from a BST‣ Can be tricky

‣ Three cases to consider

‣ Removing a leaf: easy, just do it

‣ Removing internal node w/ 1 child (e.g., 15)

‣ Removing internal node w/ 2 children (e.g., 7)

13

7 20

15 245 10

8 11 17

Page 10: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Removing from a BST - Case #2‣ Removing internal node w/ 1 child

‣ Strategy

‣ “Splice out” node by connecting its parent to its child

‣ Example: remove 15

‣ set parent’s left pointer to 17

‣ remove 15’s pointer

‣ no more references to 15 so erased (garbage collected)

‣ BST order is maintained

13

7 20

15 245 10

8 11 17

Page 11: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Removing from a BST - Case #3‣ Removing internal node w/ 2 children

‣ Replace node w/ successor

‣ successor: next largest node

‣ Delete successor

‣ Successor a.k.a. the in-order successor

‣ Example: remove 7

‣ What is successor of 7?

13

7 20

17 245 10

8 11

9

Page 12: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Removing from a BST - Case #3‣ Since node has 2 children…

‣ …it has a right subtree

‣ Successor is leftmost node in right subtree

‣ 7’s successor is 8

13

7 20

17 245 10

8 11

9

successor(node):curr = node.rightwhile (curr.left != null):

curr = curr.leftreturn curr

Page 13: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Removing from a BST - Case #3‣ Now, replace node with successor

‣ Observation

‣ Successor can’t have left sub-tree

‣ …otherwise its left child would be successor

‣ so successor only has right child

‣ Remove successor using Case #1 or #2

‣ Here, use case #2 (internal w/ 1 child)

‣ Successor removed and BST order restored

13

7 20

17 245 10

8 11

9

8

Page 14: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree — Remove( )

14

function remove(node):if node has no children: # case 1

node.parent.removeChild(node)else if node only has left child: # case 2a

if node.parent.left == node: # node is a left childnode.parent.left = node.left

else:node.parent.right = node.left

else if node only has right child: # case 2bif node.parent.left == node:

node.parent.left = node.rightelse:

node.parent.right = node.rightelse: # case 3 (node has two children)

nextNode = successor(node)node.data = nextNode.data #replace w/ nextNoderemove(nextNode) # nextNode has at most one child

Page 15: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Successor vs. Predecessor‣ In Remove( ) ‣ OK to remove in-order predecessor

instead of in-order successor

‣ Randomly picking between the two keeps tree balanced

‣ In Case #3‣ Predecessor is rightmost node of left subtree

15

Page 16: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree — Remove( )

162 minActivity #1

Page 17: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree — Remove( )

172 minActivity #1

Page 18: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree — Remove( )

181 minActivity #1

Page 19: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree — Remove( )

190 minActivity #1

Page 20: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Tree Analysis‣ How fast are BST

operations?‣ Given a tree, what is the worst-

case node to find/remove?

‣ What is the best-case tree?‣ a balanced tree

‣ What is the worst-case tree?

‣ a completely unbalanced treeB

A

D

C

13

7 20

15 245 10

Page 21: Binary Search Trees - Brown UniversityBinary Search Trees — Rotations ‣ We can re-balance unbalanced trees w/ tree rotations 21 A B C A B C A B C B A C ‣ In-order traversal of

Binary Search Trees — Rotations‣ We can re-balance unbalanced trees w/ tree rotations

21

A

B CA

B

C

A

B

C

A CB

‣ In-order traversal of all 3 trees is

‣ so BST order is preserved

Beyond CS16,But good to

know