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

DS L-5

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)
21 views

DS L-5

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/ 29

DATA STRUCTURES

LECTURE-5

TREE

Dr. Sumitra Kisan


Tree
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.

A data structure which consists of


• a finite set of elements called nodes or vertices
• a finite set of directed arcs which connect the nodes

If the tree is nonempty


• one of the nodes (the root) has no incoming arc
• every other node can be reached by following a unique sequence of consecutive arcs

Tree is a non-linear data 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. {I, J, K,
F, G, H} 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 x.
➢ 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 neighbors of that node.
➢ Subtree: Any node of the tree along with its descendant.
Basic Terminologies In Tree Data Structure
Types of Tree data structures
•Binary tree: In a binary tree, each node can have a maximum
of two children linked to it. Some common types of binary trees
include full binary trees, complete binary trees, balanced binary
trees, and degenerate or pathological binary trees.
•Ternary Tree: A Ternary Tree is a tree data structure in which
each node has at most three child nodes, usually distinguished
as “left”, “mid” and “right”.
•N-ary Tree or Generic Tree: Generic trees are a collection of
nodes where each node is a data structure that consists of
records and a list of references to its children. Unlike the linked
list, each node stores the address of multiple nodes.
Basic Operations Of Tree Data Structure

➢ Create – create a tree in the data structure.


➢ Insert − Inserts data in a tree.
➢ Search − Searches specific data in a tree to check whether it is present or not.
➢ Traversal:
➢ Depth-First-Search Traversal
➢ Breadth-First-Search Traversal
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.
•Degree of node A = 2
•Degree of node B = 3
•Degree of node C =
•Degree of node D =
•Degree of node E =
•Degree of node F =
•Degree of node G =
•Degree of node H =
•Degree of node I =
•Degree of node J =
•Degree of node K =
Height

➢ Total number of edges that lies on the longest path


from any leaf node to a particular node is called
as height of that node.
➢ Height of a tree is the height of root node.
➢ Height of all leaf nodes = 0

Height of node A =
Height of node B = 2
Height of node C =
Height of node D =
Height of node E =
Height of node F =
Height of node G =
Height of node H = 0
Height of node I =
Height of node J =
Height of node K =
Depth
➢ Total number of edges from root node to a particular node is
called as depth of that node.
➢ Depth of a tree is the total number of edges from root node to
a leaf node in the longest path.
➢ Depth of the root node = 0
➢ The terms “level” and “depth” are used interchangeably.
•Depth of node A =
•Depth of node B =
•Depth of node C =
•Depth of node D =
•Depth of node E =
•Depth of node F =
•Depth of node G =
•Depth of node H = 2
•Depth of node I =
•Depth of node J =
•Depth of node K =
Binary Trees
Binary tree is a tree data structure(non-linear) in which
each node can have at most two children which are
referred to as the left child and the right child Root

Useful in modeling processes where Parents


• a comparison or experiment has exactly two
possible outcomes
• the test is performed repeatedly

Leaf
Properties of Binary Tree

➢ The maximum number of nodes at level L of a binary tree is 2L


➢ Total number of leaf nodes in a binary tree = total number of nodes with 2 children + 1
➢ In a Binary Tree with N nodes, the minimum possible height or the minimum number of levels
is Log2(N+1)
➢ A Binary Tree with L leaves has at least | Log2L |+ 1 levels
Types of Binary Trees
➢ A balanced Binary Tree has at most 1 in difference between its left and right subtree heights, for each
node in the tree.
➢ A complete Binary Tree has all levels full of nodes, except the last level, which can also be full, or filled
from left to right. The properties of a complete Binary Tree means it is also balanced.
➢ A full Binary Tree is a kind of tree where each node has either 0 or 2 child nodes.
➢ A perfect Binary Tree has all leaf nodes on the same level, which means that all levels are full of nodes,
and all internal nodes have two child nodes. The properties of a perfect Binary Tree means it is also full,
balanced, and complete.
➢ Skewed Binary Tree- A skewed binary tree is a binary tree that satisfies the following 2 properties-
All the nodes except one node has one and only one child.
The remaining node has no child.
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Unlabeled Binary Tree

Binary tree is unlabeled if its nodes are not assigned any label.
Example-

Consider, we want to draw all the binary trees possible with 3 unlabeled nodes.

Number of binary trees possible with 3 unlabeled nodes

= 2𝑥3𝐶 3 / (3 + 1)
= 6C3 / 4
=5
Labeled Binary Tree

A binary tree is labeled if all its nodes are assigned a label.


Example

Consider, we want to draw all the binary trees possible with 3 labeled nodes.
Using the above formula, we have-

Number of binary trees possible with 3 labeled nodes


= { 2 x 3C3 / (3 + 1) } x 3!
= { 6C3 / 4 } x 6
=5x6
= 30

Similarly,
•Every other unlabeled structure gives rise to 5 different
labeled structures.
•Thus, in total 30 different labeled binary trees are possible.
Binary Tree Properties

1. Minimum number of nodes in a binary tree of height H


=H+1

To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes


2. Maximum number of nodes in a binary tree of height H
= 𝟐𝑯+𝟏 – 1
Example:
Maximum number of nodes in a binary tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
Thus, in a binary tree of height = 3, maximum number of
nodes that can be inserted = 15.
3. Total Number of leaf nodes in a Binary Tree
= Total Number of nodes with 2 children + 1

Example-

Consider the following binary tree-

Here,

•Number of leaf nodes = 3


•Number of nodes with 2 children = 2

Clearly, number of leaf nodes is one greater than number


of nodes with 2 children.
4. Maximum number of nodes at any level ‘L’ in a binary tree= 2L
Example-

Maximum number of nodes at level-2 in a binary tree


= 22
=4

Thus, in a binary tree, maximum number of nodes that


can be present at level-2 = 4.
Tree Traversal Techniques

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.
Inorder Traversal

Inorder traversal visits the node in the order: Left -> Root -> Right

Algorithm:

Inorder(tree)

•Traverse the left subtree, i.e., call Inorder(left->subtree)


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

Algorithm

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)
Postorder Traversal

Postorder traversal visits the node in the order: Left -> Right -> Root

Algorithm

Postorder(tree)
•Traverse the left subtree, i.e., call Postorder(left->subtree)
•Traverse the right subtree, i.e., call Postorder(right->subtree)
•Visit the root
Breadth First Traversal

➢ Breadth First Traversal of a tree prints all the nodes of a tree level by level.

➢ Breadth First Traversal is also called as Level Order Traversal.

➢ Level Order Traversal visits all nodes present in the same level completely before

visiting the next level


Algorithm:

LevelOrder(tree)
•Create an empty queue Q
•Enqueue the root node of the tree to Q
•Loop while Q is not empty
• Dequeue a node from Q and visit it
• Enqueue the left child of the dequeued node if it exists
• Enqueue the right child of the dequeued node if it exists
Example:
If the binary tree in figure is traversed in inorder, then the order in which the nodes will be visited is ____?

Preorder:
Postorder:

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