0% found this document useful (0 votes)
6 views9 pages

Tree Types

Uploaded by

puravbeniwal4
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)
6 views9 pages

Tree Types

Uploaded by

puravbeniwal4
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/ 9

LECTURE NOTES

on

DATA STRUCTURES
2021 – 2022

B.Tech, IIIrd Sem e s t e r


Mr. Deepanshu Singh Yadav, Assistant Profes so r

Sitapur Road,Lucknow
Uttar Pradesh
India
Pin Code: 226021

INSTITUTE OF ENGINEERING
&TECHNOLOGY

Department of Computer Science and Engineering

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 1


REFERENCES:
1. Aaron M. Tenenbaum, YedidyahLangsam and Moshe J. Augenstein “Data Structures Using C
and C++”, PHI Learning Private Limited, Delhi India.
2. Horowitz and Sahani, “Fundamentals of Data Structures”, Galgotia Publications Pvt Ltd Delhi
India.
3. A.K. Sharma ,Data Structure Using C, Pearson Education India.
4. Rajesh K. Shukla, “Data Structure Using C and C++” Wiley Dreamtech Publication
5. Lipschutz, “Data Structures” Schaum’s Outline Series, Tata Mcgraw-hill Education (India) Pvt.
Ltd
6. Michael T. Goodrich, Roberto Tamassia, David M. Mount “Data Structures and Algorithms in
C++”, Wiley India.
7. P.S. Deshpandey, “C and Datastructure”, Wiley Dreamtech Publication.
8. R. Kruse etal, “Data Structures and Program Design in C”, Pearson Education.
9. Berztiss, A.T.: Data structures, Theory and Practice :, Academic Press.
10. codeforwin.org/
11. www.programiz.com/
12. Tutorials point

Disclaimer: The e-content is exclusively meant for academic purposes and for enhancing teaching
and learning. Any other use for economic/commercial purpose is strictly prohibited. The users of the
content shall not distribute, disseminate or share it with anyone else and its use is restricted to
advancement of individual knowledge.
The information provided in this e-content is developed from authentic references, to the best of my
knowledge

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 2


Binary Trees
A binary tree is a tree data structure in which each parent node can have at most two children.
Each node of a binary tree consists of three items:
 data item
 address of left child
 address of right child

Binary Tree

As you can see in the picture given above, a node can have less than 2 children but not more
than that

TYPES OF BINARY TREE


Full Binary Tree → A binary tree in which every node has 2 children except the leaves is
known as a full binary tree.
In other words, A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 3


Complete Binary Tree →
 A binary tree which is completely filled with a possible exception at the bottom level
i.e., the last level may not be completely filled and the bottom level is filled from left
to right.
 A complete binary tree is just like a full binary tree, but with two major difference
 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.

Complete Binary Tree

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 4


Let's look at this picture to understand the difference between a full and a complete binary
tree.

A complete binary tree also holds some important properties. So, let's look at them.
The parent of node i is ⌊i2⌋⌊i2⌋. For example, the parent of node 4 is 2 and the parent of node
5 is also 2.
The left child of node i is 2i2i.
The right child of node i is 2i+12i+1

Perfect Binary Tree → In a perfect binary tree, each leaf is at the same level and all the
interior nodes have two children.
In other words, 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.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 5


Skewed 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.

Skewed Binary Tree

Balanced Binary Tree


It is a type of binary tree in which the difference between the height of the left and the right
subtree for each node is either 0 or 1.

Balanced Binary Tree

Thus, a perfect binary tree will have the maximum number of nodes for all alternative binary
trees of the same height and it will be 2h+1−12h+1−1 which we are going to prove next.

Array Representation of Binary Tree


We start by numbering the nodes of the tree from 1 to n(number of nodes).

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 6


As you can see, we have numbered from top to bottom and left to right for the same level.
Now, these numbers represent the indices of an array (starting from 1) as shown in the picture
given below.

We can also get the parent, the right child and the left child using the properties of a complete
binary tree we have discussed above i.e., for a node i, the parent is ⌊i2⌋⌊i2⌋, the left child
is 2i2i and the right child is 2i+12i+1.

So, we represented a complete binary tree using an array and saw how to get the parent and
children of any node. Let's discuss about doing the same for an incomplete binary tree.
Array Representation of Incomplete Binary Tree
To represent an incomplete binary tree with an array, we first assume that all the nodes are
present to make it a complete binary tree and then number the nodes as shown in the picture
given below.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 7


Now according to these numbering, we fill up the array.

Representation of Binary Tree using Array


Binary tree using array represents a node which is numbered sequentially level by level from
left to right. Even empty nodes are numbered.

Array index is a value in tree nodes and array value gives to the parent node of that particular
index or node. Value of the root node index is always -1 as there is no parent for root. When
the data item of the tree is sorted in an array, the number appearing against the node will
work as indexes of the node in an array.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 8


Location number of an array is used to store the size of the tree. The first index of an array
that is '0', stores the total number of nodes. All nodes are numbered from left to right level by
level from top to bottom. In a tree, each node having an index i is put into the array as its i th
element.

The above figure shows how a binary tree is represented as an array. Value '7' is the total
number of nodes. If any node does not have any of its child, null value is stored at the
corresponding index of the array.

Mr. Deepanshu Singh Yadav, Assistant Professor, IET, LUCKNOW Page 9

You might also like