Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DS Week 10 Lecture On Tree by DR Gaurav

Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

Subject: Data Structure

Using C++ (CSET243)

Week 10 Lecture

Tree Data Structure


What, Why and How?

by

Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Previous Weeks’ Learnings

• Understood the Concept of Structure and Pointers

• Discussed the Usage of Linked List and Its Applications

• Performed different operations on Linked List and its


Implementation

• Polynomial Equation Representation using Linked List

• Understood the Different Types of Linked List


Assessment Time

Q: If the elements “A”, “B”, “C” and “D” are placed in a


queue and are deleted one at a time, in what order will they
be removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC

Output: A
Assessment Time

Q: If the elements “A”, “B”, “C” and “D” are placed in a Stack
and are deleted one at a time, in what order will they be
removed?
a) ABCD
b) DCBA
c) DCAB
d) ABDC

Output: B
Assessment Time
Let the following queue can accommodate maximum six elements with the following
data
front = 2 rear = 4
queue = _______; L, M, N, ___, ___
What will happen after ADD O operation takes place?

a) front = 2 rear = 5
queue = ______; L, M, N, O, ___

b) front = 3 rear = 5
queue = L, M, N, O, ___

Correct Answer is B c) front = 3 rear = 4


queue = ______; L, M, N, O, ___

d) front = 2 rear = 4
queue = L, M, N, O, ___
Assessment Time

Q: A circular queue is implemented using an array of size 10. The array


index starts with 0, front is 6, and rear is 9. The insertion of next element
takes place at the array index.

a) 0
b) 7
c) 9
d) 10

Correct Answer is A
Topics for Visualization

Understanding Different Trees with Real Life Applications


What and Why (Problem with Linear DS (Scope & Improvement))

Operations and Analysis


How
Problems with Linear Data Structure

Problem with Linear Data Structure

Won’t be able to tell the relationship between


elements
Problems with Linear Data Structure
How will you store the academic relationship between people working in an institution?

Reference Image: www.hlcollege.edu


Understanding Tree Data Structure

Non-Linear Data Structure to


represent and organize data.
Root/Parent (Hierarchical relationship between
the nodes)

Leaf/Child
Reference Image: https://chercher.tech/images/kotlin/tree-data-structure-kotlin.png
Standard Terminology

• Parent Node: Predecessor of a node

• Child Node: Successor of a node

• Root Node: Topmost node of a tree

• Internal node: Other than leaf nodes

• Sibling:Children of the same parent node are called siblings

• Neighbour of a Node: Parent or child nodes of that node

• Subtree: Any node of the tree along with its descendant


Standard Terminology
• Level of a node: The count of edges on the path from
the root node to that node. The root node has level 0.

• Leaf Node or External Node or Terminal Node:


Nodes which do not have any child nodes

• Ancestor of a Node:Any predecessor nodes on the path


of the root to that node are called Ancestors of that node

• Descendant: Any successor node on the path from the


leaf node to that node
Standard Terminology
Number of edges:
Connection between two nodes. N nodes then it will
have (N-1) edges

Depth of a node: The length of the path from the


root to that node. Each edge adds 1 unit of length to
the path

Height of a node: The length of the longest path from


the node to a leaf node

Height/Depth of the Tree: Longest path from the


node to a leaf node

Degree of a Node: Total count of child node attached


to that node is called the degree of the node
Real Life Applications of Tree

File Systems
Directory structure used to organize subdirectories and files

Organization Structure
Real Life Applications of Tree

Traversing
(Document Object Model (DOM) in HTML)
Real Life Applications of Tree

Decision Making
Real Life Applications of Tree

Natural Language Processing


Parsing Tree (Syntax Tree)
An ordered, rooted tree that
Dialogue tree (conversation tree)
represents the syntactic structure
of a string according to some
A gameplay mechanic that is used
context-free grammar.
throughout many adventure games including
action-adventure games and role-playing
video games.
Real Life Applications of Tree

Hierarchical clustering Genetic Programming

Implementing Heap

Database Indexing

Implement Efficient Searching


and Sorting Algorithms
Understanding Trees

Tree Binary Tree

Degree of Tree =3 Degree of Tree =2


Ref GIF Image: www.penjee.com
Characteristics of Binary Tree
• The maximum number of nodes at level ‘l’ of a binary tree is 2l.

• The Maximum number of nodes in a binary tree of height ‘h’ is 2h – 1.

• In a Binary Tree with N nodes, 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.

• In Binary tree where every node has 0 or 2 children, the number of leaf
nodes is always one more than nodes with two children.

• In a non-empty binary tree, if n is the total number of nodes and e is the


total number of edges, then e = n-1

• In a Perfect Binary Tree, the number of leaf nodes is the number of


internal nodes plus 1
Types of Binary Tree
Types of 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.

In a Perfect Binary Tree, the number of leaf nodes is the


number of internal nodes plus 1. (L=I+1, where I is the total
number of nodes and L is the number of leaf nodes.)

A Perfect Binary Tree of height h (where the height of the


binary tree is the number of edges in the longest path from
Perfect/Complete Binary Tree* the root node to any leaf node in the tree, height of root node
is 0) has 2h+1 – 1 node.

*Note: In the Complete Binary Tree, Last level may or may not full completely.
Types of Binary Tree

A almost complete binary tree is a special type of binary


