Docstoc

IAT 800

Document Sample
IAT 800 Powered By Docstoc
					  IAT 800




               Linked Lists
               Binary Trees



Oct 28, 2009             IAT 800   1
Data Structures

 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”

Oct 28, 2009                  IAT 800                  2
Arrays                  0   1   2     3       4    5   6




   Store data                                   Delete i
     arr[3] = 33                                  for( j = i+1 ; j < last ; j++ )
   Go through:                                     arr[j-1] = arr[j] ;
     for( i = 0 ; i < 10 ; i++)                   last -- ;
       sum = sum + arr[i] ;                      Search for 42
   Add                                           for( i = 0 ; i < last ; i++ )
     arr[last] = newNumber ;                        if( arr[i] == 42 )
     last++ ;                                         SUCCESS ;


Oct 28, 2009                        IAT 800                                  3
               First Pointer
                               0   P     1   P           3   P
Linked List                                                      4   P

                                                 2   P


 A chain of nodes, where each node links to the
  next
 Node Has:
     – Value
     – Pointer to Next Node
   Inserting a new item is faster than array
     – move pointers around
 Deleting is also faster than array
 Searching takes time
     – Must go link by link from Node to Node

Oct 28, 2009                   IAT 800                               4
                    First Pointer
                                    0   P         1   P           3   P
Linked List                                                               4   P

                                                          2   P


Node n, first ;                                Delete n1
 Store data                                    n1 = n.next ;
     n.value = 33                               n.next = n1.next ;
   Go through:                                Search 42
     n = first ;                                n = first ;
     while( n!= NULL ) {                        while( n!= NULL ) {
        sum += n.value ;                           if( n.value == 42 )
        n = n.next;                                   SUCCESS ;
     }                                             n = n.next ;
   Add                                         }
     n = new Node( 12 );
     n.next = first ;
     first = n ;


Oct 28, 2009                        IAT 800                                   5
Runtime

 Often,  we care about the time that
  computation takes
 It’s ok if unimportant things run slow

 Important stuff needs to go fast
     – Stuff that gets run all the time
     – “The Inner Loop” is a slang term I use



Oct 28, 2009             IAT 800                6
What’s Important

 YOUR   time is important
 Runtime != Creation Time

 If any old algorithm will work,
     – AND you’re only going to do it once or twice
     – Use it!
 If     you’re going to run it millions of times
     – Think about the algorithm!

Oct 28, 2009              IAT 800                     7
Trees

A   Tree is a node-link structure
 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

Oct 28, 2009           IAT 800                        8
Binary Search Tree




Oct 28, 2009   IAT 800   9
                                       Root Pointer

Binary Search Tree                            LP 3 RP



 Each         Node has two links                     LP 5 RP

     – left and right child
     – Top node is root
     – Node without children is leaf
 Binary meaning two children
 Search tree because of the node
  arrangement

Oct 28, 2009              IAT 800                               10
Binary Search Tree
                             Root Pointer



                              LP 3 RP



               LP 1 RP                          LP 5 RP



                   LP 2 RP
 LP 0 RP                              LP 4 RP        LP 6 RP




Oct 28, 2009                    IAT 800                        11
Binary Search Tree

 For          Any node n
     – To the left
           • All child values are Less Than n
     – To the right
           • All child values are Greater Than n
 This         is a recursive definition!!!



Oct 28, 2009                   IAT 800             12
Binary Search Tree




Oct 28, 2009   IAT 800   13
Nodes

 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)




Oct 28, 2009            IAT 800                  14
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 ;
   else
         cursor = cursor.left ;
}
Oct 28, 2009                        IAT 800        15
Delete




Oct 28, 2009   IAT 800   16
Go Through

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

Oct 28, 2009           IAT 800       17
Things aren’t perfect


 Unbalanced     trees cause slower
    searches
     – Balancing algorithms manage that
 Inserting   items in order can
    cause this problem



Oct 28, 2009            IAT 800           18

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:5
posted:8/19/2012
language:English
pages:18