Algorithms and Data Structures Lecture 6 Binary Search Trees I - - PowerPoint PPT Presentation

algorithms and data structures
SMART_READER_LITE
LIVE PREVIEW

Algorithms and Data Structures Lecture 6 Binary Search Trees I - - PowerPoint PPT Presentation

Algorithms and Data Structures Lecture 6 Binary Search Trees I Fabian Kuhn Algorithms and Complexity Fabian Kuhn Algorithms and Data Structures Abstract Data Types : Dictionary Dictionary: (also: maps, associative arrays) Manages a set of


slide-1
SLIDE 1

Algorithms and Data Structures Fabian Kuhn

Lecture 6 Binary Search Trees I

Algorithms and Data Structures

Fabian Kuhn Algorithms and Complexity

slide-2
SLIDE 2

Algorithms and Data Structures Fabian Kuhn

Dictionary: (also: maps, associative arrays)

  • Manages a set of elements, where each element is represented by

a unique key Operations

  • create

: creates a new empty dictionary

  • D.insert(key, value) : inserts a new (key,value)-pair

– If there already exists an entry for key, the old entry is replaced

  • D.find(key)

: returns entry for the given key

– If such an entry exists (otherwise a default value is returned)

  • D.delete(key)

: deletes the entry for the given key Can be implemented with hash tables in (amortized) constant time!

2

Abstract Data Types : Dictionary

slide-3
SLIDE 3

Algorithms and Data Structures Fabian Kuhn

Dictionary:

Additional possible operations:

  • D.minimum()

: returns smallest key in the data struture

  • D.maximum()

: returns largest key in the data structure

  • D.successor(key)

: returns next larger key

  • D.predecessor(key) : returns next smaller key
  • D.getRange(k1, k2) : returns all entries with keys in the

interval [k1,k2] These operations cannot be implemented efficiently with a hash table.

3

Abstract Data Types : Dictionary

slide-4
SLIDE 4

Algorithms and Data Structures Fabian Kuhn

Search for key 19:

4

Binary Search Trees : Idea

2 3 4 6 9 12 15 17 19 24 27 29 16 20 18

slide-5
SLIDE 5

Algorithms and Data Structures Fabian Kuhn

  • Use the search tree of the binary search as data structure

5

Binary Search Trees : Idea

16 6 20 3 12 18 27 2 4 9 15 17 19 24 29

root

slide-6
SLIDE 6

Algorithms and Data Structures Fabian Kuhn

TreeElement: Implementation: in the same way as for list elements

6

Binary Search Tree : Elements

parent key, value left right

slide-7
SLIDE 7

Algorithms and Data Structures Fabian Kuhn

  • Binary search trees do not always need to be

nice and symmetric…

7

Binary Search Trees

Source: [CLRS]

slide-8
SLIDE 8

Algorithms and Data Structures Fabian Kuhn 8

Find in a Binary Search Tree

Search for key 𝒚

  • Use binary search

– That’s way it’s called a binary search tree …

Running time: 𝑃 depth of tree

current = root while current is not None and current.key != x: if current.key > x: current = current.left else: current = current.right

At the end:

  • Key 𝑦 not in the tree : current == None
  • Key 𝑦 found

: current.key == x

slide-9
SLIDE 9

Algorithms and Data Structures Fabian Kuhn 9

Suche Minimum / Maximum

Find smallest element in a binary search tree

  • All smaller elements are always

in the left subtree. current = root while current.left is not None: current = current.left

slide-10
SLIDE 10

Algorithms and Data Structures Fabian Kuhn

Ordering in tree: 𝑩 < 𝒜 < 𝑪 < 𝒛 < 𝑫 < 𝒚 < 𝑬 < 𝒙 < 𝑭

  • If subtree 𝐸 exists, then

the successor of 𝑦 is the smallest key in 𝐸.

  • Otherwise, 𝑥 is the

successor.

10

Search for Successor

𝒛 𝒚 𝒜 𝒙

𝑩 𝑪 𝑬 𝑫 𝑭

