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

Presentation#1

Design and Analysis of Algorithms

Presented to
Sir Ahmed Bilal
Presented by
Rabia Waseem (005)
Mohammad Zakriya (003)
Eman Malik (024)
TOPIC
TREE DATA STRCUTURE AND ITS TYPES
DEFINITION

Tree is a non-linear data structure which organizes data in a


hierarchical structure 
OR

A tree is a nonlinear data structure that consists of nodes connected by


edges.
OR

If in a graph, there is one and only one path between every pair of
vertices, then graph is called as a tree.
Example
Why Tree Data Structure?

 Data structures such as arrays, linked list, stack, and queue are
linear data structures that store data sequentially.

 In order to perform any operation in a linear data structure, the time


complexity increases with the increase in the data size.

 Tree data structures allow quicker and easier access to the data as it
is a non-linear data structure.
Tree Applications

1.Manipulate hierarchical data.

2. Make information easy to search (see tree traversal).

3. Manipulate sorted lists of data.

4. As a workflow for compositing digital images for visual effects.

5. Router algorithms

6. Most popular databases use B-Trees and T-Trees, which are variants of the tree

structure we learned above to store their data


TREE TERMINOLOGY
Parent & Child
 In other words, the node which has one or more children is called
as a parent node.
 The node which is a descendant of some node is called as a child
node.
Root & Edge
 The first node from where the tree originates is called as a root
node.
 The connecting link between any two nodes is called as an edge. In
a tree with n number of nodes, there are exactly (n-1) number of
edges.
Degree

 Degree of a node is the total number of children of that node.


 Degree of a tree is the highest degree of a node among all the
nodes in the tree.
Leaf Node & Internal Node

The node which does not have any child is called as a leaf node.
The node which has at least one child is called as an internal node.
Level
 In a tree, each step from top to bottom is called as level of a tree.
 The level count starts with 0 and increments by 1 at each level or
step.
Height
 Total number of edges that lies on the longest path from any leaf
node to a particular node is called as height of that node.
 Height of a tree is the height of root node.
 Height of all leaf nodes = 0
Depth
 Total number of edges from root node to a particular node is called as depth of
that node.
 Depth of a tree is the total number of edges from root node to a leaf node in the
longest path.
 Depth of the root node = 0
Types of tree

 Binary Tree

 Binary Search Tree

 AVL Tree

 Spanning tree
Binary Tree

A binary tree is a hierarchical data structure in which each node has at most two

children generally referred as left child and right child.

Each node contains three components:

 Pointer to left sub tree

 Pointer to right sub tree

 Data element
Representation Of Binary Tree
Properties
 Maximum node at any level will be 2^I

 Maximum node at any level will be Height of root node = height of the tree

 Maximum node at height h will be N=2^h+1-1

 Minimum number of node at height will be n=h+1

 Height if maximum and minimum node are given N=2^h+1-1

 Minimum height: H= Log2 (n +1)=h+1

 Maximum height: H=n-1


Types Of Binary Tree

 Full binary tree

 Perfect binary tree

 Complete binary tree

 Degenarate tree
Full Binary Tree
A full binary tree is a unique binary tree where every node except the
external node has two children.
Complete Binary Tree
 A complete binary tree is another specific type of binary tree where
all the tree levels are filled entirely with nodes, except the lowest
level of the tree. 
Perfect Binary Tree
 Binary tree is said to be ‘perfect’ if all the internal nodes have strictly two children,
and every external or leaf node is at the same level or same depth within a tree.
 A perfect binary tree having height ‘h.’
Degenerate Binary Tree

Binary tree is said to be a degenerate binary tree or pathological binary


tree if every internal node has only a single child. Such trees are
similar to a linked list performance-wise.
Binary Search Tree
Binary Search Tree is a binary tree in which every node contains only
smaller values in its left sub tree and only larger values in its right sub
tree.
Example
The following tree is a Binary Search Tree. In this tree, left sub tree of
every node contains nodes with smaller values and right sub tree of
every node contains larger values:
Operations on Binary Search Tree

The following operations are performed on a binary search

tree...

Search

Insertion

Deletion
Search Operation
 Step 1 - Read the search element from the user.

 Step 2 - Compare the search element

 Step 3 - If both are matched, then display "Given node is found!!!"

 Step 4 - If both are not matched

 Step 5 - If search element is smaller, then…

 Step 6- If search element is larger, then

 Step 7 - Repeat the same until we find the exact element

 Step 8 - if it is also not matched with the search element, then display "Element is not found"

and terminate the function.


Insertion Operation
 Step 1 Create a new node with given value

 Step 2 - If the tree is Empty, then set root to new node.

 Step 3 - If the tree is Not Empty, then check whether the value of new node

is smaller or larger than the node

 Step 4 - If new node is smaller than or equal to the node then move to its left child. If new node

is larger than the node then move to its right child.

 Step 5- Repeat the above steps until we reach to the leaf node

 Step 6 - After reaching the leaf node, insert the new Node as left child if the new Node is smaller

or equal to that leaf node or else insert it as right child.


Deletion Operation in BST

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


Example
AVL TREE
 AVL Tree can be defined as height balanced binary search tree in
which each node is associated with a balance factor which is
calculated by subtracting the height of its right sub-tree from that of
its left sub-tree.

 the heights of the two child sub trees of any node differ by at most
one; therefore, it is also said to be height-balanced.

 Lookup, insertion, and deletion all take O(log n) time in both the
average and worst cases, where n is the number of nodes in the tree.
Explaination
 Balance factor = height of left search tree – height of right search
tree

 Balance factor is always in between -1,0,+1

 In Best case, If height of tree is log(n) then time complexity is


O(log(n))

 In worst case, the height of tree is O(n) then the time complexity is
O(n)
Applications

 AVL trees are mostly used for in-memory sorts of sets and
dictionaries.
 AVL trees are also used extensively in database applications in
which insertions and deletions are fewer but there are frequent
lookups for data required.
 It is used in applications that require improved searching apart from
the database applications.
Example#1
Example#2
Example#3
Spanning Tree

 A spanning tree is a subset of an undirected Graph that has all the

vertices connected by minimum number of edges.

 A connected sub graph ‘S’ of graph (V, E) is said to be iff

(1) ‘S’ should contain all vertices of ‘G’

(2) ‘S’ should contain ( |V| - 1 ) edges


Formula
 

The formula of subtree.

Where the n is number of edges


What are spanning tree used for?

 Minimum spanning trees are used for network designs (i.e.


telephone or cable networks).

 They are also used to find approximate solutions for complex


mathematical problems like the Traveling Salesman Problem
Example#1
Continued…
Example#2
Continued

You might also like