Linked Structures Songs, Games, Movies Part III Fall 2013 Carola - - PowerPoint PPT Presentation

linked structures songs games movies part iii
SMART_READER_LITE
LIVE PREVIEW

Linked Structures Songs, Games, Movies Part III Fall 2013 Carola - - PowerPoint PPT Presentation

Linked Structures Songs, Games, Movies Part III Fall 2013 Carola Wenk Biological Structures Nature has evolved vascular and nervous systems in a hierarchical manner so that nutrients and signals can quickly reach their Human Nervous System


slide-1
SLIDE 1

Linked Structures Songs, Games, Movies Part III

Fall 2013 Carola Wenk

slide-2
SLIDE 2

Biological Structures

Nature has evolved vascular and nervous systems in a hierarchical manner so that nutrients and signals can quickly reach their destinations. Human Nervous System

slide-3
SLIDE 3

A large amount of information can be viewed more easily when it is laid out in a hiearchical fashion.

slide-4
SLIDE 4

A large amount of information can be viewed more easily when it is laid out in a hiearchical fashion.

slide-5
SLIDE 5

A large amount of information can be viewed more easily when it is laid out in a hiearchical fashion.

Us

slide-6
SLIDE 6

A large amount of information can be viewed more easily when it is laid out in a hiearchical fashion.

slide-7
SLIDE 7

Remember Binary Search?

median

In binary search, immediate access to the median allowed us to quickly direct our search to smaller or larger elements. Can we use this idea to develop a linked structure?

slide-8
SLIDE 8

Remember Binary Search?

median

data left right

  • We can split the list into two “halves” of a linked

structure.

slide-9
SLIDE 9

Remember Binary Search?

median

data left right

The two halves of a binary search tree can be defined recursively.

slide-10
SLIDE 10

Binary Search Trees

median

data left right

  • How do we define this type of structure in Python?
slide-11
SLIDE 11

Binary Search Trees

data smaller larger

  • A node in this linked structure is called a “leaf” if it has

no “children.” The topmost element is the “root”.

? ?

slide-12
SLIDE 12

Binary Search Tree Example

55

data left right

33

100

32 45 56

101

  • Where are the minimum and maximum of the element?
  • Can we quickly find any element?
  • What about adding/removing?
slide-13
SLIDE 13

Finding the Minimum/Maximum

  • The minimum element can be found by following the

left references, and the maximum element can be found by following right references.

55

left right

33

100

32 45 56

101

slide-14
SLIDE 14

Finding Any Item (Quickly)

55

data left right

33

100

32 45 56

101

  • In exactly the same way as binary search, we can

recursively focus on one “side” of the tree by checking the root element. How long does this take?

slide-15
SLIDE 15

Finding Any Item (Quickly)

55

data left right

33

100

32 45 56

101

  • In exactly the same way as binary search, we can

recursively focus on one “side” of the tree by checking the root element. How long does this take?

slide-16
SLIDE 16

Finding Any Item (Quickly)

55

data left right

33

100

32 45 56

101

  • In exactly the same way as binary search, we can

recursively focus on one “side” of the tree by checking the root element. How long does this take? The (worst-case) time to find an item depends on the height of the tree. How large can the height be?

slide-17
SLIDE 17

Worst Case Scenario

55 33

100

32 45 56

101

  • In this particular tree, we can find any element in two

steps.

32 33 45 56

100 101

Is this a tree? Why or why not?

slide-18
SLIDE 18

Worst Case Scenario

55 33

100

32 45 56

101

  • In this particular tree, we can find any element in two

steps.

32 33 45 56

100 101

This is a binary tree that’s not very tree-like. Why would a tree look like this?

slide-19
SLIDE 19

Adding Items

data left right

According to our definition, we know at least which side of the tree to insert. Algorithm: Recursively determine which side of the tree to insert, and create a new element at the bottom.

slide-20
SLIDE 20

Adding Items

data left right

Algorithm: Recursively determine which side of the tree to insert, and create a new element at the bottom. Unfortunately, we can’t control the order in which things are added.

slide-21
SLIDE 21

Worst Case Scenario

55 33

100

32 45 56

101

32 33 45 56

100 101

How were these two trees created?

slide-22
SLIDE 22

Worst Case Scenario

55 33

100

32 45 56

101

32 33 45 56

100 101

Insertion Order: 55, 33, 100, 32, 45, 56, 101 Insertion Order: 32, 33, 45, 56, 100, 101

Ironically, inserting items in sorted order produces an extremely “imbalanced” tree.

slide-23
SLIDE 23

Worst Case Scenario

55 33

100

32 45 56

101

32 33 45 56

100 101

The height of a binary search tree is the longest root-to- leaf path; researchers have studied how to minimize this.

slide-24
SLIDE 24

Removing Items

55 33

100

32 45 56

101

We may want to remove any item in the tree - what if we want to delete the “root”? We need to find a substitute; where is the next largest item in the tree located?

slide-25
SLIDE 25

Removing Items

55 33

100

32 45 56

101

We may want to remove any item in the tree - what if we want to delete the “root”? We need to find a substitute; where is the next largest item in the tree located?

slide-26
SLIDE 26

Removing Items

55 33

100

32 45 56

101

Which two elements can take the place of the item to be removed? Algorithm: Replace the item to be deleted with the smallest item larger than it. (The minimum element in the right subtree.)

slide-27
SLIDE 27

Removing Items

56 33

100

32 45

101

Which two elements can take the place of the item to be removed? Algorithm: Replace the item to be deleted with the smallest item larger than it. (The minimum element in the right subtree.)

slide-28
SLIDE 28

Binary Tree Conversion

  • Suppose I gave you a tree, how would you convert it

into a sorted array?

  • What if I wanted to save the tree to a file and

reconstruct it exactly?

55 33

100

32 45 56

101

slide-29
SLIDE 29

Binary Tree Conversion

  • Suppose I gave you a tree, how would you convert it

into a sorted array?

  • What if I wanted to save the tree to a file and

reconstruct it exactly?

55 33

100

32 45 56

101

slide-30
SLIDE 30

Binary Tree Conversion

  • An in-order traversal works by “visiting” the left

subtree, then the current item, and then visiting the right subtree.

55 33

100

32 45 56

101

slide-31
SLIDE 31

Binary Tree Conversion

55 33

100

32 45 56

101

An in-order traversal works by “visiting” the left subtree, then the current item, and then visiting the right subtree.

slide-32
SLIDE 32

Binary Tree Conversion

55 33

100

32 45 56

101

An in-order traversal works by “visiting” the left subtree, then the current item, and then visiting the right subtree. A pre-order traversal works by “visiting” the item we are at, then the left subtree, and then the right subtree.

slide-33
SLIDE 33

Summary of Binary Search Trees

The time to perform operations in binary search trees is highly dependent on how they are built. The best-case height of a binary tree is logarithmic in the number of elements; there are sophisticated techniques (AVL, red-black) for ensuring this height is logarithmic in the number of elements in the worst-case. Do tree data structures always have to be binary? Are they always used to add/remove/find elements in a collection?

55 33

100

32 45 56

101

between logarithmic and linear