DSC 5th Unit Notes
DSC 5th Unit Notes
DSC 5th Unit Notes
TREES
Tree: A tree is a finite set of one or more nodes such that:
1. There is specially designated node called “THE ROOT NODE”.
2. The remaining nodes are partitioned n (n>0) disjoint sets T1 , T2,T3,---------Tn , which are
called as SUB TREES of the root.
Definitions:
Node: Elements of a tree is called as Node. From the above example the nodes are :A,B,C,D,E,F,G
Parent Node: A node with branches from top to bottom is called as Parent node. From the above example the
parent nodes are: A, B, C.
Child Node: A node with edge from bottom to top or the branches of parent node are called as Child node.
From the above example the Child nodes are: B, C, D, E, F, G.
Siblings: The nodes on the same hierarchical level under the same parent node are called as Siblings. From the
above example the Siblings are: B-C: Siblings, D-E: Siblings, F-G: Siblings.
Dept. of Computer Science 1 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Leaf: A node without child node is called a Leaf. From the above example the Leaf’s are: D, E, F, G.
Internal Nodes: All nodes other than leaf nodes are called as Internal nodes.
1. Nodes with child node is an internal node.
2. From the above example internal nodes are: A, B, C.
Degree of a Node: The number of child nodes represents the degree of a nodes. From the above example:
• Degree of A:2
• Degree of B:2
• Degree of C:2
• Degree of D:0
• Degree of E:0
• Degree of F:0
• Degree of G:0
Height: Longest path from leaf node to that node is called as Height. From the above example the height of (A)
is 2.
Depth: Longest path from root node to that node is called as Depth. From the above example the Depth of (D)
is 2, Depth of (g) is 2, Depth of (B) is 1.
Path: Sequence of node from root to leaf is called as Path. From the above example the Path of (A to D) is: A-
B-D, Path of (A to G) is: A-C-G.
Sub-Tree: Node with child node forms a Sub-tree. From the above example the Sub-trees are:
A, B and C, Sub-tree: B, D and E, Sub-tree: C, F and G, Sub-tree: A, B, C, D, E, F, G.
Ancestor Node: A node that is connected to all lower-level nodes is called as Ancestor node. From the above
example the ancestor nodes are: A, B and C.
Terminal Node: A node with degree (0) zero is called as Terminal node. From the above example the terminal
nodes are: D, E, F, G. It is also called as Leaf node.
Non-Terminal Node: A node without degree (0) zero is called as non-Terminal node. From the above example
the non-terminal nodes are: A, B, C.
Dept. of Computer Science 2 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Binary Tree: Binary tree T is a finite set of nodes such that:
1. T is empty (empty Binary tree) or
2. T contains a specially designated node called Root and
3. The remaining nodes of tree form only upto two disjoint binary tree T1, T2 which are called left and
right subtree respectively.
• Level (Tree):4
• Ancestor (Tree):A,B,C,D,E
• Terminal Nodes (Tree):H,I,J,F,G
• Non-Terminal Nodes (Tree): A, B, C,D,E
• Siblings: B-C,D-E,F-G,H-I
• Node: A,B,C,D,E,F,G,H,I,J
• Edge: N=10 nodes,E=9 edges
• Parent Nodes: A,B,C,D,E
• Child Nodes: B,C,D,E,H,J,I,F,G
Dept. of Computer Science 3 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Types Of Binary Tree
1. Strictly Binary Tree
2. Full/Complete Binary Tree
Dept. of Computer Science 4 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Binary Tree Traversal
• Traversing means visiting each node exactly once in a systematic manner.
• There are three ways of traversing a binary tree they are:
1. PRE ORDER
2. IN ORDER
3. POST ORDER
ABDECFG
Dept. of Computer Science 5 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
If right[root]=/NULL
Call pre-order (right [root])
Step 4: return.
DBEAGFC
Dept. of Computer Science 6 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Algorithm In-Order (Root):
Step 1: [Traverse the left sub tree of the root R in in-order]
If root == NULL, then
Write “Tree is empty”
Step 2: [If left [root=/ NULL]
Call in-order (left[root])
Step 3: [process the root node]
Info[root]
Step 4: [Traverse the (right([root]) Sub tree of the root in in-order]
If right[root]=/NULL
Call In-order (right [root])
Step 5: return
C IMPLEMENTATION OF IN ORDER:
C Code for In - Order:
void in-order(node *ptr)
{
if(ptr)
{
in – order (ptr left);
printf (“%d”, ptr info);
in – order (ptr right);
}
}
Dept. of Computer Science 7 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
DEBFGCA
Dept. of Computer Science 8 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Array Representation of Binary Tree: It can also be called as contagious or sequential representation of
binary tree.
1 2 3 4 5 6 7 8 9 10 11 12 13
Formulas Or Rules for Case-1 Index: Starts at 0 if a node is at ith index the its
1. Left child is at (2*i) + 1 position
2. Right child is at(2*i) + 2 position
3. Parent is at (i-1/2) position
Formulas or Rules for Case-2 Index: Starts at 1 if a node is at ith index, then its
1. Left child is at (2*i) position
2. Right child is at(2*i)+1 position
3. Parent is at (i/2) position
Dept. of Computer Science 9 of 10 From the desk of Mr. Chaitanya Reddy Mtech
Data Structure Using C Shree Medha Degree College, Ballari
Binary Search Tree
1. A binary search tree is also known as an ordered binary tree.
2. Binary search tree is a variant of binary tree in which the nodes are arranged in an order.
In binary search tree, all the nodes of the left sub-tree have a value less than of the root node.
3. Also, all the node in the right sub-tree have either equal to or greater than the root node.
Heap Tree:
1. A binary heap is a complete binary tree in which every node satisfies the heap property which states
that: if B is a child of A, there key>=key(B)
2. When elements at every node will be either greater than or equal to the element at its left and right child
then it is called as Max-heap.
3. Here, root node has the highest key value in the heap.
4. When elements at every node will be either less than or equal to the elements to its left and right child
as Min-heap.
5. Here root node has the lowest key value in the heap.
Dept. of Computer Science 10 of 10 From the desk of Mr. Chaitanya Reddy Mtech