0% found this document useful (0 votes)
80 views24 pages

Tree Data Structures Guide

The document provides an overview of tree and graph data structures, including their definitions, basic terminologies, types, properties, and traversal methods. It covers various types of trees such as binary trees, AVL trees, and red-black trees, as well as graph types like directed and undirected graphs. Additionally, it explains traversal techniques for both trees and graphs, including in-order, pre-order, post-order, DFS, and BFS.

Uploaded by

21230873
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views24 pages

Tree Data Structures Guide

The document provides an overview of tree and graph data structures, including their definitions, basic terminologies, types, properties, and traversal methods. It covers various types of trees such as binary trees, AVL trees, and red-black trees, as well as graph types like directed and undirected graphs. Additionally, it explains traversal techniques for both trees and graphs, including in-order, pre-order, post-order, DFS, and BFS.

Uploaded by

21230873
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT-4 [TREES]

TOPIC-1 [INTRODUCTION TO TREES]


A tree data structure is a hierarchical structure that is used to
represent and organize data in a way that is easy to navigate and
search. It is a collection of nodes that are connected by edges and
has a hierarchical relationship between the nodes.
The topmost node of the tree is called the root, and the nodes
below it are called the child nodes. Each node can have multiple
child nodes, and these child nodes can also have their own child
nodes, forming a recursive structure.

Basic Terminologies in Tree Data Structure:

• Parent Node: The node which is a predecessor of a node


is called the parent node of that node. {B} is the parent
node of {D, E}.
• Child Node: The node which is the immediate successor
of a node is called the child node of that node.
Examples: {D, E} are the child nodes of {B}.
• Root Node: The topmost node of a tree or the node
which does not have any parent node is called the root
node. {A} is the root node of the tree. A non-empty tree
must contain exactly one root node and exactly one path
from the root to all other nodes of the tree.
• Leaf Node or External Node: The nodes which do not
have any child nodes are called leaf nodes. {K, L, M, N, O,
P, G} are the leaf nodes of the tree.
• Ancestor of a Node: Any predecessor nodes on the path
of the root to that node are called Ancestors of that
node. {A,B} are the ancestor nodes of the node {E}
• Descendant: A node x is a descendant of another node y
if and only if y is an ancestor of y.
• Sibling: Children of the same parent node are called
siblings. {D,E} are called siblings.
• Level of a node: The count of edges on the path from the
root node to that node. The root node has level 0.
• Internal node: A node with at least one child is called
Internal Node.
• Neighbour of a Node: Parent or child nodes of that node
are called neighbours of that node.
• Subtree: Any node of the tree along with its descendant.

TOPIC-2 [TYPES OF TREES]


Types of Trees in Data Structure based on the number of children:

1. Binary Tree: A binary Tree is defined as a Tree data structure with


at most 2 children. Since each element in a binary tree can have only
2 children, we typically name them the left and right child.
For Example:
Consider the tree below. Since each node of this tree has only 2
children, it can be said that this tree is a Binary Tree.

Types of Binary Tree:


1. Binary Tree consists of following types based on the number
of children:
• Full Binary Tree: A full binary tree is a binary tree with
either zero or two child nodes for each node.

• Degenerate Binary Tree: A Tree where every internal


node has one child. Such trees are performance-wise
same as linked list. A degenerate or pathological tree is a
tree having a single child either left or right.
• Skewed Binary Tree: A skewed binary tree is a
pathological/degenerate tree in which the tree is either
dominated by the left nodes or the right nodes. Thus,
there are two types of skewed binary tree: left-skewed
binary tree and right-skewed binary tree.

2. Based on completion of levels, the binary tree can be divided


into following types:
• Complete Binary Tree: A Binary Tree is a Complete
Binary Tree if all the levels are filled except possibly the
last level and the last level has all keys as left as possible.
A complete binary tree is just like a full binary tree, but
with two major differences:
• Every level except the last level must be filled.
• All the leaf elements must lean towards the left.
• The last leaf element might not have a right sibling i.e. a complete
binary tree does not have to be a full binary tree.

• Perfect Binary Tree: A Binary tree is a Perfect Binary


Tree in which all the internal nodes have two children
and all leaf nodes are at the same level. A perfect binary
tree is a type of binary tree in which every internal node
has exactly two child nodes and all the leaf nodes are at
the same level.

• Balanced Binary Tree: A binary tree is balanced if the


