M211

M211
Introduction to Algorithms and Data Structures
Week 12
Prepared by: Dr. Bayan Abu shawar
AOU-Jordan
1. Basic terminologies relates to Trees
A rooted tree: is a tree which has a vertex which is designed as the root and every edge is
directed away from the root.
A vertex: is called a leaf if it has no children.
Ancestors of a vertex: are the vertices in the path from the root to this vertex, excluding
vertex itself.
Descendants of a vertex V: are those vertices that have V as an ancestor.
Siblings: nodes (brothers) who have the same parent.
The depth of a node n is the length of the path from the root to the node.
The set of all nodes at a given depth is sometimes called a level of the tree. The root node is
at depth zero.
The height of a rooted tree is the length of the longest path from root to any vertex.
Example:
 The root of tree is: b
 a & c are siblings of b
 e and f are siblings of d
 ancestor of f are: d, a, b
 decedents of a vertex a: e, d, f, g
1
2. Binary tree and tree traversal
 Binary tree: is a tree in which each node has at most two children.
 A full binary tree is a tree in which every node has zero or two children.
 A perfect binary tree (complete binary tree) is a full binary tree in which all leaves
are at the same depth.
In computer science, tree-traversal refers to the process of visiting each node in a tree,
exactly once, in a systematic way. Such traversals are classified by the order in which the
nodes are visited. The general recursive pattern for traversing a (non-empty) binary tree is
this: At node N you must do these three things:



(L) recursively traverse its left subtree. When this step is finished you are back at N
again.
(R) recursively traverse its right subtree. When this step is finished you are back at N
again.
(N) Actually process N itself.
There are3 forms of visiting these nodes:
1. Pre-order traversal
2. In-order traversal
3. Post-order traversal
 Pre-order (depth-first) traversal, +perform the following operations recursively at each
node, starting with the root node:
1. Visit the node.
2. Traverse the left subtree.
3. Traverse the right subtree.
Simply: do (N) before anything else.
 In-order traversal, perform the following operations recursively at each node, starting
with the root node:
1. Traverse the left sub-tree.
2. Visit the node.
3. Traverse the right sub-tree.
Simply: do (N) in between the two sub-trees.
2
 Post-order traversal, perform the following operations recursively at each node, starting
with the root node:
1. Traverse the left sub-tree.
2. Traverse the right sub-tree.
3. Visit the node.
Simply: do (N) after everything else
 Level-order (breadth-first) traversal: where we visit every
node on a level before going to a lower level.
Example: In this binary search tree,
 Preorder traversal sequence: F, B, A, D, C, E, G, I, H
 Inorder traversal sequence: A, B, C, D, E, F, G, H, I
o Note that the inorder traversal of this binary search tree yields an ordered list
 Postorder traversal sequence: A, C, E, D, B, H, I, G, F
 Level-order traversal sequence: F, B, G, A, D, I, C, E, H
Example: assume you have the below binary tree
Solution:
Pre-Order: A-B-D-C-E-F
In-Order: B-D-A-E-C-F
Post-Order: D-B-E-F-C-A
Breadth first traverse: A-B-C-D-E-F
3
What is a Heap Data Structure?
1. It is a complete binary tree; that is, each level of the tree is completely filled, except
possibly the bottom level. At this level, it is filled from left to right.
2. It satisfies the heap-order property: The data item stored in each node is greater than
or equal to the data items stored in its children.
Example:
In the above examples only (a) is a heap. (b) is not a heap as it is not complete and (c) is
complete but does not satisfy the second property defined for heaps.
Heap as an Array
We could implement array as an array, where u take row by row from tree and fill array. In
other words, we simply number the nodes in the heap from top to bottom, numbering the
nodes on each level from left to right and store the ith node in the ith location of the array. (of
course remembering that array indexing in Java starts at zero).
4
Example: The heap and its representation in an array is shown below:
Note that: The root of the tree is stored at A[0], its left-child at A[1], its right child at A[2]
etc.
Accessing the Heap Values
Given the index i of a node, the indices of its parent Parent(i), left-child LeftChild(i) and right
child RightChild(i) can be computed simply :
Heaps must also satisfy the heap property for every node, i, other than the root.
A[Parent(i)] ≥ A[i]
Therefore, the largest element in a heap is stored at the root, and the subtrees rooted at a node
contain smaller values than does the node itself.
Running time complexity of building a heap is: O(n log n)
5
Example: building a heap
Heap Sort:
HeapSort - O(N log N) sorting algorithm, it works as follows:
While heap is not empty
Remove root and insert it at the end of the array
Remove last element in heap and insert it in root position
Reconstruct the heap correctly (heapify)
6
Example:
7
Heap sort is:
8
Example: Sort below tree according to heap sort
9
Heap sort is:
10