0% found this document useful (0 votes)
40 views

Programming and Problem Solving Module5

The document discusses different types of trees like binary trees and complete binary trees. It describes tree terminology and different tree traversal methods like pre-order, in-order, and post-order traversal. Finally, it covers graph terminology and explains the breadth-first search algorithm for traversing graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Programming and Problem Solving Module5

The document discusses different types of trees like binary trees and complete binary trees. It describes tree terminology and different tree traversal methods like pre-order, in-order, and post-order traversal. Finally, it covers graph terminology and explains the breadth-first search algorithm for traversing graphs.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

PROGRAMMING AND PROBLEM SOLVING

(MODULE 5- DATASTRUCTURES & TREES &


LINKED LIST)
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
2. TREES :
A tree is defined as a set of one or more nodes where one node is designated as the root of the tree and all the
remaining nodes are sub-tree of the root.

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.

From the above fig:


R is the root node and the two trees T1 and T2 are called the left and right sub-trees of R.
T1 is said to be the left successor of R & T2 is called the right successor of R.
The left sub-tree of the root node consists of the nodes: 2, 4, 5, 8, and 9 & the right sub-tree of the root
node consists of nodes: 3, 6, 7,10, 11, and 12.
In the tree,
Root Node 1 has two successors: 2 and 3.
Node 2 has two successor nodes: 4 and 5.
Node 3 has two successor nodes: 6 and 7.
Node 4 has two successors: 8 and 9.
Node 5 has no successor.
Node 6 has two successors: 10 and 11.
Node 7 has only one successor: 12
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
2.1 TYPES OF TREES :
(2) COMPLETE BINARY TREE :
 A complete binary tree is a binary tree that satisfies two properties.
(i) First, in a complete binary tree, every level, except possibly the last, is completely filled
(ii) Second, all nodes appear as far left as possible
• The below figure is a complete binary tree
PROGRAMMING & PROBLEM SOLVING (MODULE 5)

TRAVERSING A BINARY TREE


(i) Pre-order traversal :
To traverse a non-empty binary tree in pre-order, the following operations are performed recursively at each
node. The algorithm works by:
1. Visiting the root node, 2. Traversing the left sub-tree, and finally 3. Traversing the right sub-tree.

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

Example of Pre-order traversal : TRAVERSAL ORDER: A, B, D, G, H, L, E, C, F, I, J, and K


Example of In-order traversal : TRAVERSAL ORDER: G, D, H, L, B, E, A, C, I, F, K, and J
Example of Post-order traversal : TRAVERSAL ORDER: G, L, H, D, E, B, I, K, J, F, C, and A
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
GRAPH TERMINOLOGY
1. Adjacency node : Two nodes are said to be adjacent if they are connect to each other through an edge.
2. Degree of a node : Degree of a node is the total number of edges containing the node .
3. Regular graph : Regular graph is a graph where every node has the same degree.
4. Path : Path represents a sequence of edges between the two vertices.
5. Closed path : The edge which has the same end-points are known as closed path.
6. Cycle : A path in which the first and the last vertices are same.
7. Connected graph : A graph is said to be connected if for any two vertices (u, v) in V there is a path from u
to v.
8. Complete graph :A graph is said to be complete if all its nodes are fully connected. A complete graph has
n(n–1)/2 edges.
9. Labelled graph or weighted graph A graph is said to be labelled if every edge in the graph is assigned
some data. In a weighted graph, the edges of the graph are assigned some weight or length
PROGRAMMING & PROBLEM SOLVING (MODULE 5)
BREADTH FIRST SEARCH ALGORITHM
• Breadth-first search is the most common search strategy for traversing graph. This algorithm searches
breadthwise in a graph, so it is called breadth-first search.
• BFS algorithm starts searching from the root node and expands all successor node at the current level
before moving to nodes of next level.
Algorithm for breadth first search
Step 1: SET STATUS=1 (ready state) for each node in G
Step 2: Enqueue the starting node A and set its STATUS=2 (waiting state)
Step 3: Repeat Steps 4 and 5 until QUEUE is empty
Step 4: Dequeue a node N. Process it and set its STATUS=3 (processed state).
Step 5: Enqueue all the neighbours of N that are in the ready state (whose STATUS=1) and set their STATUS=2
[END OF LOOP]
Step 6 : EXIT

You might also like