height of the tree is O(Log n) where n is the number of
nodes. For Example, the AVL tree maintains O(Log n)
height by making sure that the difference between the
heights of the left and right subtrees is at most 1. Red-
Black trees maintain O(Log n) height by making sure that
the number of Black nodes on every root to leaf paths is
the same and that there are no adjacent red nodes.
Balanced Binary Search trees are performance-wise
good as they provide O(log n) time for search, insert and
delete.

Some Special Types of Trees: Based on node values, the Binary


Tree can be classified into the following special types:
• Binary Search Tree: Binary Search Tree is a node-based
binary tree data structure that has the following properties:
• The left subtree of a node contains only nodes with

keys lesser than the node’s key.


• The right subtree of a node contains only nodes with

keys greater than the node’s key.


• The left and right subtree each must also be a binary

search tree.

• AVL Tree: AVL tree is a self-balancing Binary Search


Tree (BST) where the difference between heights of left
and right subtrees cannot be more than one for all nodes.
Example of AVL Tree shown below:
The below tree is AVL because the differences between the
heights of left and right subtrees for every node are less than
or equal to 1.
• Red Black Tree: A red-black tree is a kind of self-balancing binary
search tree where each node has an extra bit, and that bit is often
interpreted as the color (red or black). These colors are used to
ensure that the tree remains balanced during insertions and
deletions. Although the balance of the tree is not perfect, it is good
enough to reduce the searching time and maintain it around O(log
n) time, where n is the total number of elements in the tree. This tree
was invented in 1972 by Rudolf Bayer.

Properties of Tree Data Structure:


• Number of edges: An edge can be defined as the connection
between two nodes. If a tree has N nodes, then it will have (N-1)
edges. There is only one path from each node to any other node
of the tree.
• Depth of a node: The depth of a node is defined as the length of
the path from the root to that node. Each edge adds 1 unit of length
to the path. So, it can also be defined as the number of edges in
the path from the root of the tree to the node.
• Height of a node: The height of a node can be defined as the
length of the longest path from the node to a leaf node of the tree.
• Height of the Tree: The height of a tree is the length of the
longest path from the root of the tree to a leaf node of the tree.
• Degree of a Node: The total count of subtrees attached to that
node is called the degree of the node. The degree of a leaf node
must be 0. The degree of a tree is the maximum degree of a node
among all the nodes in the tree.
Applications of Tree Data Structure:
• File System: This allows for efficient navigation and
organization of files.
• Data Compression: Huffman coding is a popular technique for
data compression that involves constructing a binary tree where
the leaves represent characters and their frequency of occurrence.
The resulting tree is used to encode the data in a way that
minimizes the amount of storage required.
• Compiler Design: In compiler design, a syntax tree is used to
represent the structure of a program.
• Database Indexing: B-trees and other tree structures are used in
database indexing to efficiently search for and retrieve data.

TOPIC-3 [TREE TRAVERSAL]


Tree Traversal refers to the process of visiting or accessing each node
of the tree exactly once in a certain order. Tree traversal algorithms
help us to visit and process all the nodes of the tree. Since tree is not a
linear data structure, there are multiple nodes which we can visit after
visiting a certain node. There are multiple tree traversal techniques
which decide the order in which the nodes of the tree are to be visited.
1. In-Order Traversal: This technique follows the 'left root right'
policy. It means that first left subtree is visited after that root node is
traversed, and finally, the right subtree is traversed. As the root node is
traversed between the left and right subtree, it is named In-order
traversal.
Hint: In-order traversal visits the node in the order: Left -> Root ->
Right.
Algorithm for In-order Traversal:
In-order(tree)
• Traverse the left subtree, i.e., call In-order(left->subtree)
• Visit the root.
• Traverse the right subtree, i.e., call In-order(right->subtree)

2. Pre-Order Traversal: This technique follows the 'root left right'


policy. It means that, first root node is visited after that the left subtree
is traversed recursively, and finally, right subtree is recursively
traversed. As the root node is traversed before (or pre) the left and right
subtree, it is called preorder traversal.

Hint: Preorder traversal visits the node in the order: Root -> Left ->
Right
Algorithm for Preorder Traversal:
Preorder(tree)
• Visit the root.
• Traverse the left subtree, i.e., call Preorder(left->subtree)
• Traverse the right subtree, i.e., call Preorder(right->subtree)

3. Post-Order Traversal: This technique follows the 'left-right root'