slide-11
SLIDE 11

Algorithms and Data Structures Fabian Kuhn 11

Search for Successor

Find successor of a node 𝒗 (assumption: 𝑣 ≠ None)

if u.right is not None: # min in right subtree current = u.right while current.left is not None: current = current.left return current else # find first node towards root s.t. u is in left subtree current = u parent = current.parent while parent is not None and current == parent.right: current = parent parent = current.parent return parent

Running time: 𝑃 depth of tree

slide-12
SLIDE 12

Algorithms and Data Structures Fabian Kuhn 12

Search for Predecessor

Find predecessor of a node 𝒗 (assumption: 𝑣 ≠ None)

if u.left is not None: # max in left subtree current = u.left while current.right is not None: current = current.right return current else # find first node towards root s.t. u is in right subtree current = u parent = current.parent while parent is not None and current == parent.left: current = parent parent = current.parent return parent

Running time: 𝑃 depth of tree

slide-13
SLIDE 13

Algorithms and Data Structures Fabian Kuhn 13

Inserting a Key

Insert keys 5, 1, 14, 6.5, 19 …

𝟔 𝟐 𝟐𝟓 𝟕. 𝟔 𝟐𝟘

Running time: 𝑃 depth of tree

slide-14
SLIDE 14

Algorithms and Data Structures Fabian Kuhn 14

Inserting a key 𝑦 with value 𝑏

if root is None: root = new TreeElement(x, a, None, None, None) else: current = root; parent = None while current is not None and current.key != x: parent = current if x < current.key: current = current.left else: current = current.right if current is None: if x < parent.key: parent.left = new TreeElement(x, a, parent, None, None) else: parent.right = new TreeElement(x, a, parent, None, None) else: current.value = a

(key 𝑦 is not contained in tree) binary search (key 𝑦 is already contained, replace value)

key value parnet right child left child

slide-15
SLIDE 15

Algorithms and Data Structures Fabian Kuhn

Delete key 𝒚, simple cases:

  • Key 𝑦 is in a leaf node 𝑣 of the tree

– leaf = node has no children

  • Node with key 𝑦 has only 1 child

15

Deleting a Key I

𝒚

𝒙

𝒚

𝒙

w.right = None w.left = None 𝒘

𝒙 𝒚 None

𝒘

𝒙 𝒚 None delete 𝒚

𝒘

𝒙

w.left = v Case, where 𝑦 is the right child of 𝑥 is symmetric.

slide-16
SLIDE 16

Algorithms and Data Structures Fabian Kuhn 16

Deleting a Key II

Delete key 𝒚, node has two children:

  • Delete key 6:

15 6 18 17 20 8 3 7 4 2 13 9 delete key 6 4 predecessor 7 successor

slide-17
SLIDE 17

Algorithms and Data Structures Fabian Kuhn 17

Deleting a Key III

Delete key 𝒚, node has two children:

  • Predecessor is largest key in left subtree.

– Predecessor has no right child.

  • Successor is smallest key in right subtree.

– Successor has no left child.

  • Write key and data of precedessor (or alternatively successor)

to the node of key 𝑦

  • Delete predecessor / successor node

– Predecessor / successor is either a leaf or it has only one child.

slide-18
SLIDE 18

Algorithms and Data Structures Fabian Kuhn 18

Deleting a Key IV

Delete key 𝒚:

  • 1. Find node 𝑣 with 𝑣.key = 𝑦

– as usual, by using binary search

  • 2. If 𝑣 does not have 2 children, delete node 𝑣

– Assumption: 𝑤 is parent of 𝑣, 𝑣 is left child of 𝑤 (other case is symmetric) – If 𝑣 is a leaf, we do 𝑤.left = None – If 𝑣 has one child 𝑥, we do 𝑤.left = 𝑥

  • 3. If 𝑣 has two children, determine predecessor node 𝑤

– also works with successor node

  • 4. Set 𝑣.key = 𝑤.key and 𝑣.data = 𝑤.data
  • 5. Delete node 𝑤 (in the same way as deleting 𝑣 above)

