You are on page 1of 29

CHAPTER 3 Data Structures

Page 1

Stack ADT
• Stack uses the following functions

Page 2

Initializing a Stack An empty stack has t = -1. Input Parameters: None Output Parameters: None empty() { return t == -1 } Page 3 . An empty stack has t = -1. Input Parameters: None Output Parameters: None stack_init() { t = -1 } Testing for an Empty Stack This algorithm returns true if the stack is empty or false if the stack is not empty.

The algorithm assumes that the stack is not empty. in which case.Adding an Element to a Stack This algorithm adds the value val to a stack. The most recently added item is at index t. t = -1. Input Parameters: val Output Parameters: None push(val) { t = t + 1 data[t] = val } Removing an Element From a Stack This algorithm removes the most recently added item from a stack. Input Parameters: None Output Parameters: None pop() { t = t – 1 } 7 6 5 4 3 2 1 0 t = -1 Page 4 . The algorithm assumes that the array is not full. The most recently added item is at index t unless the stack is empty. The stack is represented using an array data.

top() s. The code uses the abstract data type stack. Input Parameter: s (the stack) Output Parameters: None one(s) { if (s.empty()) return false val = s.pop() flag = s. Input Parameters: None Output Parameters: None top() { return data[t] } The following algorithm returns true if there is exactly one element on the stack.empty() s.Returning the Top Element in a Stack This algorithm returns. The stack is represented using an array data. the most recently added item in a stack. The algorithm assumes that the stack is not empty. but does not remove.push(val) return flag } Page 5 . The most recently added item is at index t.

The Queue ADT Page 6 .

