07 Lecture
07 Lecture
Data Structures
Lecture 07
Introduction to Tree Data
Structure
Khalid Ahmad Mubariz 1
Contents
Introduction to tree
Terminology of tree
Binary tree
Binary tree types
Binary Tree Implementation
Traversing the Binary Tree
End
2
What is Tree?
•Atree is a nonlinear hierarchical data structure that consists
of nodes connected by edges.
3
Why Tree Data Structure?
•Other 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. But, it
is not acceptable in today's computational world.
•Different tree data structures allow quicker and easier access to the
data as it is a non-linear data structure.
4
Tree Terminologies
•Node A node is an entity that contains a key or value and pointers
to its child nodes. The last nodes of each path are called leaf nodes
or external nodes that do not contain a link/pointer to child
nodes.
•Edge It is the link between any two nodes.
5
Tree Terminologies
•Root It is the topmost node of a tree.
•Heightof a Node The height of a node is the number of edges
from the node to the deepest leaf (ie. the longest path from
the node to a leaf node).
•Depth of a Node The depth of a node is the number of edges
from
the root to the node.
•Heightof a Tree The height of a Tree is the height of the root
node or the depth of the deepest node.
6
Tree Terminologies
•Height and depth of each node in a tree
7
Tree Terminologies
•Degree of a Node The degree of a node is the total number of
branches of that node.
•ForestA collection of disjoint trees is called a forest.
🞄Creating forest from a tree
8
Types of Tree
•Binary Tree
9
Tree Traversal
10
Types of Traversal
11
Inorder traversal
•First, visit all the nodes in the left subtree
•Then the root node
•Visit all the nodes in the right subtree
🞄Inorder(root->left)
🞄Display(root->data)
🞄Inorder(root->right)
12
Preorder traversal
•Visit root node
•Visit all the nodes in the left subtree
•Visit all the nodes in the right subtree
🞄display(root->data)
🞄preorder(root->left)
🞄preorder(root->right)
13
Postorder traversal
•Visit all the nodes in the left subtree
•Visit all the nodes in the right subtree
•Visit the root node
🞄postorder(root->left)
🞄postorder(root->right)
🞄display(root->data)
14
Tree Applications
•BinarySearch Trees(BSTs) are used to quickly check whether an
element is present in a set or not.
•Heap is a kind of tree that is used for heap sort.
•Amodified version of a tree called Tries is used in modern
routers to store routing information.
•Most popular databases use B-Trees and T-Trees, which are
variants of the tree structure we learned above to store their data
•Compilersuse a syntax tree to validate the syntax of every
program you write.
15
Binary Tree
•Abinary tree is a tree data structure in which each parent node
can have at most two children. For example,
16
Types of Binary Tree
17
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.
18
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.
19
Complete Binary
Tree
•A complete binary tree is just like a full binary tree, but with
two major differences
🞄Every level must be completely filled
🞄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.
20
Balanced Binary
Tree
•It is a type of binary tree in which the difference between the left
and the right subtree for each node is either 0 or 1.
21
Degenerate or Pathological
Tree
•A degenerate or pathological tree is the tree having a single
child either left or right.
22
Binary Tree Representation
•Anode of a binary tree is represented by a class containing a
data part and two objects to other classes of the same type.
🞄Class Node{ int data; Node left; Node right; };
23
Basic Operations
•Following are the basic operations of a tree −
🞄Search − Searches an element in a tree.
🞄Insert − Inserts an element in a tree.
🞄Pre-order Traversal − Traverses a tree in a pre-order manner.
🞄In-order Traversal − Traverses a tree in an in-order manner.
🞄Post-order Traversal − Traverses a tree in a post-order manner.
24
Binary Tree Applications
25
End
26