– Node 𝑤 has at most 1 child!

Running time: 𝑃 depth of tree

slide-19
SLIDE 19

Algorithms and Data Structures Fabian Kuhn

The operations find, min, max, predecessor, successor, insert, delete all have running time 𝑷 depth of tree . What is the depth of a binary search tree?

19

Running Times Binary Search Tree

Worst Case: 𝚰(𝒐)

n

n-1 n-2 1 2

Best Case: 𝚰 𝐦𝐩𝐡 𝒐

  • max. #nodes in

depth 𝑙 is 2𝑙

  • depth ≥ ⌊log2 𝑜⌋

Average Case: 𝚰 𝐦𝐩𝐡 𝒐

  • If the keys are inserted in random order, the

depth is 𝑃 log 𝑜

  • typical case?
slide-20
SLIDE 20

Algorithms and Data Structures Fabian Kuhn

  • 1. Insert all element into a binary search tree
  • 2. Read out the elements in sorted order

– Simplest solution: always find and delete minimum – Or better: find minimum and afterwards 𝑜 − 1 times successor

Better solution: reading out all elements:

  • Recursively:

1. Read out left subtree (recursively) 2. Read out root 3. Read out right subtree (recursively)

Running time for depth 𝑷 𝐦𝐩𝐡 𝒐 :

  • Insert: 𝑃 𝑜 ⋅ log 𝑜
  • Read out: 𝑃 𝑜

20

Sorting with a Binary Search Tree

slide-21
SLIDE 21

Algorithms and Data Structures Fabian Kuhn

Given: keys 𝑦min and 𝑦max (𝑦min ≤ 𝑦ma𝑦) Goal: Output all keys 𝒚 with 𝒚𝐧𝐣𝐨 ≤ 𝒚 ≤ 𝒚𝐧𝐛𝐲.

getrange(u, xmin, xmax): if u is not None: if u.key > xmin: getrange(u.left, xmin, xmax) if (xmin <= u.key) and (u.key <= xmax): add u to output if u.key < xmax:

getrange(u.right, xmin, xmax)

  • Assumption: #keys in range [𝑦min, 𝑦max] is equal to 𝑙
  • Running time: certainly 𝑃 𝑜 and certainly also Ω(𝑙 + depth)

21

Reading Out a Part of the Elements

𝑦min 𝑦max deal with subtree of u

slide-22
SLIDE 22

Algorithms and Data Structures Fabian Kuhn

Given: keys 𝑦min and 𝑦max (𝑦min ≤ 𝑦ma𝑦) Goal: Output all keys 𝒚 with 𝒚𝐧𝐣𝐨 ≤ 𝒚 ≤ 𝒚𝐧𝐛𝐲.

22

Reading Out a Part of the Elements

𝑦min 𝑦max root

Number of visited nodes:

  • #grün = 𝒍
  • #rot ≤ Tiefe
  • #blau ≤ Tiefe

Running time: 𝑷(𝒍 + depth)

slide-23
SLIDE 23

Algorithms and Data Structures Fabian Kuhn 23

Traversal of a Binary Search Tree

1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11 1 2 3 4 5 6 7 8 9 10 11

Goal: visit all nodes of a binary search tree once. In-Order: Pre-Order: Post-Order: Level-Order:

slide-24
SLIDE 24

Algorithms and Data Structures Fabian Kuhn 24

Traversal of a Binary Search Tree

Depth First Search / DFS Traversal Pre-Order: 15, 6, 3, 2, 4, 7, 13, 9, 18, 17, 20 In-Order: 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20 Post-Order: 2, 4, 3, 9, 13, 7, 6, 17, 20, 18, 15 Breadth First Search / BFS Traversal Level-Order: 15, 6, 18, 3, 7, 17, 20, 2, 4, 13, 9

  • Does not work in the same way

⟹ we will afterwards look at this recursively

slide-25
SLIDE 25

Algorithms and Data Structures Fabian Kuhn

