Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

Chapter 6

Tree Data Structures

05/06/2024 1
Outline
Definition of Tree Structures
Binary Trees and Binary Search Trees
Basic Tree Operations
Traversing in a Binary Tree
General Trees and Their Implementations

05/06/2024 2
Definition of Tree Structures

A tree structure is a way of representing


the hierarchical nature of a structure in a
graphical form.

Tree is an example of non-linear data


structures.

05/06/2024 3
Tree..

05/06/2024 4
Tree terminologies
The root of a tree is the node with no parents.
There can be at most one root node in a tree
(node A in the above example
An edge refers to the link from parent to child
(all links in the figure).
A node with no children is called leaf node
(E,J,K,H and I).
Children of same parent are called siblings (B,
C, D are siblings of A and E, F are the siblings
05/06/2024 5
Tree …
A node p is an ancestor of a node q if there
exists a path from root to q and p appears on
the path. For example, A, C and G are the
ancestors for K
The node q is called a descendant of p.
Set of all nodes at a given depth is called
level of the tree (B, C and D are same level).
The root node is at level zero.
05/06/2024 6
Tree …

05/06/2024 7
Tree …
The depth of a node is the length of the path from the root to the node

(depth of G is 2, A - C - G).

The height of a node is the length of the path from that node to the

deepest node.

The height of a tree is the length of the path from the root to the

deepest node in the tree.

A (rooted) tree with only one node (the root) has a height of zero.

In the previous example, height of B is 2 (B - F - J).


05/06/2024 8
Tree …
Size of a node is the number of descendants
it has including itself (size of the sub tree C
is 3).
If every node in a tree has only one child
(except leaf nodes) then we call such trees as
skew trees.
If every node has only left child then we call
them as left skew trees.
Similarly, if every node has only right child
then we call them as the right skew trees.
05/06/2024 9
Tree …

05/06/2024 10
Implementation of Tree

The tree data structure can be created by creating the nodes dynamically
with the help of the pointers. The tree in the memory can be represented
as shown below:

The above figure shows the representation of the tree data structure in
the memory. In the above structure, the node contains three fields. The
second field stores the data; the first field stores the address of the left 11
05/06/2024
Binary Trees and Binary Search Trees

A tree is called binary tree if each node


has zero child, one child or two children.
We can visualize a binary tree as
consisting of a root and two disjoint
binary trees, called the left and right
subtrees of the root.

05/06/2024 12
Binary Trees …

05/06/2024 13
Types of Binary Trees
Strict Binary Tree: A binary tree is called
strict binary tree if each node has exactly
two children or no children.

05/06/2024 14
Types of Binary Trees…
Full Binary Tree: A binary tree is called full
binary tree if each node has exactly two
children and all leaf nodes are at same level.

05/06/2024 15
Types of Binary Trees…
Complete binary tree
A binary tree is called complete binary tree if all
leaf nodes are at height h or h - 1 and also
without any missing number in the sequence.

05/06/2024 16
Properties of binary tree

05/06/2024 17
Binary Search Tree
Binary Search Tree is a node-based binary tree data
structure which 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.

05/06/2024 18
Example of BST

05/06/2024 19
BST….
Searching a key
To search a given key in Binary Search Tree,
we first compare it with root, if the key is
present at root, we return root.
If key is greater than root’s key, we recur for
right subtree of root node.
Otherwise we recur for left subtree.

05/06/2024 20
BST…
Illustration to search 6 in above tree:
1. Start from root.
2. Compare the search element with root, if less
than root, then recurse for left, else recurse
for right.
3. If element to search is found anywhere,
return true, else return false.

05/06/2024 21
Implementations

1.int SearchBST (
2.Node *RootNodePtr, int X)
3.{
4.if(RootNodePtr == NULL)
5.return 0; // 0 means (false, not found).
6.else
7.if(RootNodePtr ->Num == X)
8.return 1; // 1 means (true, found).
9.else if(RootNodePtr ->Num > X)
10.return(SearchBST(RootNodePtr ->Left, X));
11.else
12.return(SearchBST(RootNodePtr ->Right, X));
13.}

05/06/2024 22
BST…
Insertion of a key
A new key is always inserted at leaf.
We start searching a key from root till we
hit a leaf node.
Once a leaf node is found, the new node is
added as a child of the leaf node.

05/06/2024 23
illustration

05/06/2024 24
implementation
• void InsertBST( ) • NP->Left = InsNodePtr;
• { • Inserted=1;
• Node *InsNodePtr; • }
• InsNodePtr = new Node; • else
• cout << "Enter The Number" << endl;
• NP = NP->Left;
• cin>>InsNodePtr->Num;
• }
• InsNodePtr->Left=NULL;
• InsNodePtr->Right=NULL; • else {
• if(RootNodePtr== NULL) • if(NP->Right == NULL)
• RootNodePtr=InsNodePtr; • {
• else • NP->Right = InsNodePtr;
• { • Inserted=1;
• Node *NP=RootNodePtr; • }
• int Inserted=0; • else
• while(Inserted ==0)
• NP = NP->Right;
• {
• }
• if(NP->Num > InsNodePtr->Num)
• {
• }
• if(NP->Left == NULL) • }
• { • }

05/06/2024 25
Traversing (Visiting) in a BST
Binary search tree can be traversed in three
ways.
• PreOrder traversal:- traversing binary tree
in the order of parent, left and right.
• InOrder traversal:- traversing binary tree in
the order of left, parent and right.
• PostOrder traversal:- traversing binary tree
in the order of left, right and parent.
Example:
05/06/2024 26
Traversing…

05/06/2024 27
Traversing …
Preorder traversal:
10, 6, 4, 8, 7, 15, 14, 12, 11, 13, 18, 16, 17, 19
Inorder traversal:
4, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19
Used to display nodes in ascending order.
PostOrder traversal:
4, 7, 8, 6, 11, 13, 12, 14, 17, 16, 19, 18, 15, 10

05/06/2024 28
Application of binary tree traversal
Store values on leaf nodes and operators on internal
nodes:

Preorder traversal: - used to generate mathematical

expression in prefix notation.

Inorder traversal: - used to generate mathematical

expression in infix notation.

Postorder traversal: - used to generate mathematical

expression in postfix notation.


05/06/2024 29
Example

05/06/2024 30
Example…
• Preorder traversal: + – A * B C + D / E F ==>
Prefix notation
• Inorder traversal: A – B * C + D + E / F ==> Infix
notation
• Postorder traversal: A B C * – D E F / + + ==>
Postfix notation

05/06/2024 31

Preorder traversal
1. Process the value in the root (e.g. print the root value).
2. Traverse the left subtree with a preorder traversal.
3. Traverse the right subtree with a preorder traversal.
Inorder traversal :-
prints the node values in ascending order:
1. Traverse the left subtree with an inorder traversal.
2. Process the value in the root (e.g. print the root value).
3. Traverse the right subtree with an inorder traversal.
Postorder traversal
1. Traverse the left subtree with a postorder traversal.
2. Traverse the right subtree with a postorder traversal.
3. Process the value in the root (e.g. print the root value).
05/06/2024 32
Basic operations of binary tree

05/06/2024 33
c h- 5
d of
E n
05/06/2024 34
CH=6

Graphs
05/06/2024 35
What is graphs ?
A graph is a pictorial representation of
a set of objects where some pairs of
objects are connected by links.
The interconnected objects are
represented by points termed
as vertices, and
the links that connect the vertices are
called edges.
05/06/2024 36
Graph …
Formally, a graph is a pair of sets (V, E),
where V is the set of vertices and E is the set
of edges, connecting the pairs of vertices.
Take a look at the following graph −

In the graph,
V = {a, b, c, d, e}
E = {ab, ac, bd, cd, de}

05/06/2024 37

You might also like