Chapter 7 - Tree DS
Chapter 7 - Tree DS
1
Introduction
• Tree is a hierarchical data structure that consists of
nodes connected by edges. It's a non-linear data
structure where each node can have zero or more
child nodes.
• Each node has only one parent (except for the root node,
which has no parent).
• Tree is a collection of data
(Node) which is organized in
hierarchical structure.
2
Introduction
• Node in a tree data structure stores the actual data
of that particular element and link to next element
in hierarchical structure.
• In a tree, if we have N number of nodes then we
can have a maximum of N-1 number of links
3
Tree Terminology
Root Node
• In a tree data structure, the first node is called as Root Node.
• Every tree must have a root node. We can say that the root
node is the origin of the tree data structure.
• In any tree, there must be only one root node. We never have
multiple root nodes in a tree.
4
Tree Terminology
Edge
• The connecting link between any two nodes is called as
EDGE. In a tree with 'N‘ number of nodes there will be a
maximum of 'N-1' number of edges.
5
Parent Tree Terminology
• The node which is a predecessor of any node is called as
6
Child Tree Terminology
• The node which is successor (descendant) of any node is called
as Child Node. In simple words, the node which has a link
from its parent node is called as child node.
• In a tree, any parent node can have any number of child
nodes.
7
Siblings Tree Terminology
• The nodes which belong to same Parent are called as
SIBLINGS. In simple words, the nodes with the same parent
are called Sibling nodes.
8
Leaf Tree Terminology
• The node which does not have a child is called as LEAF Node.
In simple words, a leaf is a node with no child.
• In a tree data structure, the leaf nodes are also called as
External Nodes.
• In a tree, leaf node is also called as 'Terminal' node.
9
Internal Nodes Tree Terminology
• The node which has at least one child is called as INTERNAL
Node. Internal nodes are also called as 'Non-Terminal nodes.
10
Degree Tree Terminology
• The total number of children of a node is called as DEGREE of
that Node. In simple words, the Degree of a node is total
number of children it has.
• The highest degree of a node among all the nodes in a tree is
called as 'Degree of Tree'
11
Level Tree Terminology
• The root node is said to be at Level 0 and the children of root
node are at Level 1 and the children of the nodes which are at
Level 1 will be at Level 2 and so on...
• In simple words, in a tree each step from top to bottom is
called as a Level and the Level count starts with '0' and
incremented by one at each level (Step).
12
Tree Terminology
Height
• In a tree data structure, the total number of edges from leaf
node to a particular node in the longest path is called as
HEIGHT of that Node. In a tree, height of the root node is
said to be height of the tree. In a tree, height of all leaf nodes
is '0'.
13
Depth Tree Terminology
• The total number of egdes from root node to a particular node
is called as DEPTH of that Node. In a tree, the total number of
edges from root node to a leaf node in the longest path is said
to be Depth of the tree.
• In simple words, the highest depth of any leaf node in a tree is
said to be depth of that tree. In a tree, depth of the root node
is '0'.
14
Path Tree Terminology
In a tree data structure, the sequence of Nodes and Edges from
one node to another node is called as PATH between that two
Nodes. Length of a Path is total number of nodes in that path.
15
Tree Terminology
Sub Tree
• In a tree data structure, every child node will form a sub tree
on its parent node.
16
17
Tree Terminology
18
Types of Trees
There are three types of trees −
1. General Trees
2. Binary Trees
3. Binary Search Trees
19
1. General Trees
• General trees data structures that don't have limitations
on the number of children each node can have, unlike
binary trees where nodes have at most two children.
• Each node in a general tree can have multiple
children.
• These trees don't necessarily follow a specific order
among their children, so the arrangement can vary
(b) (c)
(a)
20
2. Binary Trees
• Binary Trees are general trees in which the root node can only
hold up to maximum 2 subtrees: left subtree and right subtree.
• A binary tree is a tree data structure in which each parent node
can have utmost two children.
21
Types of Binary Tree
23
Types of Binary Tree
5. 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.
26
Binary Tree Traversals
• When we wanted to display a binary tree, we need to follow
some order in which all the nodes of that binary tree must be
displayed. In any binary tree, displaying order of nodes
Inorder traversal
1. First, visit all the nodes in the left subtree
2.Then the root node
3.Visit all the nodes in the right subtree
1. Search
2. Insertion
3. Deletion
31
BST Operation: Search
In a binary search tree, the search operation is as follows...
• Step 1 - Read the search element from the user.
• Step 2 - Compare the search element with the value of root
node in the tree.
• Step 3 - If both are matched, then display "Given node is
found!!!" and terminate the function
• Step 4 - If both are not matched, then check whether search
element is smaller or larger than that node value.
• Step 5 - If search element is smaller, then continue the
search process in left subtree.
• Step 6- If search element is larger, then continue the
search process in right subtree.
32
BST Operation: Search
• Step 7 - Repeat the same until we find the exact element or
until the search element is compared with the leaf node
• Step 8 - If we reach to the node having the value equal to the
search value then display "Element is found" and terminate
the function. not found" and terminate the function.
• Step 9 - If we reach to the leaf node and if it is also not
matched with the search element, then display "Element is not
found" and terminate the function.
33
BST Operation: Search
34
BST Operation: Insertion
In binary search tree, new node is always inserted as a leaf
node.
The insertion operation is performed as follows...
Step 1 - Create a newNode with given value and set its left and
right to NULL.
Step 2 - Check whether tree is Empty.
Step 3 - If the tree is Empty, then set root to newNode.
Step 4 - If the tree is Not Empty, then check whether the
value of newNode is smaller or larger than the node (here it is
root node).
Step 5 - If newNode is smaller than or equal to the node
then move to its left child. If newNode is larger than the
node then move to its right child. 35
BST Operation: Insertion
Step 6- Repeat the above steps until we reach to the leaf node
(i.e., reaches to NULL).
Step 7 - After reaching the leaf node, insert the newNode as left
child if the newNode is smaller or equal to that leaf node or
else insert it as right child.
36
BST Operation: Deletion
Deleting a node from Binary search tree includes following three
cases...
• Case 1: Deleting a Leaf node (A node with no children)
• Case 2: Deleting a node with one child
• Case 3: Deleting a node with two children
41