+ All Categories
Home > Documents > 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Date post: 14-Jan-2016
Category:
Upload: aleron
View: 30 times
Download: 0 times
Share this document with a friend
Description:
308-203A Introduction to Computing II Lecture 9: Binary Search/Trees. Fall Session 2000. Binary Search. Goal : Find a particular element in a sorted array Solution : Break the data to be searched in two by looking at the middle element - PowerPoint PPT Presentation
30
308-203A Introduction to Computing II Lecture 9: Binary Search/Trees Fall Session 2000
Transcript
Page 1: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

308-203AIntroduction to Computing II

Lecture 9: Binary Search/Trees

Fall Session 2000

Page 2: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Binary Search

• Goal: Find a particular element in a sorted array

• Solution: Break the data to be searched in two by looking at the middle element

• Example: Looking up a word in the dictionary one usually starts by opening the dictionary near the middle

Page 3: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Binary Search: exampleLookup “20” in a sorted array of 16 elements:

(1, 4, 12, 15, 17, 20, 33, 41, 49, 57, 72, 88, 92, 94, 102, 145)

Middle (8th) element = 41 > 20

Page 4: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Binary Search: exampleLookup “20” in a sorted array of 16 elements:

(1, 4, 12, 15, 17, 20, 33, 41, 49, 57, 72, 88, 92, 94, 102, 145)

Middle (4th) element = 15 < 20

Remaining 7 elements

Page 5: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Binary Search: exampleLookup “20” in a sorted array of 16 elements:

(1, 4, 12, 15, 17, 20, 33, 41, 49, 57, 72, 88, 92, 94, 102, 145)

Middle (6th) element = 20

Remaining 3 elements

done

Page 6: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Pseudocode (recursive)

Find( x, A[min..max] ){ middle = A[min + (max - min)/2 ] ;

if (middle = x) return A[ middle ] ;

else if (middle < x) return Find(x, A[min, middle-1]

else return Find(x, A[middle+1, max]);}

Page 7: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Recurrence Equation

Worst case: we don’t find x until only one elementis left to check

T(1) = 1T(n) = 1 + T(n/2)

The recurrence:

The solution: T(n) = 1 + T(n/2) = 1 + (1 + T(n/4) ) = 1 + … + T(n/2i) + … T(1) = O( log n )

Page 8: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Trees

Definition: A tree is a collection of nodes whereeach node may have:

a) one parent-nodeb) one left child-nodec) one right child-node

There is exactly one node in the tree which has noparent, and it is called the root. All other nodes haveexactly one parent

Page 9: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Tree nodes

Node

Parent

Left child Right child

Page 10: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Trees - example

Root

A B

C D

E F

null null

null null

Page 11: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Definitions

Definition: A leaf of a tree is an node with no children

Definition: The depth of the tree is the length of the longest path from root to leaf

Definition: A balanced tree is a tree where the depth is O( log n ), n being the number of nodes in the tree

Page 12: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Trees - example

Root

A B

C D

E F

Depth = 4

Leaf

LeafLeaf

Page 13: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Balanced Trees - example

Root

A B

C D

E F

UNBALANCEDBALANCED

Root

A B

C DE F

Page 14: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Binary Search Trees

We can store data in a tree in an ordercorresponding to a binary search

15

4

1 12

20

17 41

33

Page 15: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Binary Search Trees

Definition: A binary search tree is a tree in whichwhere nodes have the binary search property:

I. For any node Y reached through the leftchild of a node X, Y < X

II. For any node Y reached through the rightchild of a node X, X < Y

Page 16: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Tree Traversal

traversal - Visiting all nodes in a tree

This can be done in three different ways:

• preorder traversal• inorder traversal• postorder traversal

Page 17: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Preorder Tree Traversal

Preorder( node N ){

// Do something with N firstprint(N.key);

// Visit the childrenpreorder(N.leftChild);preorder(N.rightChild);

}

Page 18: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Preorder Tree Traversal

15

4

1 12

20

17 41

33

Result: (15, 4, 1, 12, 20, 17, 41, 33)

Page 19: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Postorder Tree Traversal

Postorder( node N ){

// Visit the childrenpostorder(N.leftChild);postorder(N.rightChild);

// Do something with N afterwardsprint(N.key);

}

Page 20: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Preorder Tree Traversal

15

4

1 12

20

17 41

33

Result: (1, 12, 4, 17, 33, 41, 20, 15)

Page 21: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Inorder Tree Traversal

Inorder( node N ){

// Visit the left childreninorder(N.leftChild);

// Do something with Nprint(N.key);

// Visit the rightchildreninorder(N.rightChild);

}

Page 22: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Inorder Tree Traversal

15

4

1 12

20

17 41

33

Result: (1, 4, 12, 15, 17, 20, 33, 41)

note this is the sorted order

Page 23: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Order of Growth

O( n ) because we visit each nodeexactly once and at each node do( 1 ) computations

Page 24: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Search

To search, start from the root and followthe appropriate child links.

Search( Key x, Node N ){

if (N.key = x) done;

if (x < N.key) Search(x, N.leftChild);

else Search(x, N.leftChild);}

Page 25: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Order of Growth?

O( d ) where d is the depth of the tree

What is that in terms of n???

Page 26: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Order of Growth?

O( d ) where d is the depth of the tree

What is that in terms of n???

log n < d < n depending on whether thetree is balanced

Page 27: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Insertion of a new node

A simple algorithm:

I) Do “Search” until you find where the node belongs

II) Insert it there

Page 28: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Insertion - example

15

4

1 12

20

17 41

33

Insert 13:

13

Page 29: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Insertion

- Like search takes O( d ) time

- Does not guarantee the tree will be balanced

- For this reason there are specialized “balanced tree” algorithms which preserve balance, e.g. AVL trees, Red-Black trees, etc.

Page 30: 308-203A Introduction to Computing II Lecture 9: Binary Search/Trees

Any questions?


Recommended