Académique Documents
Professionnel Documents
Culture Documents
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 stack_init() { t = -1 }
Page 4
Page 5
Page 6
Initializing a Queue
This algorithm initializes a queue to empty. An empty queue has r = f = -1. Input Parameters: None Output Parameters: None queue_init() { r = f = - 1 }
7 6 5 4 3 This algorithm returns true if the queue is empty or false 2 if the queue is not empty. An empty queue has r = f = 1 1. 0 r = f = -1 Input Parameters: None Output Parameters: None empty() { return r == - 1 }
Page 7
Page 8
Page 10
Page 15
Page 16
Each element of the array represents a node in the graph and points to a list of nodes adjacent to it.
Page 17
Input Parameter: adj Output Parameters: None degrees1(adj) { for i = 1 to adj.last { count = 0 ref = adj[i] while (ref != null) { count = count + 1 ref = ref.next } println(vertex + i + has degree + count) } }
Page 18
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
Input Parameter: am Output Parameters: None degrees2(am) { for i = 1 to am.last { count = 0 for j = 1 to am.last if (am[i][j] == 1) count = count + 1 println(vertex + i + has degree + count) } }
Page 20
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
Binary tree
A tree T is a binary tree if each vertex has an out-degree of at most 2.
Page 22
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
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
Inorder Traversal
This algorithm performs an inorder traversal of the binary tree with root root. Input Parameter: root Output Parameters: None inorder(root) { if (root != null) { inorder(root.left) print root -> data inorder(root.right) } }
// visit root
Page 25
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.left) postorder(root.right) print root -> data // visit root } }
Page 26
If we want to find out whether a given object is present in a binary search tree, we can proceed as follows. Suppose the item is the number 39. Beginning at the root of the tree, we see that 39 is greater than 21, so that if 39 is present in the tree, it must occur in the right hand sub-tree because of the Binary tree property
Page 27
Page 28