tree where all the levels of the tree are filled completely
except the lowest level nodes which are filled from as
left as possible.

It can be represented using an array. If the parent is it


index i so the left child is at 2i+1 and the right child is
at 2i+2.
Almost Complete Binary Tree

F
Types of 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.

Full/Strict Binary Tree


(Not Almost Complete Binary Tree)
Types of Binary Tree

A degenerate or pathological tree is the tree having a


single child either left or right.

Degenerate or Pathological Tree


Types of 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.

left right

Skewed Binary Tree


Tree Representation
Tree Representation

struct node
{
int data;
struct node *left;
struct node *right;
};
Tree Representation
#include <stdlib.h>
#include <iostream>
using namespace std;

struct Node
{
int data;
struct Node *left;
struct Node *right;
};

// New node creation

struct Node *newNode (int data)


{
struct Node *node = new Node;
node->data = data;
node->left = NULL;
node->right = NULL;
return (node);
}
Tree Representation

1
int main()
{
struct Node *root = newNode(1);
2 3 root->left = newNode(2);
root->right = newNode(3);
}
Tree Traversal
Pre-Order Tree Traversal

Pre-Order Traversal ( Root-Left-Right)

1.Traverse root node (Visit)

2.Traverse left subtree

3.Traverse right subtree

Pre-order (node visited at position red ●): F, B, A, D, C, E, G, I, H


Assessment Time

Pre-Order Traversal Output:


(A) 1 5 12 6 9
(B) 1 9 12 5 6
(C) 1 12 6 5 9
(D) 1 12 5 6 9
Correct Answer is D
Pre-Order Tree Traversal
// Preorder traversal

Pre-Order Traversal ( Root-Left-Right) void preorderTraversal(struct Node* node)


{
1.Traverse root node (Visit) if (node == NULL)

2.Traverse left subtree return;


cout << node->data << "->";
3.Traverse right subtree preorderTraversal(node->left);
preorderTraversal(node->right);

}
In-Order Tree Traversal

In-Order Traversal (Left-Root-Right)

1.Traverse left subtree

2. Traverse root node (Visit)

3.Traverse right subtree

In-order (node visited at position green ●): A, B, C, D, E, F, G, H, I;


Assessment Time

In-Order Traversal Output:


(A) 12 5 6 1 9
(B) 5 12 6 1 9
(C) 5 6 12 9 1
(D) 1 12 5 6 9
Correct Answer is B
In-Order Tree Traversal
// Preorder traversal

In-Order Traversal ( Root-Left-Right) void InorderTraversal(struct Node* node)


{
1.Traverse left subtree if (node == NULL)

2.Traverse root node (Visit) return;


InorderTraversal(node->left);
3.Traverse right subtree cout << node->data << "->";
InorderTraversal(node->right);

}
Post-Order Tree Traversal

Post-Order Traversal (Left-Right-Root)

1.Traverse left subtree

2.Traverse right subtree

3.Traverse root node (Visit)

Post-order (node visited at position blue ●): A, C, E, D, B, H, I, G, F.


Assessment Time

Post-Order Traversal Output:


(A) 5 12 6 1 9
(B) 5 6 12 1 9
(C) 5 6 12 9 1
(D) 6 5 12 9 1
Correct Answer is B
Post-Order Tree Traversal
// Postorder traversal

Post-Order Traversal ( Left-Right-Root) void postorderTraversal(struct Node* node)


{
1.Traverse left subtree if (node == NULL)

2.Traverse right subtree return;


postorderTraversal(node->left);
3.Traverse root node (Visit) postorderTraversal(node->right);
cout << node->data << "->";

}
Time Complexity Analysis
Time Complexity:
In-order: O(n)
Pre-order: O(n)
Post-order: O(n)

Space Complexity
In-order: O(n)
Pre-order: O(n)
Post-order: O(n)

Data Structure: Stack


Scope of Improvement

Searching element in a Binary Tree takes O(n) Time


(Worst Case) In-order: O(n)/Pre-order: O(n)/Post-order:
O(n)

Can we improve the time?


Scope of Improvement
Search 49 in a given Tree Search 27 in BST

O(n) times Can we improve the


searching time??

For 11 elements, max 11 number of


comparison for Searching 49 element in For 31 elements, searching 27 in Binary Search

Binary Tree. Tree takes max 4 comparisons (11 in LS)

Ref GIF Image: www.penjee.com


Assumption: Elements should be in sorted order
Analyzing Binary Search Trees
Search 27 Searching an element in Binary Search Tree takes
average logn comparisons.

Height of the Total number of


Tree(h)
Comparisons
depend on the
hight of the Tree

For 31 elements, searching 27 in Binary Search Tree takes max 4 For n elements, h= logn
comparisons (11 in Linear Search)

Assumption: Elements should be in sorted order


Ref GIF Image: www.penjee.com
Binary Search Trees

Best Case
Searching the key element present
at the root of the tree

Best Case Time Complexity


Key Element = 10
Total Number of Comparision = 1

b(n) = 1 = Constant b(n) = O(1)


Binary Search Trees

Worst Case
Searching the key element present at
leaf node of a tree

Worst Case Time Complexity


Key Element = 8
Total Number of Comparision = 3
i.e. log 8 =3

for n elements w(n) = logn w(n) = O(logn)


Any Queries?
Office MCub311
Discussion Time: 3-5 PM
Mob: +91-8586968801

You might also like