Programming and Problem Solving Module5
Programming and Problem Solving Module5
2.1 TERMINOLOGIES
(i) Root Node : It is the topmost node in the tree.
(ii) Sub Trees : If the root node R is not NULL, then the trees T1,T2, and T3 are called the sub-trees of R.
(iii) Leaf Node : A node that has no children is called the leaf node or the terminal node.
(iv) Path : A sequence of consecutive edges is called a path. Example : The path from the root node A to I is
A,D & I.
(v) Ancestor Node : An ancestor node is any predecessor node on the path from root to that node.
Example : nodes A, C, and G are the ancestors of node K.
(vi) Descendent Node : A descendant node is any successor node on any path from the node to a leaf node.
Example : nodes C, G, J, and K are the descendants of node A
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
2.1 TYPES OF TREES :
(1) BINARY TREE :
A binary tree is a data structure that is defined as a collection of elements called nodes.
The pre-order traversal of the tree is given as A, B, C. Root node first, the left sub-tree next, and then the right
sub-tree. In this algorithm, the left sub-tree is always traversed before the right sub-tree.
(ii) In-order traversal :
To traverse a non-empty binary tree in in-order, the following operations are performed recursively at each node.
The algorithm works by:
2. Traversing the left sub-tree, 2. Visiting the root node, and finally 3. Traversing the right sub-tree.
The in-order traversal of the tree is given as B, A, C. Left sub-tree first, the root node next, and then the right sub-
tree. In this algorithm, the left sub-tree is always traversed before the root node and the right subtree
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
TRAVERSING A BINARY TREE
(iii) Post-order traversal :
To traverse a non-empty binary tree in post-order, the following operations are performed recursively at each
node. The algorithm works by:
1. Traversing the left sub-tree, 2. Traversing the right sub-tree, and finally 3. Visiting the root node.
The post-order traversal of the tree is given as B, C, and A. Left sub-tree first, the right sub-tree next,
and finally the root node. In this algorithm, the left sub-tree is always traversed before the right
sub-tree and the root node.
Examples of all of these traversals