An empty queue has r = f = -1. An empty queue has r = f = 1 1.Initializing a Queue This algorithm initializes a queue to empty.1 } 7 6 5 4 3 This algorithm returns true if the queue is empty or false 2 if the queue is not empty. 0 r = f = -1 Input Parameters: None Output Parameters: None empty() { return r == .1 } Testing for an Empty Queue Page 7 . Input Parameters: None Output Parameters: None queue_init() { r = f = .

The algorithm assumes that the queue is not full. and the least recently added item is at index f (front). The most recently added item is at index r (rear). Input Parameters: val Output Parameters: None enqueue(val) { if (empty()) r = f = 0 else { r = r + 1 if (r == SIZE) r = 0 } data[r] = val } Page 8 .Adding an Element to a Queue This algorithm adds the value val to a queue. r = f = -1. The queue is represented using an array data of size SIZE. If the queue is empty.

The most recently added item is at index r (rear). Input Parameters: None Output Parameters: None dequeue() { // does queue contain one item? if (r == f) r = f = -1 else { f = f + 1 if (f == SIZE) f = 0 } } Page 9 . r = f = -1. and the least recently added item is at index f (front). The algorithm assumes that the queue is not empty. If the queue is empty. The queue is represented using an array of size SIZE.Removing an Element From a Queue This algorithm removes the least recently added item from a queue.

Returning the Front Element in a Queue • This algorithm returns. but does not remove. the least recently added item in a queue. • The algorithm assumes that the queue is not empty. Input Parameters: None Output Parameters: None front() { return data[f] } Page 10 . The queue is represented using an array data. The least recently added item is at index f (front).

The first node is referenced by start.Inserting in a Linked List • This algorithm prints the data in each node in a linked list. Input Parameters: start Output Parameters: None print(start)  star is a pointer to the  top of the linked list { while (start != null) { println(start -> data) start = start -> next } } Page 11 .

The first node is referenced by start. Input Parameters: start Output Parameters: None print(start) { while (start != null) { println(start -> data) start = start -> next } } Page 12 .next = pos.next } Printing the Data in a Linked List This algorithm prints the data in each node in a linked list.Deleting the Data from a Linked List This algorithm deletes the node after the node referenced by pos.next. Input Parameters: pos Output Parameters: None delete(pos) { pos.

Initializing a Stack (linked list implementation) This algorithm initializes a stack to empty. is the top of the stack. referenced by t. Input Parameters: None Output Parameters: None stack_init() { t = null } Testing for an Empty Stack This algorithm returns true if the stack is empty or false if the stack is not empty. An empty stack has t = null. Input Parameters: None Output Parameters: None empty() { return t == null } Page 13 . The stack is implemented as a linked list. An empty stack has t = null. The start of the linked list.

referenced by t. referenced by t. Input Parameters: val Output Parameters: None push(val) { temp = new node temp -> data = val temp -> next = t t = temp } Removing an Element From a Stack This algorithm removes the most recently added item from a stack. The start of the linked list. The stack is implemented using a linked list. The algorithm assumes that the stack is not empty. The stack is implemented using a linked list. Input Parameters: None Output Parameters: None pop() { t = t -> next } Page 14 . The start of the linked list.Adding an Element to a Stack This algorithm adds the value val to a stack. is the top of the stack. is the top of the stack.

referenced by t. the most recently added item in a stack. The start of the linked list. The algorithm assumes that the stack is not empty. but does not remove. The stack is implemented using a linked list.Returning the Top Element in a Stack This algorithm returns. is the top of the stack. Input Parameters: None Output Parameters: None top() { return t -> data } Page 15 .

Adjacency List of a Graph Page 16 .

Graph Adjacency List Each element of the array represents a node in the graph and points to a list of nodes adjacent to it. Page 17 .

last { count = 0 ref = adj[i] while (ref != null) { count = count + 1 ref = ref. The graph is represented using adjacency lists. The next field in each node. The vertices are 1. . . 2. Input Parameter: adj Output Parameters: None degrees1(adj) { for i = 1 to adj.Computing Vertex Degrees Using Adjacency Lists This algorithm computes the degree of each vertex in a graph. except the last.next } println(“vertex ” + i + “ has degree ” + count) } } Page 18 . . references the next node in the list. . The next field of the last node is null. adj[i] is a reference to the first node in a linked list of nodes representing the vertices adjacent to vertex i.

where the ijth element is 1 if there is an edge between i and j. and 0 if not 0 1 2 3 0 0  1 0  0  1 2 3 1 0 1  0 1 1 1 0 1  1 1 0  Page 19 .Graph adjacency matrix The graph is represented using an adjacency matrix.

. and 0 if not. 2.Computing Vertex Degrees Using an Adjacency Matrix This algorithm computes the degree of each vertex in a graph. Input Parameter: am Output Parameters: None degrees2(am) { for i = 1 to am.last { count = 0 for j = 1 to am. where am[i][j] is 1 if there is an edge between i and j. The vertices are 1.last if (am[i][j] == 1) count = count + 1 println(“vertex ” + i + “ has degree ” + count) } } Page 20 . The graph is represented using an adjacency matrix am. . . .

Tree Terminology • A tree consists of a collection of elements or nodes. with each node linked to its successors • The node at the top of a tree is called its root • The links from a node to its successors are called branches • The successors of a node are called its children • The predecessor of a node is called its parent • Each node in a tree has exactly one parent except for the root node. which has no parent • Nodes that have the same parent are siblings • A node that has no children is called a leaf node • A generalization of the parent-child relationship is the ancestordescendent relationship Page 21 .

Page 22 .Binary tree • A tree T is a binary tree if each vertex has an out-degree of at most 2.

Preorder Traversal This algorithm performs a preorder traversal of the binary tree with root root. Input Parameter: root Output Parameters: None preorder(root) { if (root != null) { print (root -> data) //visit root preorder(root -> left) preorder(root -> right) } } Page 23 .

Counting Nodes in a Binary Tree This algorithm returns the number of nodes in the binary tree with root root. Input Parameter: root Output Parameters: None count_nodes(root) { if (root == null ) return 0 count = 1 // count root count = count + count_nodes(root -> left) // add in nodes in left subtree count = count + count_nodes(root -> right) // add in nodes in right subtree return count } Page 24 .

right) } } // visit root Page 25 . Input Parameter: root Output Parameters: None inorder(root) { if (root != null) { inorder(root.Inorder Traversal This algorithm performs an inorder traversal of the binary tree with root root.left) print root -> data inorder(root.

left) postorder(root.right) print root -> data // visit root } } Page 26 .Postorder Traversal This algorithm performs a postorder traversal of the binary tree with root root. Input Parameter: root Output Parameters: None postorder(root) { if (root != null) { postorder(root.

Beginning at the root of the tree. it must occur in the right hand sub-tree because of the Binary tree property Page 27 . we see that 39 is greater than 21. so that if 39 is present in the tree. Suppose the item is the number 39.Binary Search Tree • A binary search tree is a binary tree in which the value stored at each node of the tree is greater than the values stored in its left subtree and less than the values stored in its right subtree • If we want to find out whether a given object is present in a binary search tree. we can proceed as follows.

and nothing occurs in the empty tree. We have therefore established that 39 is not present in the tree Page 28 . We next compare 39 with 31. move to the right and finally compare 39 with 35. it must occur in the right hand sub-tree of the tree rooted at 35. Since 39 is greater than 35. if it occurs at all.Binary Search tree • Since 39 is less than 40. But the right-hand sub-tree at 35 is empty. it must occur in the left-hand sub-tree of the tree rooted at 40.

root = null.temp) return root } Page 29 .val) { // set up node to be added to tree temp = new node temp -> data = val temp.right = null if (root == null) // special case: empty tree return temp else if (temp -> data > root -> data) BSTinsert_recurs(root->right.left = temp. If the tree is empty.Inserting Into a Binary Search Tree This algorithm inserts the value val into a binary search tree with root root.temp) else BSTinsert_recurs(root->left.val Output Parameters: None BSTinsert(root. Input Parameter: root. The algorithm returns the root of the tree containing the added item. We assume that “new node” creates a new node with data field data and reference fields left and right.