DS Week 10 Lecture On Tree by DR Gaurav
DS Week 10 Lecture On Tree by DR Gaurav
DS Week 10 Lecture On Tree by DR Gaurav
Week 10 Lecture
by
Dr Gaurav Kumar
Asst. Prof, Bennett University
Quick Recap of Previous Weeks’ Learnings
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, ___
d) front = 2 rear = 4
queue = L, M, N, O, ___
Assessment Time
a) 0
b) 7
c) 9
d) 10
Correct Answer is A
Topics for Visualization
Leaf/Child
Reference Image: https://chercher.tech/images/kotlin/tree-data-structure-kotlin.png
Standard Terminology
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
Implementing Heap
Database Indexing
• 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.
*Note: In the Complete Binary Tree, Last level may or may not full completely.
Types of Binary Tree
F
Types of Binary Tree
left right
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;
};
1
int main()
{
struct Node *root = newNode(1);
2 3 root->left = newNode(2);
root->right = newNode(3);
}
Tree Traversal
Pre-Order Tree Traversal
}
In-Order Tree Traversal
}
Post-Order Tree Traversal
}
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)
For 31 elements, searching 27 in Binary Search Tree takes max 4 For n elements, h= logn
comparisons (11 in Linear Search)
Best Case
Searching the key element present
at the root of the tree
Worst Case
Searching the key element present at
leaf node of a tree