0% found this document useful (0 votes)
1 views26 pages

07 Lecture

The document provides an introduction to tree data structures, focusing on terminology, types of binary trees, and traversal methods. It explains the advantages of trees over linear data structures and outlines various applications of tree structures in computing. Key concepts such as node, edge, root, and different types of binary trees are discussed, along with basic operations and traversal techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views26 pages

07 Lecture

The document provides an introduction to tree data structures, focusing on terminology, types of binary trees, and traversal methods. It explains the advantages of trees over linear data structures and outlines various applications of tree structures in computing. Key concepts such as node, edge, root, and different types of binary trees are discussed, along with basic operations and traversal techniques.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Ghazni university

Faculty of Computer Science

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

🞄You can create a forest by cutting the root of a tree.

8
Types of Tree

•Binary Tree

•Binary Search Tree


•AVL Tree
•B-Tree

9
Tree Traversal

•In order to perform any operation on a tree, you need to reach to


the specific node. The tree traversal algorithm helps in visiting
a required node in the tree.
•Traversing a tree means visiting every node in the tree. You might,
for instance, want to add all the values in the tree or find the largest
one. For all these operations, you will need to visit each node of
the tree.
•Linear data structures like arrays, stacks, queues, and linked
list have only one way to read the data. But a hierarchical
data structure like a tree can be traversed in different ways.

10
Types of Traversal

•Depending on the order in which we do this, there can be three


types of traversal.
• Inorder traversal
• Preorder traversal
• Postorder 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

•Full Binary Tree


•Perfect Binary Tree
•Complete Binary Tree
•Balanced Binary Tree
•Degenerate or Pathological 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

•For easy and quick access to data


•In router algorithms
•To implement heap data structure
•Syntax tree

25
End

Would anyone like to ask any questions?

26

You might also like