0% found this document useful (0 votes)
2 views41 pages

Chapter 7 - Tree DS

Chapter 7 discusses tree data structures, which are hierarchical and consist of nodes connected by edges. It covers key terminology such as root nodes, edges, parent and child nodes, as well as various types of trees including binary trees and binary search trees. Additionally, the chapter outlines operations on binary search trees, including search, insertion, and deletion, along with their applications in fields like file systems and artificial intelligence.

Uploaded by

yabt832
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)
2 views41 pages

Chapter 7 - Tree DS

Chapter 7 discusses tree data structures, which are hierarchical and consist of nodes connected by edges. It covers key terminology such as root nodes, edges, parent and child nodes, as well as various types of trees including binary trees and binary search trees. Additionally, the chapter outlines operations on binary search trees, including search, insertion, and deletion, along with their applications in fields like file systems and artificial intelligence.

Uploaded by

yabt832
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/ 41

CHAPTER 7

Tree Data Structure

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

Parent Node. In simple words, the node which has a branch

from it to any other node is called a parent node.

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.

• In a tree, all the nodes except root are 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.

In below example the path A - B - E - J has length 4.

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

1. Full Binary Tree


• A full Binary tree is a special type of
binary tree in which every parent
node/internal node has either two or
no children.

2. Perfect Binary Tree


• 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.
22
Types of Binary Tree
3. Complete Binary Tree
A complete binary tree is just like a full
binary tree, but with the major differences
❖ 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
doesn't have to be a full binary tree.

4. Degenerate or Pathological Tree


A degenerate or pathological tree is
the tree having a single child either
left or right

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.

6. Balanced Binary Tree


• It is a type of binary tree in which the
difference between the height of the
left and the right subtree for each node
is either 0 or 1.
24
25
3. Binary Search Trees
• Binary Search Trees possess all the properties of Binary Trees
including some extra properties of their own.
• The data in the Binary Search Trees (BST) is always stored in
such a way that the values in the left subtree are always less
than the values in the root node and the values in the right
subtree are always greater than the values in the root node,

i.e. left subtree < root node ≤ right subtree.

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

depends on the traversal method.

Displaying (or) visiting order of nodes in a binary tree is called


as Binary TreeTraversal.

There are three types of binary tree traversals.


1.In - Order Traversal
2. Pre - Order Traversal
3.Post - Order Traversal
27
In - Order Traversal (leftChild - root - rightChild)
• Traverse the left sub-tree first, and then traverse the root and
the right sub-tree respectively. This procedure will be applied
to each sub-tree of the tree recursively.

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

Output In-Order Traversal


I - D - J- B - F - A - G - K - C - H 28
Pre - Order Traversal (root - leftChild - rightChild)
Traverse the root first then traverse into the left sub-tree and
right sub-tree respectively. This procedure will be applied to
each sub-tree of the tree recursively.

1. Visit root node


2. Visit all the nodes in the left subtree
3. Visit all the nodes in the right subtree

Output Pre-Order Traversal


A – B – D – I – J - F - C- G – K - H 29
Post - Order Traversal ( leftChild - rightChild - root )
Traverse the left sub-tree and then traverse the right sub-tree
and root respectively.This procedure will be applied to each
sub-tree of the tree recursively.

1. visit all the nodes in the left subtree

2. visit all the nodes in the right subtree

3. visit the root node

Output Post - Order Traversal


I - J- D - F - B - K - G - H - C - A 30
Operations on a Binary Search Tree(BST)

The following operations are performed on a


binary search tree...

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

Case 1: Deleting a leaf node


We use the following steps to delete a
leaf node from BST...
• Step 1 - Find the node to be
deleted using search operation
• Step 2 - Delete the node using free
function (If it is a leaf) and
terminate the function.
37
BST Operation: Deletion
Case 2: Deleting a node with one child

We use the following steps to


delete a node with one child
from BST...
• Step 1 - Find the node to be
deleted using search
operation
• Step 2 - If it has only one
child then create a link
between its parent node and
child node.
• Step 3 - Delete the node
using free function and
terminate the function. 38
Case 3: Deleting a node with two children
We use the following steps to delete a node with two
children from BST...
• Step 1 - Find the node to be deleted using search
operation.
• Step 2 - If it has two children, then find the largest
node in its left subtree (OR) the smallest node in its
right subtree.
• Step 3 - Swap both deleting node and node which is
found in the above step.
• Step 4 - Then check whether deleting node came to case 1
or case 2 or else goto step 2.
• Step 5 - If it comes to case 1, then delete using case 1 logic.
• Step 6- If it comes to case 2, then delete using case 2 logic.
• Step 7 - Repeat the same process until the node is deleted
from the tree. 39
40
Applications of Tree:
• File Systems: Each folder or directory is a node in the tree,
and files are the leaves.
• Database Indexing: Many databases use trees to index their
data.
• Compiler Design: The syntax of programming languages is
often defined using a tree structure called a parse tree. This is
used by compilers to understand the structure of the code and
generate machine code from it.
• Artificial Intelligence: Decision trees are often used in
artificial intelligence to make decisions based on a series of
criteria.
• Trees are used in several games like moves in chess.

41

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