0% found this document useful (0 votes)
58 views

07 Trees

The document discusses different types of tree data structures, including their properties and applications. It covers binary trees, binary search trees, and heap trees. Key points include: - Trees have hierarchical structures with root, child, and parent nodes - Binary trees restrict each node to at most two children - Binary search trees organize nodes to keep left subtrees less than parent nodes and right subtrees greater than parent nodes - Heap trees are almost complete binary trees that satisfy ordering properties for max-heap or min-heap implementations - Common tree operations like search, insertion, and deletion have different algorithms and time complexities depending on the tree type.

Uploaded by

Kotla Nishanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views

07 Trees

The document discusses different types of tree data structures, including their properties and applications. It covers binary trees, binary search trees, and heap trees. Key points include: - Trees have hierarchical structures with root, child, and parent nodes - Binary trees restrict each node to at most two children - Binary search trees organize nodes to keep left subtrees less than parent nodes and right subtrees greater than parent nodes - Heap trees are almost complete binary trees that satisfy ordering properties for max-heap or min-heap implementations - Common tree operations like search, insertion, and deletion have different algorithms and time complexities depending on the tree type.

Uploaded by

Kotla Nishanth
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

Data Structures and Algorithms - EE 390

Chapter 6
Trees

Dr. Anirban Dasgupta


Assistant Professor

Department of Electronics And Electrical Engineering, IIT Guwahati


Tree Data Structure: Nomenclature
Tree is a hierarchical data structure

The topmost node is called root of the tree.


The elements that are directly under an
element are called its children.
The element directly above something is
called its parent.
Elements with no children are called leaves.
Nodes which are neither leaf or root are called
internal nodes.
Department of Electronics And Electrical Engineering, IIT Guwahati
Why Trees?
One reason to use trees might be because you want to 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
Binary Trees (BT) have at-most two
Tree data children per node
structure
Children of BT are termed left and
Non- right
Binary
binary
Strict Binary Trees have either zero
two children per node
Strict/Full Almost
Binary Complete
Almost Complete BT (ACBT)
completes a level from left to right
before starting next level
Complete
Complete BT (CBT) completes all
levels

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.

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: Properties

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


• 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, l = 0, number of nodes = 20 = 1
• Assume that the maximum number of nodes on level ‘l’ is 2 l
• Since in Binary tree every node has at most 2 children, next level would
have twice nodes, i.e. 2 * 2l
Department of Electronics And Electrical Engineering, IIT Guwahati
Binary Tree: Properties
2) The maximum number of nodes in a binary tree of height ‘h’ is 2 h – 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 h is 1 + 2 + 4
+ .. + 2h. This is a simple geometric series with h terms and sum of
this series is 2h– 1.
Department of Electronics And Electrical Engineering, IIT Guwahati
Binary Tree: Properties
3) In a Binary Tree with N nodes, minimum possible height
or the minimum number of levels is Log2(N+1)

• We already have, N=2h– 1


• Here the height, h 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 Log2(N+1)
Department of Electronics And Electrical Engineering, IIT Guwahati
Binary Tree: Properties
4) A Binary Tree with L leaves has at least Log2L levels

A Binary tree has the maximum number of leaves (and a minimum


number of levels) when all levels are fully filled.
Let all leaves be at level l, then below is true for the number of leaves
L.
Max leaves L of level l is 2l
So min levels for L leaves = Log2L

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: Advantages
If there is no ordering, then we may have to compare every key
to search for a given key.

This would be equivalent to linear search, whose complexity is


O(n)

The properties of Binary Search Tree provides an ordering


among keys so that the operations like search, minimum and
maximum can be done fast.
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, O(log2n)


return true, else return false.

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


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


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


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


Heap Sort
14, 24, 12, 11, 25, 8, 35
35
For ascending make min heap For descending make max heap

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

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

7
8

BST behaves 8
like linked list 7 9

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.

Department of Electronics And Electrical Engineering, IIT Guwahati


AVL Rotations
• perform rotation in AVL tree only in case if Balance Factor is other than -1, 0, and 1
• four types of rotations which are as follows:
• L L rotation: Inserted node is in the left subtree of left subtree of A
• R R rotation : Inserted node is in the right subtree of right subtree of A
• L R rotation : Inserted node is in the right subtree of left subtree of A
• R L rotation : Inserted node is in the left subtree of right subtree of A
• 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
• 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