preorder(node): if node is not None: visit(node) preorder(node.left) preorder(node.right) inorder(node): if node is not None: inorder(node.left) visit(node) inorder(node.right) postorder(node): if node is not None: postorder(node.left) postorder(node.right) visit(node)

25

DFS Traversal

slide-26
SLIDE 26

Algorithms and Data Structures Fabian Kuhn

  • Does not work recursively as for DFS traversal
  • Observations:

– The root of a subtree is always visited before its children – If a node 𝑣 is visited before node 𝑤, then also the children of node 𝑣 are visited before the children of node 𝑤. – Idea: Use a FIFO queue: when visiting 𝑣, then the children of 𝑣 are inserted into the FIFO queue.

26

BFS Traversal

slide-27
SLIDE 27

Algorithms and Data Structures Fabian Kuhn

  • Does not work recursively as for DFS traversal

27

BFS Traversal

FIFO Queue:

𝟐𝟔 𝟕 𝟐𝟗 𝟒 𝟖 𝟐𝟖 𝟑𝟏 𝟑 𝟓 𝟐𝟒 𝟘

slide-28
SLIDE 28

Algorithms and Data Structures Fabian Kuhn

  • Does not work recursively as for DFS traversal
  • Solution with a FIFO queue:

– When visiting a node, insert its children into a FIFO queue

BFS-Traversal: Q = new Queue() Q.enqueue(root) while not Q.empty(): node = Q.dequeue() visit(node) if node.left is not None: Q.enqueue(node.left) if node.right is not None: Q.enqueue(node.right)

28

BFS Traversal

slide-29
SLIDE 29

Algorithms and Data Structures Fabian Kuhn

DFS Traversal:

  • Each node is visited exactly once
  • Time cost per node: 𝑃(1)
  • Overall time for DFS traversal: 𝑷 𝒐

BFS Traversal:

  • Each node is visited exactly once

– Cost per node is linear in the number of children, i.e., 𝑃 1 for binary trees – Each node is inserted into the FIFO queue exactly once

  • Cost per node: 𝑃 1
  • Overall time for BFS traversal: 𝑷(𝒐)

29

Analysis Tree Traversal

slide-30
SLIDE 30

Algorithms and Data Structures Fabian Kuhn

In-order traversal:

  • Visits all elements of a binary search tree in sorted order
  • Sorting:

1. Insert all elements 2. In-order traversal

  • Observation: Order only depends on the set of elements (keys)

and not on the structure of the tree.

30

Application DFS Traversal I

slide-31
SLIDE 31

Algorithms and Data Structures Fabian Kuhn 31

Application DFS Traversal II

8 left subtree right subtree 5 4 2 1 3 7 6 10 9 13 11 12 14

Pre-order traversal:

  • From a pre-order traversal sequence, the tree can be

reconstructed uniquely (and efficiently).

  • Useful to store a tree, e.g., in a file

Example: 8, 5, 4, 2, 1, 3, 7, 6, 10, 9, 13, 11, 12, 14

slide-32
SLIDE 32

Algorithms and Data Structures Fabian Kuhn

Post-order traversal:

  • Deleting a whole binary search tree
  • First, one has to free the memory of the substrees before freeing

the memory of the root node. delete-tree(node) if (node != None) delete-tree(node.left) delete-tree(node.right) delete node

32

Application DFS Traversal III

slide-33
SLIDE 33

Algorithms and Data Structures Fabian Kuhn

Worst case running time of the operations find, min, max, predecessor, successor, insert, delete: 𝑷 depth of tree

  • In the best case, the depth is 𝐦𝐩𝐡𝟑 𝒐

– Definition depth: length of longest path from the root to a leaf

  • In the worst case, the depth is 𝒐 − 𝟐
  • In the average case, the depth is 𝑷 𝐦𝐩𝐡 𝒐

– Average case here means a random insertion order

Next lecture: How can we guarantee that the depth of a binary search tree is always 𝑃 log 𝑜 ?

33

Efficiency of a Binary Search Tree