1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence...

66
1 Linked Lists Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of contiguity of memory.

Transcript of 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence...

Page 1: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

1

Linked ListsLinked Lists

A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of contiguity of memory.

Page 2: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

2

Singly-linked ListsSingly-linked Lists

A list in which there is a preferred direction. A minimally linked list. The item before has a pointer to the item

after.

Page 3: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

3

Singly-linked ListSingly-linked List

Implement this structure using objects and references.

Page 4: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

4

Singly-linked ListSingly-linked List

11

7

4

1

head

Page 5: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

5

Singly-linked ListSingly-linked List

11

7

4

1

head

Page 6: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

6

Singly-linked ListSingly-linked List

class ListElement

{

Object datum ;

ListElement nextElement ;

. . .

}

datum nextElement

Page 7: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

7

Singly-linked ListSingly-linked List

ListElement newItem =

new ListElement(new Integer(4)) ;

ListElement p = null ;

ListElement c = head ;

while ((c != null) && !c.datum.lessThan(newItem))

{

p = c ;

c = c.nextElement ;

}

newItem.nextElement = c ;

p.nextElement = newItem ;

Page 8: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

8

Singly-linked ListSingly-linked List

11

7

4

1

head

pc

newElement

Page 9: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

9

Analysing Singly-linked ListAnalysing Singly-linked List

Accessing a given location is O(n). Setting a given location is O(n). Inserting a new item is O(n). Deleting an item is O(n) Assuming both a head at a tail pointer,

accessing, inserting or deleting can be O(1).

Page 10: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

10

Doubly-linked ListsDoubly-linked Lists

A list without a preferred direction. The links are bidirectional: implement this

with a link in both directions.

Page 11: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

11

Doubly-linked ListDoubly-linked List

tail

head

Page 12: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

12

Doubly-linked ListDoubly-linked List

tail

head

Page 13: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

13

Doubly-linked ListDoubly-linked List

class ListElement

{

Object datum ;

ListElement nextElement ;

ListElement previousElement ;

. . .

}

datum nextElement

previousElement

Page 14: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

14

Doubly-linked ListDoubly-linked List

ListElement newItem =

new ListElement(new Integer(4)) ;

ListElement c = head ;

while ((c.next != null) &&

!c.next.datum.lessThan(newItem))

{

c = c.nextElement ;

}

newItem.nextElement = c.nextElement ;

newItem.previousElement = c ;

c.nextElement.previousElement = newItem ;

c.nextElement = newItem ;

Spot the deliberate mistake. What needs to be done to correct this?

Page 15: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

15

Doubly-linked ListDoubly-linked List

tail

head

c

newItem

Page 16: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

16

Doubly-linked ListDoubly-linked List

Performance of doubly-linked list is formally similar to singly linked list.

The complexity of managing two pointers makes things very much easier since we only ever need a single pointer into the list.

Iterators and editing are made easy.

Page 17: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

17

Doubly-linked ListDoubly-linked List

Usually find the List type in a package is a doubly-linked list.

Singly-linked list are used in other data structures.

Page 18: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

18

Stack and QueueStack and Queue

Familiar with the abstractions of stack and queue.

Page 19: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

19

StackStack

push pop

isEmpty

top

Page 20: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

20

QueueQueue

insert remove

isEmpty

Page 21: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

21

Implementing StackImplementing Stack

tos

Page 22: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

22

Implementing QueueImplementing Queue

tail

head

Page 23: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

23

Multi-listsMulti-lists

Multi-lists are essentially the technique of embedding multiple lists into a single data structure.

A multi-list has more than one next pointer, like a doubly linked list, but the pointers create separate lists.

Page 24: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

24

Multi-listsMulti-lists

head

Page 25: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

25

Multi-listsMulti-lists

head

Page 26: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

26

Multi-lists (Not Required)Multi-lists (Not Required)

headhead

Page 27: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

27

Linked StructuresLinked Structures

A doubly-linked list or multi-list is a data structure with multiple pointers in each node.

In a doubly-linked list the two pointers create bi-directional links

In a multi-list the pointers used to make multiple link routes through the data.

Page 28: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

28

Linked StructuresLinked Structures

What else can we do with multiple links? Make them point at different data: create

Trees (and Graphs).

Page 29: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

29

TreesTrees

node

leaf node

degree

root children

parentLevel 1

Level 2

Level 3

height = depth = 3

Page 30: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

30

TreesTrees

Crucial properties of Trees: Links only go down from parent to child. Each node has one and only one parent (except

root which has no parent). There are no links up the data structure; no

child to parent links. There are no sibling links; no links between

nodes at the same level.

Page 31: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

31

TreesTrees

If we relax the restrictions, it is not a Tree, it is a Graph.

A Tree is a directed, acyclic Graph that is single parent.

Page 32: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

32

TreesTrees

Binary Trees have degree 2. Red–Black Trees and AVL Trees are Binary

Trees with special extra properties; they are balanced.

B-Trees, B+-Trees, B*-Trees are more complicated Trees with flexible branching factor: these are used very extensively in databases.

Page 33: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

33

Binary TreesBinary Trees

Trees are immensely useful for sorting and searching.

Look at Binary Trees as they are the simplest.

Page 34: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

34

Binary TreesBinary Trees

This is a complete binary tree.

Page 35: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

35

Binary TreesBinary Trees

How to insert something in the list? Need a metric, there must be an order

relation defined on the nodes. The elements are in the tree in a given

