DSC 5th Unit Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Data Structure Using C Shree Medha Degree College, Ballari

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

Root Node: The starting node of a tree is called as Root Node.


• Tree will have only one Root Node.
• From the above example the Root node is: A

Edge: The link or connection between two nodes is called as an Edge.


• if there are n nodes the edges are n-1.
• From the above example the edges are: N = 7 nodes, E = 6 edges.

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

Level: Every step or hierarchy in a tree is called as Level.


1. Level of a tree starts from 0.
2. Every step of Hierarchy the level will be incremented by 1.
3. Level of tree=2

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

1. Strictly Binary Tree:


• Strictly binary tree is a binary tree that has non empty left and right sub tree.
• The out degree of every node is either 0 or 2.
• The nodes with 2 children’s are called as internal nodes.
• The nodes with no children’s are called as external nodes.

Full/Complete Binary Tree:


• Full or complete binary tree is a binary tree that contains maximum number of possible nodes in
all levels.
• In a full binary tree, all the leaf’s are at same level.
• In a full binary tree, all internal nodes have equal degree.
• The out degree of every node is 2 except terminal nodes.

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

Rules Of Binary Tree Traversal Are:


1. PRE ORDER: Root-Left Subtree-Right Subtree.
2. IN ORDER: Left Subtree-Root-Right Subtree.
3. POST ORDER: Left Subtree-Right Subtree-Root.

Pre Order Binary Tree Traversal:


Rules to be followed for the pre-order technique is:
1. Process the root R. Root Left Right
2. Traverse left Subtree of root R in preorder.
3. Traverse the right Subtree of Root R in preorder.

ABDECFG

Algorithm Pre -Order (Root):


Step 1: [process the root node]
If root=/NULL, then
Process info[root]
Else
Write “Tree is empty”
Step 2: [Traverse the left Sub tree recursively in preorder]
If left [root =/ NULL]
Call pre-order (left[root])
Step 3: [Traverse the right Sub tree recursively in preorder]

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.

C - Implementation Of Pre Order:


C Code for Pre - Order:
void pre – order (NODE *ptr)
{
if (ptr)
{
printf (“%d”, ptr info);
pre – order (ptr Left);
pre – order (ptr Right);
}
}

In - Order Binary Tree Traversal:


Rules to be followed for the In-order technique is:
1. Traverse left Subtree of root R in in-order.
2. Process the root R.
3. Traverse the right Subtree of Root R in in-order.

Left Root Right

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);
}
}

Post Order Binary Tree Traversal:


Rules to be followed for the post-order technique is:
1. Traverse left Subtree of root R in preorder.
2. Traverse the right Subtree of Root R in preorder.
3. Process the root R.

Left Root 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

Algorithm Post-Order (Root):


Step 1: [if root=/NULL, then
Write “Tree is empty”
Step 2: [Traverse the left Sub tree recursively in preorder]
if left [root=/ NULL ]
Call pre-order (left[root])
Step 3: [Traverse the right Sub tree recursively in preorder]
if right[root]=/NULL
Call pre-order (right [root])
Step 4: [process the Root node]
Info[root]
Step 5: return.

C Implementation of Post Order:


C Code for Post - Order:
void pos t- order (NODE *ptr)
{
if(ptr)
{
Post – order (ptr Left);
Post - order (ptr Right);
printf (“%d”, ptr info);
}
}

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.

CASE - 1: Index starts from 0


0 1 2 3 4 5 6 7 8 9 10

CASE - 2: Index Starts from 1

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.

Max-Heap Min - Heap

Dept. of Computer Science 10 of 10 From the desk of Mr. Chaitanya Reddy Mtech

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy