0% found this document useful (0 votes)
0 views4 pages

Binary Tree

A Binary Tree is a hierarchical data structure where each node has at most two children. It can be classified into various types such as Full, Complete, Perfect, and Binary Search Trees, each with distinct properties. Binary Trees can be represented using arrays or linked lists, with each method having its advantages and disadvantages in terms of memory usage, ease of implementation, and access speed.
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)
0 views4 pages

Binary Tree

A Binary Tree is a hierarchical data structure where each node has at most two children. It can be classified into various types such as Full, Complete, Perfect, and Binary Search Trees, each with distinct properties. Binary Trees can be represented using arrays or linked lists, with each method having its advantages and disadvantages in terms of memory usage, ease of implementation, and access speed.
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/ 4

Binary tree

A Binary Tree non linear and hierarchical data structure in which each node has at most two children, referred to as the left child
and the right child.
Representation of Binary Tree
Each node in a Binary Tree has three parts:
• Data
• Pointer to the left child
• Pointer to the right child

Types of Binary Tree


Binary Tree can be classified into multiples types based on multiple factors:
• On the basis of Number of Children
o Full Binary Tree
A full binary tree is a binary tree with either zero or two child nodes for each node.

o Degenerate Binary Tree(Skewed Binary Trees)


Every non-leaf node has just one child in a binary tree known as a Degenerate Binary tree
• On the basis of Completion of Levels
o Complete Binary Tree
• A Complete Binary Tree is a binary tree where:
• All levels are fully filled,
except possibly the last level, which is filled from left to right.

o Perfect Binary Tree


A Perfect Binary Tree is a binary tree where:

1. All internal nodes have exactly 2 children

2. All leaf nodes are at the same level


o Balanced
Binary Tree
It stays nearly
equal in both left
and right height
of subtrees the
difference is O.

• On the basis of Node Values:


o Binary Search Tree
A Binary Search Tree is a special kind of binary tree that follows this rule:
For every node:
• All values in the left subtree are less than the node
• All values in the right subtree are greater than the node

o AVL Tree

• An AVL Tree is a special kind of Binary Search Tree (BST) that is always balanced.
• It is named after its inventors: Adelson-Velsky and Landis
It automatically balances itself after insert or delete
• The balance factor must be -1,0,1

Balance Factor=Height of Left Subtree−Height of Right Subtree


Properties of Binary Tree
• The maximum number of nodes at level L of a binary tree is 2L
Level 0 (root): 2⁰ = 1 node
Level 1: 2¹ = 2 nodes
Level 2: 2² = 4 nodes
Level 3: 2³ = 8 nodes

• The maximum number of nodes in a binary tree of height H is 2H – 1


Example:
Height H = 1:
Max nodes = 2¹ − 1 = 1
A
Height H = 2: Max nodes = 2² − 1 = 3
A
/\
B C
Height H = 3: Max nodes = 2³ − 1 = 7
A
/\
B C
/\/\
D EF G
Height H = 4:Max nodes = 2⁴ − 1 = 15

• Total number of leaf nodes in a binary tree = total number of nodes with 2 children + 1

Let’s now take a full binary tree:


A
/\
B C
/\/\
D EF G
Now check:
Nodes with 2 children: A, B, C → 3 nodes
Leaf nodes: D, E, F, G → 4 nodes
Apply the formula:
Leaf nodes = Full nodes + 1
=3+1
=4

• In a Binary Tree with N nodes, the minimum possible height or the minimum number of levels is Log2(N+1)

➤ If N = 7 nodes:
log₂(7 + 1) = log₂(8) = 3
So, minimum height = 3

Binary tree representation:

Binary tree representation refers to the way we


store and manage a binary tree in memory
using a programming structure.

Array representation of binary trees

Array representation of a binary tree is a


method of storing a binary tree in a sequential
array (or list) using level order traversal,
where:

• The root node is placed at index 1 (or


0)
• For any node at index i:
o Its left child is at index 2i
o Its right child is at index 2i + 1
10
/ \
20 30
/ \ /
40 50 60

• Node at index 1 = 10
o Left → 2×1 = 2 → 20
o Right → 2×1+1 = 3 → 30
• Node at index 2 = 20
o Left → 2×2 = 4 → 40
o Right → 2×2+1 = 5 → 50
• Node at index 3 = 30
o Left → 2×3 = 6 → 60
o Right → 2×3+1 = 7 → Not present
It fits all rules
Linked List representation:
Linked list representation of a binary tree is a method in which each
node is represented as a structure or class containing:
• The data
• A pointer to the left child
• A pointer to the right child

Feature Array Representation Linked List Representation

Structure type Sequential (uses index) Non-sequential (uses pointers)

Node connection Based on index formulas Using left and right pointers

Best suited for Complete binary trees Any type of binary tree
Memory usage May waste memory in skewed trees Memory efficient, no wasted space

Ease of implementation Simple indexing logic Needs dynamic memory and pointers

Insertion/Deletion Complex and slow (due to shifting) Easy and flexible


Access speed Faster (direct index access) Slower (follow pointers)

Tree size Fixed or needs resizing Dynamic (grows as needed)

Used in Heaps, Priority Queues General binary trees, recursive processing

You might also like