Top Banner
Lists • A list is a finite, ordered sequence of data items. • Two Implementations – Arrays – Linked Lists
26

Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Lists

• A list is a finite, ordered sequence of data items.

• Two Implementations– Arrays– Linked Lists

Page 2: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Arrays

• Static Allocation of elements

20 23 54 12 23

Page 3: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Linked Lists• Dynamic allocation of new list elements.

Page 4: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Comparison of Implementations

Array-Based Lists:• Insertion is O( ).• Direct access is ( ).• No overhead if all array positions are full.

Linked Lists:• Insertion is ( ).• Direct access is O( ).• Every element requires overhead.

Page 5: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Stacks

LIFO: Last In, First Out.

Restricted form of list: Insert and remove only at front of list.

Notation:• Insert: PUSH• Remove: POP• The accessible element is called TOP.

Page 6: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Queues

FIFO: First in, First Out

Restricted form of list: Insert at one end, remove from the other.

Notation:• Insert: Enqueue• Delete: Dequeue• First element: Front• Last element: Rear

Page 7: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Graphs

A graph G = (V, E) consists of a set of vertices V, and a set of edges E, such that each edge in E is a connection between a pair of vertices in V.

The number of vertices is denoted by |V| or n, and the number of edges is denoted by |E| or m.

Page 8: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Paths and CyclesPath: A sequence of vertices v1, v2, …, vn of length n-1 with an

edge from vi to vi+1 for 1 i < n.

A path is simple if all vertices on the path are distinct.

A cycle is a path of length 3 or more that connects vi to itself.

A cycle is simple if the path is simple, except the first and last vertices are the same.

Page 9: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Connected Components

An undirected graph is connected if there is at least one path from any vertex to any other.

The maximum connected subgraphs of an undirected graph are called connected components.

Page 10: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Complete Graphs

• An undirected graph is complete if

• A directed graph is complete if

B

DC

A

E

Page 11: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Bipartite Graphs• An undirected graph, G=(V,E), is called bipartite if V

can be partitioned into two non-empty sets X and Y such that each edge e in E has one end in X and the other in Y.

• A complete bipartite graph is

BA E

C F

Page 12: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Adjacency Matrix Representation of Graphs

A

B

C

D

F

E A B C D E F

A

B

C

D

E

F

A

B

C

D

F

E

Page 13: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Adjacency List Representation of Graphs

A

B

C

D

F

E

A

B

C

D

F

E

0

1

2

3

4

5

Page 14: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Planar Graphs

• Definition: A graph G = (V,E) is planar if it can be embedded in the plane without edge crossings

Page 15: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Examples

B

DC

A B

DC

A

Page 16: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Examples

B

DC

A B

DC

A

Page 17: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Examples

B

DC

A E

F

Page 18: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Planar Graphs Properties

• Let m, n, and r denote the number of edges, the number of vertices, and the number of regions in a planar graph, respectively.

1. n – m + r = 2 (Euler’s Formula)

2. m 3n – 6– m = O(n)

Page 19: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Rooted Trees• A tree T is a connected undirected graph

containing no cycles• A rooted tree is a tree with a

distinguished vertex r, called the root.

Page 20: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Theorem

• If T is a tree with n vertices, then– Any two vertices of T are connected by a

unique path.– T has exactly n-1 edges– The addition of one or more edges to T creates

a cycle.

Page 21: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Binary Trees

Notation: Node, children, edge, parent, ancestor, descendant, path, depth, height, level, leaf node, internal node, subtree.

Page 22: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Full and Complete Binary Trees

Full binary tree: Each node is either a leaf or internal node with exactly two non-empty children.

Complete binary tree: If the height of the tree is d, then all levels except possibly level d are completely full. Level d has all nodes filled in to the left side.

Page 23: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Useful Theorems

Theorem: The number of leaves in a non-empty full binary tree is one more than the number of internal nodes.

Theorem: The number of nodes at level j 2j.

Theorem: The number of nodes, n, in a complete binary tree T of height k is such that

2k n 2k+1-1

Theorem: The height of any binary tree with n vertices is at least log n and at most n – 1.

Page 24: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Traversals (1)

Any process for visiting the nodes in some order is called a traversal.

Any traversal that lists every node in the tree exactly once is called an enumeration of the tree’s nodes.

Page 25: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Traversals (2)

• Preorder traversal: Visit each node before visiting its children.

• Postorder traversal: Visit each node after visiting its children.

• Inorder traversal: Visit the left subtree, then the node, then the right subtree.

Page 26: Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists.

Binary Search Trees

BST Property: All elements stored in the left subtree of a node with value K have values < K. All elements stored in the right subtree of a node with value K have values >= K.