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

## Presentation on theme: "Lists A list is a finite, ordered sequence of data items. Two Implementations –Arrays –Linked Lists."— Presentation transcript:

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

Arrays Static Allocation of elements 2023541223

Linked Lists Dynamic allocation of new list elements.

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.

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.

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

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.

Paths and Cycles Path: A sequence of vertices v 1, v 2, …, v n of length n-1 with an edge from v i to v i+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 v i to itself. A cycle is simple if the path is simple, except the first and last vertices are the same.

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.

Complete Graphs An undirected graph is complete if A directed graph is complete if B D C A E

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

Adjacency Matrix Representation of Graphs A B C D F E ABCDEF A B C D E F A B C D F E

Adjacency List Representation of Graphs A B C D F E A B C D F E 0 1 2 3 4 5

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

Examples B DC A B DC A

B DC A B DC A

B DC AE F

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)

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.

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.

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

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.

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  2 j. Theorem: The number of nodes, n, in a complete binary tree T of height k is such that 2 k  n  2 k+1 -1 Theorem: The height of any binary tree with n vertices is at least  log n  and at most n – 1.

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.

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.

Binary Search Trees BST Property: All elements stored in the left subtree of a node with value K have values = K.

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

Similar presentations