order; assume ascending order.

Page 36: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

36

Binary TreesBinary Trees

Inserting an element in the Binary Tree involves:

If the tree is empty, insert the element as the root.

Page 37: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

37

Binary TreesBinary Trees

If the tree is not empty: Start at the root. For each node decide whether the element is the

same as the one at the node or comes before or after it in the defined order.

When the child is a null pointer insert the element.

Page 38: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

38

Binary TreeBinary Tree

root

37

37

Page 39: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

39

Binary TreeBinary Tree

root

3

9

37

37, 9, 3

Page 40: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

40

Binary TreesBinary Trees

root

3 14

9

37

68

37, 9, 3, 68, 14, 54

54

Page 41: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

41

Binary TreesBinary Trees

Delete this one

Page 42: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

42

Binary TreesBinary Trees

Page 43: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

43

Binary TreesBinary Trees

Delete this one

Page 44: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

44

Binary TreesBinary Trees

Assume ascendingorder.

Page 45: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

45

Binary TreesBinary Trees

Delete this one

Page 46: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

46

Binary TreesBinary Trees

Assume ascendingorder.

Page 47: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

47

Binary TreeBinary Tree

In Java:

class Unit

{

public Unit(Object o, Unit l, Unit r)

{ datum = o ; left = l ; right = r ; }

Object datum ;

Unit left ;

Unit right ;

}

Page 48: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

48

Binary TreeBinary Tree

Copying can be done recursively:

public Object clone()

{

return new Unit(datum,

(left != null) ?

((Unit)left).clone() : null,

(right != null) ?

((Unit)right).clone() : null

) ;

}

Page 49: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

49

Binary TreeBinary Tree

Can take a tour around the tree, doing something at each stage:

void inOrder (Function f)

{

if (left != null) { left.inOrder(f) ; }

f.execute(this) ;

if (right != null) { right.inOrder(f); }

}

Page 50: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

50

Binary TreeBinary Tree

Can take a different tour around the tree, doing something at each stage:

void preOrder (Function f)

{

f.execute(this) ;

if (left != null) { left.preOrder(f) ; }

if (right != null) { right.preOrder(f); }

}

Page 51: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

51

Binary TreeBinary Tree

Can take yet another tour around the tree, doing something at each stage:

void postOrder (Function f)

{

if (left != 0) { left.postOrder(f) ; }

if (right != 0) { right.postOrder(f); }

f.execute(this) ;

}

Page 52: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

52

Traversing a Binary TreeTraversing a Binary Tree

Four sorts of route through a tree: In-order. Pre-order. Post-order. Level-order.

Page 53: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

53

Traversing a Binary TreeTraversing a Binary Tree

Pre-order, post-order and in-order are related since they just rearrange order of behaviour. Depth-first searches.

Level-order is different. Breadth-first search.

Page 54: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

54

Traversing a Binary TreeTraversing a Binary Tree

root

3 14

9

37

68

54

inorder: 3, 9, 14, 37, 54, 68preorder: 37, 9, 3, 14, 68, 54postorder: 3, 14, 9, 54, 68, 37levelorder: 37, 9, 68, 3, 14, 54

This is a complete binary tree.

Page 55: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

55

Searching and SortingSearching and Sorting

A Tree is an inherently sorted data structure.

A Tree can be an index to data rather than holding data.

Searching using a Tree is much better than linear search, in fact it is a sort of binary chop search.

Page 56: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

56

Binary TreesBinary Trees

Balance is important when working with Binary Trees: Height is O(log2 n) in the best case but O(n) in the

worst case (tree becomes a linear list). Worst case occurs when data is fed in in order. Lookup time, insertion time and removal time are

all O(log2 n) when the tree is balanced and O(n) in the worst case (directly proportional to approximate height).

Page 57: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

57

Problem with Binary TreeProblem with Binary Tree

If data is entered in sorted order, the tree becomes a list.

This degeneration loses the O(log2 n) behaviour.

How can we get around this?

Page 58: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

58

Problem with Binary TreeProblem with Binary Tree

Make the tree self-balancing.

Page 59: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

59

AVL TreeAVL Tree

A binary tree that is self-modifying. Is nearly balanced at all times. No sub-tree is more than one level deeper

than its sibling. Adelson-Velskii and Landis were the

progenitors.

Page 60: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

60

AVL TreeAVL Tree

AVL trees insert data by inserting as any normal binary tree.

The tree may become unbalanced. Thus, there is then a second stage, the tree

re-balances itself if it needs to.

Page 61: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

61

AVL TreeAVL Tree

When removal occurs, the tree may become unbalanced.

There is, therefore, a second stage, the tree re-balances itself if it needs to.

Page 62: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

62

AVL TreeAVL Tree

AVL trees are now considered inefficient and are therefore rarely used.

Trees are, however, so important that efficiency is necessary.

Page 63: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

63

Red-Black TreeRed-Black Tree

These trees have a different algorithm for handling the modifications.

Instead of measuring the unbalancedness of the tree, each node is coloured.

Page 64: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

64

Red-Black TreeRed-Black Tree

Insertion does not require two phases since the tree can be re-balanced as the position of the insertion point is found.

This makes it far more efficient than the AVL tree.

Page 65: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

65

B-TreeB-Tree

Used in database systems. Not used in memory bound systems.

Page 66: 1 Linked Lists A linked list is a sequence in which there is a defined order as with any sequence but unlike array and Vector there is no property of.

66

End of this SessionEnd of this Session