Linked%20Lists - PowerPoint PPT Presentation

About This Presentation
Title:

Linked%20Lists

Description:

BNode root, cursor ; cursor = root ; while( cursor != null ) // search ... public void inOrder (BNode cursor) if (cursor == null) return; inOrder(cursor.left ) ... – PowerPoint PPT presentation

Number of Views:20
Avg rating:3.0/5.0
Slides: 19
Provided by: chris186
Category:
Tags: 20lists | cursor | linked

less

Transcript and Presenter's Notes

Title: Linked%20Lists


1
IAT 800
  • Linked Lists
  • Binary Trees

2
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 Smiths phone number

3
Arrays
  • Store data
  • arr3 33
  • Go through
  • for( i 0 i lt 10 i)
  • sum sum arri
  • Add
  • arrlast newNumber
  • last
  • Delete i
  • for( j i1 j lt last j )
  • arrj-1 arrj
  • last --
  • Search for 42
  • for( i 0 i lt last i )
  • if( arri 42 )
  • SUCCESS

4
Linked List
  • 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

5
Linked List
  • Node n, first
  • Store data
  • n.value 33
  • Go through
  • n first
  • while( n! NULL )
  • sum n.value
  • n n.next
  • Add
  • n new Node( 12 )
  • n.next first
  • first n
  • Delete n1
  • n1 n.next
  • n.next n1.next
  • Search 42
  • n first
  • while( n! NULL )
  • if( n.value 42 )
  • SUCCESS
  • n n.next

6
Runtime
  • Often, we care about the time that computation
    takes
  • Its 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

7
Whats Important
  • YOUR time is important
  • Runtime ! Creation Time
  • If any old algorithm will work,
  • AND youre only going to do it once or twice
  • Use it!
  • If youre going to run it millions of times
  • Think about the algorithm!

8
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

9
Binary Search Tree
10
Binary Search Tree
Root Pointer
3
LP
RP
  • Each Node has two links
  • left and right child
  • Top node is root
  • Node without children is leaf
  • Binary meaning two children
  • Search tree because of the node arrangement

5
LP
RP
11
Binary Search Tree
Root Pointer
3
LP
RP
1
LP
RP
5
LP
RP
2
LP
RP
0
LP
RP
4
LP
RP
6
LP
RP
12
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!!!

13
Binary Search Tree
14
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)

15
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 gt cursor.key )
  • cursor cursor.right
  • else
  • cursor cursor.left

16
Delete
17
Go Through
  • public void inOrder (BNode cursor)
  • if (cursor null)
  • return
  • inOrder(cursor.left )
  • System.out.println(cursor.key )
  • inOrder(cursor.right )

18
Things arent perfect
  • Unbalanced trees cause slower searches
  • Balancing algorithms manage that
  • Inserting items in order can cause this problem
Write a Comment
User Comments (0)
About PowerShow.com