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

Post on 14-Jan-2016

29 views 0 download

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

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

308-203AIntroduction 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

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

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

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

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

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]);}

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 )

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

Tree nodes

Node

Parent

Left child Right child

Trees - example

Root

A B

C D

E F

null null

null null

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

Trees - example

Root

A B

C D

E F

Depth = 4

Leaf

LeafLeaf

Balanced Trees - example

Root

A B

C D

E F

UNBALANCEDBALANCED

Root

A B

C DE F

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

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

Tree Traversal

traversal - Visiting all nodes in a tree

This can be done in three different ways:

• preorder traversal• inorder traversal• postorder traversal

Preorder Tree Traversal

Preorder( node N ){

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

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

}

Preorder Tree Traversal

15

4

1 12

20

17 41

33

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

Postorder Tree Traversal

Postorder( node N ){

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

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

}

Preorder Tree Traversal

15

4

1 12

20

17 41

33

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

Inorder Tree Traversal

Inorder( node N ){

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

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

// Visit the rightchildreninorder(N.rightChild);

}

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

Order of Growth

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

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);}

Order of Growth?

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

What is that in terms of n???

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

Insertion of a new node

A simple algorithm:

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

II) Insert it there

Insertion - example

15

4

1 12

20

17 41

33

Insert 13:

13

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.

Any questions?