Binary Search Trees CS16: Introduction to Data Structures & - - PowerPoint PPT Presentation

binary search trees
SMART_READER_LITE
LIVE PREVIEW

Binary Search Trees CS16: Introduction to Data Structures & - - PowerPoint PPT Presentation

Binary Search Trees CS16: Introduction to Data Structures & Algorithms Spring 2020 Outline Binary Search Trees Searching BSTs Adding to BSTs Removing from BSTs by CynthT http://cyntht.deviantart.com/ BST Analysis


slide-1
SLIDE 1

Binary Search Trees

CS16: Introduction to Data Structures & Algorithms Spring 2020

slide-2
SLIDE 2

Outline

  • Binary Search Trees
  • Searching BSTs
  • Adding to BSTs
  • Removing from BSTs
  • BST Analysis
  • Balancing BSTs
by CynthT http://cyntht.deviantart.com/
slide-3
SLIDE 3

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
slide-4
SLIDE 4

Searching a BST

  • Find 11
  • Each comparison tells us whether to go left or right
4 13 7 20 15 24 5 10 8 11 11 < 13 11 > 7 11 > 10
slide-5
SLIDE 5

Searching a BST

  • What if item isn’t in tree?
  • Find 14
5 13 7 20 15 24 5 10 8 11 14 > 13 14 < 20 reached leaf w/o finding it so not in tree
slide-6
SLIDE 6

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
slide-7
SLIDE 7

Inserting in a BST

  • To insert, perform a search and add as new leaf
  • Insert 17
7 13 7 20 15 24 5 10 8 11 17 > 13 17 < 20 17 > 15 17
slide-8
SLIDE 8

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 child node.addRight(toInsert) else: insert(node.right, toInsert)
slide-9
SLIDE 9

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 24 5 10 8 11 17
slide-10
SLIDE 10

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 24 5 10 8 11 17
slide-11
SLIDE 11

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 24 5 10 8 11 9
slide-12
SLIDE 12

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 24 5 10 8 11 9 successor(node): curr = node.right while (curr.left != null): curr = curr.left return curr
slide-13
SLIDE 13

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 24 5 10 8 11 9 8
slide-14
SLIDE 14

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 child node.parent.left = node.left else: node.parent.right = node.left else if node only has right child: # case 2b if node.parent.left == node: node.parent.left = node.right else: node.parent.right = node.right else: # case 3 (node has two children) nextNode = successor(node) node.data = nextNode.data #replace w/ nextNode remove(nextNode) # nextNode has at most one child
slide-15
SLIDE 15

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
slide-16
SLIDE 16

Binary Search Tree — Remove( )

16

2 min

Activity #1

slide-17
SLIDE 17

Binary Search Tree — Remove( )

17

2 min

Activity #1

slide-18
SLIDE 18

Binary Search Tree — Remove( )

18

1 min

Activity #1

slide-19
SLIDE 19

Binary Search Tree — Remove( )

19

0 min

Activity #1

slide-20
SLIDE 20

Binary Search Tree Analysis

  • How fast are BST
  • perations?
  • 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 tree
B A D C 13 7 20 15 24 5 10
slide-21
SLIDE 21

Binary Search Trees — Rotations

  • We can re-balance unbalanced trees w/ tree rotations
21 A B C A B C A B C A C B
  • In-order traversal of all 3 trees is
  • so BST order is preserved

Beyond CS16, But good to know