Download as pdf or txt
Download as pdf or txt
You are on page 1of 34

Data Structures and Algorithms - EE 390

Module 3
Trees

Dr. Anirban Dasgupta


Assistant Professor

Department of Electronics And Electrical Engineering, IIT Guwahati


Tree Data Structure

Tree is a hierarchical data structure

Elements are grouped in levels or


ranks

Department of Electronics And Electrical Engineering, IIT Guwahati


Terminology
Node: fundamental building block of a tree, representing a single element

Root: topmost node serving as the starting point for the tree, having no parent

Parent: node in a tree that has one or more child nodes

Child: node in a tree that has a parent node, situated lower in hierarchy than its parent

Siblings: nodes that share the same parent, at the same level in the tree

Leaf: node in a tree that has no children, at the bottom level of the hierarchy

Depth: level or distance of a node from the root

Height: length of the longest path from the root to a leaf.

Subtree: tree formed by a node and all its descendants

Parent Pointer: reference or link from a child node to its parent node

Department of Electronics And Electrical Engineering, IIT Guwahati


Why Trees?

Trees store information that naturally forms a hierarchy.

Trees with some ordering e.g., Binary Search Trees (BST) provide
moderate access/search (quicker than Linked List and slower than arrays).

Trees provide moderate insertion/deletion (quicker than Arrays and


slower than Unordered Linked Lists).

Like Linked Lists and unlike Arrays, Trees don’t have an upper limit on
number of nodes as nodes are linked using pointers.
Department of Electronics And Electrical Engineering, IIT Guwahati
Tree Data Structure: Applications
Store hierarchical data, like folder structure, XML/HTML data

Indexing and multi-level indexing

Implement searching algorithms

Decision trees

Organization chart of a large organization

Department of Electronics And Electrical Engineering, IIT Guwahati


Binary Tree Data Structure
A tree whose elements have at most 2 children is called a binary tree.
Since each element in a binary tree can have only 2 children, we
typically name them the left and right child.

Department of Electronics And Electrical Engineering, IIT Guwahati


Binary Tree Implementation
A Binary Tree node contains following parts.
1. Data
2. Pointer to left child
3. Pointer to right child

Department of Electronics And Electrical Engineering, IIT Guwahati


Binary Tree Types

Strict Binary Complete


Binary
Trees Almost Complete
Non-binary

Department of Electronics And Electrical Engineering, IIT Guwahati


Binary Tree: Properties

1) The maximum number of nodes at level ‘𝑙’ of a binary tree is 2𝑙.


• Here level is the number of nodes on the path from the root to the
node (including root and node).
• Level of the root is 0.
• This can be proved by induction.
• For root, 𝑙 = 0, number of nodes = 20 = 1
• Assume that the maximum number of nodes on level ‘𝑙’ is 2𝑙
• Since in a Binary tree, every node has at most 2 children, the next level, i.e.,
(𝑙 + 1) would have twice nodes, i.e. 2 × 2𝑙 = 2𝑙+1
Department of Electronics And Electrical Engineering, IIT Guwahati
Binary Tree: Properties
2) The maximum number of nodes in a binary tree of height ‘ℎ’ is 2ℎ – 1.

• Here the height of a tree is the maximum number of nodes on the root
to leaf path.
• Height of a tree with a single node is considered as 1.
• A tree has maximum nodes if all levels have maximum nodes.
• So maximum number of nodes in a binary tree of height ℎ is 1 +
2 + 4 + . . . + 2ℎ. This is a simple geometric series with ℎ terms and
sum of this series is 2ℎ– 1.
Department of Electronics And Electrical Engineering, IIT Guwahati
Binary Tree: Properties
3) In a Binary Tree with 𝑁 nodes, minimum possible height
or the minimum number of levels is log 2(𝑁 + 1)

• We already have maximum, 𝑁 = 2ℎ – 1


• Here the height, ℎ of a tree is the maximum number of nodes on the
root to leaf path.
• If we consider the convention where the height of a root node is
considered as 0, then above formula for minimum possible height
becomes log 2(𝑁 + 1)
Department of Electronics And Electrical Engineering, IIT Guwahati
Strict Binary Trees

Strict Binary Trees


• have either zero two
children per node
• also known as a full binary
tree or proper binary tree
• cannot have only one child

Department of Electronics And Electrical Engineering, IIT Guwahati


Almost Complete Binary Trees (ACBT)

ACBT completes a level from left to right before starting next level

Department of Electronics And Electrical Engineering, IIT Guwahati


Complete Binary Trees (CBT)
• CBT has all complete levels
• Levels completed from left to right before starting next level
• Also called full binary trees

Department of Electronics And Electrical Engineering, IIT Guwahati


Binary Search Trees
Binary Search Tree is a node-based binary tree data structure which has the
following properties:

• The left subtree of a node contains only nodes


with keys lesser than the node’s key.
• The right subtree of a node contains only nodes
with keys greater than the node’s key.
• The left and right subtree each must also be a
binary search tree.
• There must be no duplicate nodes.

Department of Electronics And Electrical Engineering, IIT Guwahati


Binary Search Trees: Searching
Start from the root.

Compare the searching element with root, if


less than root, then recursively call left
subtree, else recursively call right subtree.

If the element to search is found anywhere,


