This preview shows pages 1–7. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Basic algorithms with linked lists and binary trees 1 Linked List Traversal A generic algorithm for visiting all list nodes Can be specialized for many goals Find minimum / maximum Search for given value Count number of occurrences of given value Building block for more complex algorithms Sorting 2 Linked List Traversal 3 val: 13.40 next: val: 12.50 next: val:7.45 Next: val:2.50 next: NULL Input : L // linked list; L is link to first node (red arrow) // a node has two fields // a value, called val , and // a link to the next node, called next Output : // all node values, in the order in which they appear in the list TraverseList (L) curr = L // current node link is the link to the first node while curr != NULL // while end of list has not been reached print curr>val // print the val field of the node to which curr link points curr = curr>next // move to next node endwhile endTraverseList Trace 4 val: 13.40 next: val: 12.50 next: val:7.45 Next: val:2.50 next: NULL Input : L // linked list; L is link to first node (red arrow) // a node has two fields // a value, called val , and // a link to the next node, called next Output : // all node values, in the order in which they appear in the list TraverseList (L) curr = L // current node link is the link to the first node while curr != NULL // while end of list has not been reached print curr>val // print the val field of the node curr = curr>next // move to next node endwhile endTraverseList A 1 L A 2 A 3 curr curr>val curr>next Print Out L 13.40 A 1 13.40 A1 12.50 A2 12.50 A2 7.45 A3 7.45 A3 2.50 NULL 2.50 NULL Input : L // linked list with ( val, next ) nodes V // value to be inserted Output : // linked list with new node storing V, sorted InsertSortedList (L, V) N = link to new node // create new node N>val = V // val of new node is input value V; dont know next yet curr = L // current node link is the link to the first node prev = NULL // link to previous node, initially null while curr != NULL // while end of list has not been reached if curr>val < V then // if the current value is smaller than V break // stop loop; insert btw prev and curr endif prev = curr // move to next node, update prev & curr curr = curr>next endwhile N>next = curr if prev != NULL then prev>next = N else L = N endif endInsertSortedList Insert node in sorted list 5 val: 13.40 next: val: 12.50 next: val:7.45 Next: NULL A 1 L A 2 Input : L // linked list with ( val, next ) nodes V // value to be inserted Output : // linked list with new node storing V, sorted InsertSortedList (L, V) N = link to new node // create new node N>val = V // val of new node is input value V; dont know next yet curr = L // current node link is the link to the first node prev = NULL // link to previous node, initially null while curr != NULL // while end of list has not been reached if curr>val < V then // if the current value is smaller than V break // stop loop; insert btw prev and curr endif prev = curr // move to next node, update prev & curr...
View Full
Document
 Spring '08
 Staff
 Algorithms

Click to edit the document details