policy. It means that the first left subtree of the root node is traversed,
after that recursively traverses the right subtree, and finally, the root
node is traversed. As the root node is traversed after (or post) the left
and right subtree, it is called post-order traversal.
Hint: Post-order traversal visits the node in the order: Left -> Right -
> Root
Algorithm for Post-order Traversal:
Algorithm post-order(tree)
• Traverse the left subtree, i.e., call post-order(left->subtree)
• Traverse the right subtree, i.e., call post-order(right->subtree)
• Visit the root
TOPIC-4 [INTRODUCTION TO GRAPHS]
A graph can be defined as group of vertices and edges that are used to
connect these vertices. A graph can be seen as a cyclic tree, where the
vertices (Nodes) maintain any complex relationship among them
instead of having parent child relationship.
➢ Generally, a graph G is represented as G = ( V , E ), where V is
set of vertices and E is set of edges.
➢ The following is a graph with 5 vertices and 6 edges.
This graph G can be defined as G = ( V , E )
Where V = {A,B,C,D,E} &,
E= {(A,B),(A,C) (A,D),(B,D), (C,D), (B,E), (E,D) }.
TOPIC-5 [TYPES OF GRAPHS]
1. Null Graph: A graph is known as a null graph if there are no
edges in the graph.

2. Trivial Graph: Graph having only a single vertex, it is also the


smallest graph possible.

3. Undirected Graph: A graph in which edges do not have any


direction. That is the nodes are unordered pairs in the definition of
every edge.

4. Directed Graph: A graph in which edge has direction. That is the


nodes are ordered pairs in the definition of every edge.
5. Connected Graph: The graph in which from one node we can
visit any other node in the graph is known as a connected graph.

6. Disconnected Graph: The graph in which at least one node is not


reachable from a node is known as a disconnected graph.

7. Regular Graph: The graph in which the degree of every vertex


is equal to K is called K regular graph.

8. Complete Graph: The graph in which from each node there is an


edge to each other node.
TOPIC-6 [REPRESENTATION OF GRAPHS]
There are two ways to store a graph:
➢ Adjacency Matrix: In this method, the graph is stored in the
form of the 2D matrix where rows and columns denote vertices.
Each entry in the matrix represents the weight of the edge
between those vertices.

➢ Adjacency List: This graph is represented as a collection of


linked lists. There is an array of pointer which points to the edges
connected to that vertex.

TOPIC-7 [GRAPH TRAVERSAL]


Graph traversal is a technique used for a searching vertex in a graph.
The graph traversal is also used to decide the order of vertices is
visited in the search process. A graph traversal finds the edges to be
used in the search process without creating loops. That means using
graph traversal we visit all the vertices of the graph without getting
into looping path.
There are two graph traversal techniques and they are as follows:
1. DFS (Depth First Search): DFS traversal of a graph produces
a spanning tree as result. Spanning Tree is a graph without loops.
We use Stack data structure with maximum size of total number of
vertices in the graph to implement DFS traversal.
We use the following steps to implement DFS traversal:
• Step 1 - Define a Stack of size total number of vertices in the
graph.
• Step 2 - Select any vertex as starting point for traversal. Visit
that vertex and push it on to the Stack.
• Step 3 - Visit any one of the non-visited adjacent vertices of a
vertex which is at the top of stack and push it on to the stack.
• Step 4 - Repeat step 3 until there is no new vertex to be visited
from the vertex which is at the top of the stack.
• Step 5 - When there is no new vertex to visit then use back
tracking and pop one vertex from the stack.
• Step 6 - Repeat steps 3, 4 and 5 until stack becomes Empty.
• Step 7 - When stack becomes Empty, then produce final spanning
tree by removing unused edges from the graph
2. BFS (Breadth First Search): BFS traversal of a graph produces
a spanning tree as result. Spanning Tree is a graph without loops.
We use Queue data structure with maximum size of total number
of vertices in the graph to implement BFS traversal.
We use the following steps to implement BFS traversal...
• Step 1 - Define a Queue of size total number of vertices in the
graph.
• Step 2 - Select any vertex as starting point for traversal. Visit
that vertex and insert it into the Queue.
• Step 3 - Visit all the non-visited adjacent vertices of the vertex
which is at front of the Queue and insert them into the Queue.
• Step 4 - When there is no new vertex to be visited from the vertex
which is at front of the Queue then delete that vertex.
• Step 5 - Repeat steps 3 and 4 until queue becomes empty.
• Step 6 - When queue becomes empty, then produce final
spanning tree by removing unused edges from the graph

You might also like