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.

Slides:



Advertisements
Similar presentations
EcoTherm Plus WGB-K 20 E 4,5 – 20 kW.
Advertisements

1 A B C
Chapter 25 Lists, Stacks, Queues, and Priority Queues
Simplifications of Context-Free Grammars
Variations of the Turing Machine
PDAs Accept Context-Free Languages
TK1924 Program Design & Problem Solving Session 2011/2012
AP STUDY SESSION 2.
1
Copyright © 2003 Pearson Education, Inc. Slide 1.
STATISTICS INTERVAL ESTIMATION Professor Ke-Sheng Cheng Department of Bioenvironmental Systems Engineering National Taiwan University.
David Burdett May 11, 2004 Package Binding for WS CDL.
Create an Application Title 1Y - Youth Chapter 5.
Add Governors Discretionary (1G) Grants Chapter 6.
CALENDAR.
CHAPTER 18 The Ankle and Lower Leg
The 5S numbers game..
Binary Tree Structure a b fe c a rightleft g g NIL c ef b left right pp p pp left key.
Media-Monitoring Final Report April - May 2010 News.
Break Time Remaining 10:00.
Factoring Quadratics — ax² + bx + c Topic
EE, NCKU Tien-Hao Chang (Darby Chang)
Turing Machines.
Table 12.1: Cash Flows to a Cash and Carry Trading Strategy.
PP Test Review Sections 6-1 to 6-6
Chapter 10: Applications of Arrays and the class vector
Chapter 24 Lists, Stacks, and Queues
1 IMDS Tutorial Integrated Microarray Database System.
Multicore Programming Skip list Tutorial 10 CS Spring 2010.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Data structure is concerned with the various ways that data files can be organized and assembled. The structures of data files will strongly influence.
Briana B. Morrison Adapted from William Collins
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
Copyright © 2012, Elsevier Inc. All rights Reserved. 1 Chapter 7 Modeling Structure with Blocks.
Biology 2 Plant Kingdom Identification Test Review.
Adding Up In Chunks.
FAFSA on the Web Preview Presentation December 2013.
MaK_Full ahead loaded 1 Alarm Page Directory (F11)
1 Termination and shape-shifting heaps Byron Cook Microsoft Research, Cambridge Joint work with Josh Berdine, Dino Distefano, and.
Artificial Intelligence
When you see… Find the zeros You think….
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 11: Priority Queues and Heaps Java Software Structures: Designing.
Before Between After.
Slide R - 1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Prentice Hall Active Learning Lecture Slides For use with Classroom Response.
12 October, 2014 St Joseph's College ADVANCED HIGHER REVISION 1 ADVANCED HIGHER MATHS REVISION AND FORMULAE UNIT 2.
Subtraction: Adding UP
: 3 00.
5 minutes.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
1 hi at no doifpi me be go we of at be do go hi if me no of pi we Inorder Traversal Inorder traversal. n Visit the left subtree. n Visit the node. n Visit.
Converting a Fraction to %
Numerical Analysis 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CSE Lecture 17 – Balanced trees
Clock will move after 1 minute
famous photographer Ara Guler famous photographer ARA GULER.
Foundations of Data Structures Practical Session #7 AVL Trees 2.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13 Pointers and Linked Lists.
Physics for Scientists & Engineers, 3rd Edition
Select a time to count down from the clock above
Copyright Tim Morris/St Stephen's School
1.step PMIT start + initial project data input Concept Concept.
9. Two Functions of Two Random Variables
1 Dr. Scott Schaefer Least Squares Curves, Rational Representations, Splines and Continuity.
1 Decidability continued…. 2 Theorem: For a recursively enumerable language it is undecidable to determine whether is finite Proof: We will reduce the.
1 Non Deterministic Automata. 2 Alphabet = Nondeterministic Finite Accepter (NFA)
Bioinformatics Programming 1 EE, NCKU Tien-Hao Chang (Darby Chang)
CSCE 2100: Computing Foundations 1 The Tree Data Model
Presentation transcript:

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 contiguity of memory.

2 Singly-linked Lists u A list in which there is a preferred direction. u A minimally linked list. u The item before has a pointer to the item after.

3 Singly-linked List u Implement this structure using objects and references.

4 Singly-linked List head

5 Singly-linked List head

6 Singly-linked List class ListElement { Object datum ; ListElement nextElement ;... } datumnextElement

7 Singly-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 ;

8 Singly-linked List head p c newElement

9 Analysing Singly-linked List u Accessing a given location is O(n). u Setting a given location is O(n). u Inserting a new item is O(n). u Deleting an item is O(n) u Assuming both a head at a tail pointer, accessing, inserting or deleting can be O(1).

10 Doubly-linked Lists u A list without a preferred direction. u The links are bidirectional: implement this with a link in both directions.

11 Doubly-linked List tail head

12 Doubly-linked List tail head

13 Doubly-linked List class ListElement { Object datum ; ListElement nextElement ; ListElement previousElement ;... } datumnextElement previousElement

14 Doubly-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?

15 Doubly-linked List tail head c newItem

16 Doubly-linked List u Performance of doubly-linked list is formally similar to singly linked list. u The complexity of managing two pointers makes things very much easier since we only ever need a single pointer into the list. u Iterators and editing are made easy.

17 Doubly-linked List u Usually find the List type in a package is a doubly-linked list. u Singly-linked list are used in other data structures.

