July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

19
July 2, 2015 IAT 265 1 IAT 265 Linked Lists, Binary Search Trees

Transcript of July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

Page 1: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 1July 2, 2015

IAT 265

Linked Lists, Binary Search Trees

Page 2: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 2July 2, 2015

Data Structures

g With a collection of data, we often want to do many things– Store and organize data– Go through the list of data and do X per

item– Add new data– Delete unwanted data– Search for data of some value

• “Give me Smith’s phone number”

Page 3: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 3July 2, 2015

Arrays

g Store dataarr[3] = 33

g Iterate (go thru):for( i = 0 ; i < 10 ; i++)

sum = sum + arr[i] ;g Add

arr[last] = newNumber ;last++ ;

g Delete ifor( j = i+1 ; j < last ;

j++ ) arr[j-1] = arr[j] ;

last -- ;g Search for 42

for( i = 0 ; i < last ; i++ ) if( arr[i] == 42 )

SUCCESS ;

0 1 2 3 4 5 6

Page 4: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 4July 2, 2015

Linked List

g A chain of nodes, where each node links to the next

g Node Has:– Value– Pointer to Next Node

g Inserting a new item is faster than array– move pointers around

g Deleting is also faster than arrayg Searching takes time

– Must go link by link from Node to Node

0 P 1 P

2 P

3 P4 P

First Pointer

Page 5: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 5July 2, 2015

Linked List

class Node {int value ;Node next ;

}Node n, first ;

g Store datan.value = 33

g Iterate:n = first ;while( n!= NULL ) {

sum += n.value ; n = n.next;

}

g Addn = new Node( 12 );n.next = first ;first = n ;

g Delete n1n1 = n.next ;n.next = n1.next ;

g Search 42n = first ;while( n!= NULL ) {

if( n.value == 42 )SUCCESS ;

n = n.next ;}

0 P 1 P

2 P

3 P4 P

First Pointer

Page 6: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 6July 2, 2015

Runtime

g Often, we care about the time that computation takes

g It’s ok if unimportant things run slowg Important stuff needs to go fast

– Stuff that gets run all the time– “The Inner Loop” is a slang term I use

Page 7: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 7July 2, 2015

What’s Important

g YOUR time is important g Runtime != Creation Timeg If any old algorithm will work,

– AND you’re only going to do it once or twice

– Use it!g If you’re going to run it millions of

times– Think about the algorithm!

Page 8: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 8July 2, 2015

Trees

g A Tree is a node-link structureg It is built to enable fast searching

– What Write Runtime– Store Like linked list As fast– Go Thru More complex As fast– Add More complex A little slower– Delete More complex A little

slower– Search A little complex

Much faster

Page 9: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 9July 2, 2015

Binary Search Tree

Page 10: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 10July 2, 2015

Binary Search Tree

g Each Node has two links– left and right child– Top node is root– Node without children is leaf

g Binary meaning two childreng Search tree because of the node

arrangement

3LP

Root Pointer

RP

5LP RP

Page 11: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 11July 2, 2015

Binary Search Tree

3LP

Root Pointer

RP

1LP RP

0LP RP2LP RP

5LP RP

4LP RP 6LP RP

Page 12: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 12July 2, 2015

Binary Search Tree

g For Any node n– To the left

• All child values are Less Than n– To the right

• All child values are Greater Than n

g This is a recursive definition!!!

Page 13: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 13July 2, 2015

Binary Search Tree

Page 14: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 14July 2, 2015

Nodes

g Typically, each node has 3 or 4 parts:– The key (names in pictures)– The payload that goes with the key

(not shown)– The left child pointer (possibly null)– The right child pointer (possibly null)

Page 15: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 15July 2, 2015

Binary Search Tree Search

class BNode {int key ;BNode left, right ;

}BNode root, cursor ;

cursor = root ;while( cursor != null ) // search for SEARCH{

if( SEARCH == cursor.key )SUCCESS ;

if( SEARCH > cursor.key )cursor = cursor.right ;

elsecursor = cursor.left ;

}

Page 16: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 16July 2, 2015

Delete

Page 17: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 17July 2, 2015

Iterate (In Order)

public void inOrder (BNode cursor){ if (cursor == null) return; inOrder(cursor.left ); System.out.println(cursor.key ); inOrder(cursor.right );}

Page 18: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 18July 2, 2015

Things aren’t perfect

g Unbalanced trees cause slower searches– Balancing algorithms manage

thatg Inserting items in order can

cause this problem

Page 19: July 2, 2015IAT 2651 Linked Lists, Binary Search Trees.

IAT 265 19July 2, 2015

Trees in Javag Use the TreeSet class to get this

functionality– You don’t have to build the structure yourself– Stores Objects

TreeSet t1 = new TreeSet();t1.add( “aaa” );

Iterator it1 = t1.iterator();while( it1.hasNext() ) {

Object o1 =it1.next(); System.out.println( o1 );}