return true, else return false.
O(log2n)
Department of Electronics And Electrical Engineering, IIT Guwahati
Binary Search Trees: Insertion
Start from the root.

Compare the inserting element with root, if


less than root, then recursively call left
subtree, else recursively call right subtree.

A new key is always inserted at the leaf.


After reaching the end, just insert that node at
left (if less than current) else right.
𝑂(ℎ), ℎ is height of the tree

Department of Electronics And Electrical Engineering, IIT Guwahati


Binary Search Trees: Deletion
When we delete a node, three
possibilities arise

Leaf Only one child Two children

Find inorder successor of the


Copy the child to the node and node. Copy contents of the
Simply remove from the tree
delete the child inorder successor to the node
and delete the inorder successor.

Department of Electronics And Electrical Engineering, IIT Guwahati


Heap Trees
Two
Properties

Structural Ordering

Structural: Must be ACBT Ordering:


• Complete one level before going to next • Max Heap: Parent>Child
• Insert left child first before right child • Min Heap: Parent<Child

Department of Electronics And Electrical Engineering, IIT Guwahati


Max and Min Heap Trees

10 2

8 7 3 4

6 4 3 2 6 7 10 8

Max Heap Min Heap

Department of Electronics And Electrical Engineering, IIT Guwahati


Constructing Heap Trees

Insert one by one, 14


𝑂(𝑛 log 𝑛)
Methods
Heapify, 𝑂(𝑛) 24
24
Insert one by one
Max Heap
14 12
14, 24, 12, 11, 25, 8, 35

Department of Electronics And Electrical Engineering, IIT Guwahati


Heapify
14, 24, 12, 11, 25, 8, 35 14

Max Heap
Insert every element as it is
24 12

11 25 8 35

For N elements, N/2 are leaf elements

Department of Electronics And Electrical Engineering, IIT Guwahati


Heapify
14, 24, 12, 11, 25, 8, 35
14 Max Heap 14

Compare 2nd last level


24 12 25 35

11 25 8 35 11 24 8 12

Department of Electronics And Electrical Engineering, IIT Guwahati


Heapify
14, 24, 12, 11, 25, 8, 35
14 Max Heap 35

Compare 3rd last level


25 35 25 14

11 24 8 12 11 24 8 12

Department of Electronics And Electrical Engineering, IIT Guwahati


Deletion in Heap Trees
Delete either root, or rightmost leaf

35

25 14

11 24 8 12

Department of Electronics And Electrical Engineering, IIT Guwahati


Tree Traversal
x

• Preorder: Root Left Right (xyz) 25


• Inorder: Left Root Right (yxz)
• Postorder: Left Right Root (yzx) y z

11 24

• Preorder: 25, 11, 8, 12, 24


• Inorder: 8, 11, 12, 25, 24
• Postorder: 8, 12, 11, 24, 25 8 12

Department of Electronics And Electrical Engineering, IIT Guwahati


AVL Tree
• AVL Tree invented by GM Adelson-Velsky and EM Landis
• AVL Tree can be defined as height balanced binary search tree (BST)

7,8,9 Balance Factor (k) = height (left(k)) - height (right(k))

7
8

BST behaves 8
like linked list 7 9

9
BST depends on the height of the tree and skewed structure is the
worst case leads to O(n) time complexity
Department of Electronics And Electrical Engineering, IIT Guwahati
AVL Tree
• Tree is said to be balanced if balance factor of each
node is in between -1 to 1, otherwise, the tree will be
unbalanced and need to be balanced.
• If balance factor of any node is 1, it means that the
left sub-tree is one level higher than the right sub-
tree.
• If balance factor of any node is 0, it means that the
left sub-tree and right sub-tree contain equal height.
• If balance factor of any node is -1, it means that the
left sub-tree is one level lower than the right sub-tree.
• The node whose balance factor doesn't lie between -1
and 1, is called critical node.

Department of Electronics And Electrical Engineering, IIT Guwahati


AVL Rotations
• perform rotation in AVL tree only if Balance Factor is other than -1, 0, and 1
• four types of rotations which are as follows:
• L L (right) rotation: Inserted node is in the left subtree of left subtree
• R R (left) rotation : Inserted node is in the right subtree of right subtree
• L R rotation : Inserted node is in the right subtree of left subtree
• R L rotation : Inserted node is in the left subtree of right subtree
• first two rotations LL and RR are single rotations
• next two rotations LR and RL are double rotations

Department of Electronics And Electrical Engineering, IIT Guwahati


RR Rotation

Department of Electronics And Electrical Engineering, IIT Guwahati


LL Rotation

Department of Electronics And Electrical Engineering, IIT Guwahati


RL and LR Rotation

• RL rotation = LL rotation + RR rotation


• Convert to RR

Department of Electronics And Electrical Engineering, IIT Guwahati


LR Rotation

• LR rotation = RR rotation + LL rotation


• Convert to LL

Department of Electronics And Electrical Engineering, IIT Guwahati


References
• Cormen, Leiserson, Rivest, and Stein, Introduction to Algorithms, 3rd
edition, The MIT Press, McGraw-Hill, 2001.
• C. Cherniavsky and J. A. Storer, An Introduction to Data Structures and
Algorithms, 2nd edition. Birkhauser Boston, 2001.
• Javapoint
• GeeksforGeeks

Department of Electronics And Electrical Engineering, IIT Guwahati

You might also like