18 Stack and Queue u Familiar with the abstractions of stack and queue.

19 Stack pushpop isEmpty top

20 Queue insert remove isEmpty

21 Implementing Stack tos

22 Implementing Queue tail head

23 Multi-lists u Multi-lists are essentially the technique of embedding multiple lists into a single data structure. u A multi-list has more than one next pointer, like a doubly linked list, but the pointers create separate lists.

24 Multi-lists head

25 Multi-lists head

26 Multi-lists (Not Required) head

27 Linked Structures u A doubly-linked list or multi-list is a data structure with multiple pointers in each node. u In a doubly-linked list the two pointers create bi-directional links u In a multi-list the pointers used to make multiple link routes through the data.

28 Linked Structures u What else can we do with multiple links? u Make them point at different data: create Trees (and Graphs).

29 Trees node leaf node degree root children parent Level 1 Level 2 Level 3 height = depth = 3

30 Trees u Crucial properties of Trees: v Links only go down from parent to child. v Each node has one and only one parent (except root which has no parent). v There are no links up the data structure; no child to parent links. v There are no sibling links; no links between nodes at the same level.

31 Trees u If we relax the restrictions, it is not a Tree, it is a Graph. u A Tree is a directed, acyclic Graph that is single parent.

32 Trees u Binary Trees have degree 2. u Red–Black Trees and AVL Trees are Binary Trees with special extra properties; they are balanced. u B-Trees, B+-Trees, B*-Trees are more complicated Trees with flexible branching factor: these are used very extensively in databases.

33 Binary Trees u Trees are immensely useful for sorting and searching. u Look at Binary Trees as they are the simplest.

34 Binary Trees This is a complete binary tree.

35 Binary Trees u How to insert something in the list? u Need a metric, there must be an order relation defined on the nodes. u The elements are in the tree in a given order; assume ascending order.

36 Binary Trees u Inserting an element in the Binary Tree involves: u If the tree is empty, insert the element as the root.

37 Binary Trees u If the tree is not empty: v Start at the root. v 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. v When the child is a null pointer insert the element.

38 Binary Tree root 37

39 Binary Tree root , 9, 3

40 Binary Trees root , 9, 3, 68, 14, 54 54

41 Binary Trees Delete this one

42 Binary Trees

43 Binary Trees Delete this one

44 Binary Trees Assume ascending order.

45 Binary Trees Delete this one

46 Binary Trees Assume ascending order.

47 Binary Tree u In Java: class Unit { public Unit(Object o, Unit l, Unit r) { datum = o ; left = l ; right = r ; } Object datum ; Unit left ; Unit right ; }

48 Binary Tree u Copying can be done recursively: public Object clone() { return new Unit(datum, (left != null) ? ((Unit)left).clone() : null, (right != null) ? ((Unit)right).clone() : null ) ; }

49 Binary Tree u 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); } }

50 Binary Tree u 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); } }

51 Binary Tree u 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) ; }

52 Traversing a Binary Tree u Four sorts of route through a tree: v In-order. v Pre-order. v Post-order. v Level-order.

53 Traversing a Binary Tree u Pre-order, post-order and in-order are related since they just rearrange order of behaviour. Depth-first searches. u Level-order is different. Breadth-first search.

54 Traversing a Binary Tree root inorder: 3, 9, 14, 37, 54, 68 preorder: 37, 9, 3, 14, 68, 54 postorder: 3, 14, 9, 54, 68, 37 levelorder: 37, 9, 68, 3, 14, 54 This is a complete binary tree.

55 Searching and Sorting u A Tree is an inherently sorted data structure. u A Tree can be an index to data rather than holding data. u Searching using a Tree is much better than linear search, in fact it is a sort of binary chop search.

56 Binary Trees u Balance is important when working with Binary Trees: v Height is O(log 2 n) in the best case but O(n) in the worst case (tree becomes a linear list). v Worst case occurs when data is fed in in order. v Lookup time, insertion time and removal time are all O(log 2 n) when the tree is balanced and O(n) in the worst case (directly proportional to approximate height).

57 Problem with Binary Tree u If data is entered in sorted order, the tree becomes a list. u This degeneration loses the O(log 2 n) behaviour. u How can we get around this?

58 Problem with Binary Tree u Make the tree self-balancing.

59 AVL Tree u A binary tree that is self-modifying. u Is nearly balanced at all times. u No sub-tree is more than one level deeper than its sibling. u Adelson-Velskii and Landis were the progenitors.

60 AVL Tree u AVL trees insert data by inserting as any normal binary tree. u The tree may become unbalanced. u Thus, there is then a second stage, the tree re-balances itself if it needs to.

61 AVL Tree u When removal occurs, the tree may become unbalanced. u There is, therefore, a second stage, the tree re-balances itself if it needs to.

62 AVL Tree u AVL trees are now considered inefficient and are therefore rarely used. u Trees are, however, so important that efficiency is necessary.

63 Red-Black Tree u These trees have a different algorithm for handling the modifications. u Instead of measuring the unbalancedness of the tree, each node is coloured.

64 Red-Black Tree u Insertion does not require two phases since the tree can be re-balanced as the position of the insertion point is found. u This makes it far more efficient than the AVL tree.

65 B-Tree u Used in database systems. u Not used in memory bound systems.

66 End of this Session