Tree
Tree
1
Introduction
Tree is an abstract model of hierarchical structure that consists of nodes with a parent-child relationship.
Terminologies : Root, Node, Parent and child, Leaf node, Internal node, Edges, Siblings, Subtree etc.
Characteristics:
2
Tree depth, height and level
Depth: The depth of a node is the number of edges from the root to that node. We can determine the depth of
each node in the tree.
Height: The height of a tree is the maximum depth among all its nodes.
Level: The level of a node is the distance from the root, where the root node is considered level 0 and each
subsequent level is incremented by 1.
Depth of Node I :3
Height of Tree :3
Level of node G :2
3
Binary Tree
A binary tree is a type of tree data structure in which each node can have at most two child nodes, referred
to as the left child and the right child.
It is commonly used to organize and store data in a hierarchical manner, enabling fast access and
manipulation operations.
4
Basics operation in Binary Tree
Binary trees support several basic operations that allow for the manipulation and traversal of the tree's
nodes. Here are the key basic operations in a binary tree:
Insertion: The insertion operation adds a new node to the binary tree.
Deletion: The deletion operation removes a node from the binary tree.
Search: The search operation allows us to find a specific node within the binary tree.
Traversal: Traversal refers to visiting all the nodes of the binary tree in a specific order. There are three
common traversal techniques:
• In-order Traversal: Visit the left subtree, then the current node, and finally the right subtree.
• Pre-order Traversal: Visit the current node, then the left subtree, and finally the right subtree.
• Post-order Traversal: Visit the left subtree, then the right subtree, and finally the current node.
Height: The height operation determines the maximum number of edges from the root to a leaf node in
the binary tree.
5
Insertion operation in Binary Tree
The insertion operation in a binary tree involves adding a new node to the tree. The steps for inserting a
node into a binary tree are as follows:
1) Start at the root node. If the tree is empty, make the new node the root and exit the operation.
2) Compare the value of the new node with the value of the current node.
i. If the value of the new node is less than the current node, move to the left child of the current
node.
ii. If the value of the new node is greater than the current node, move to the right child of the
current node.
iii. Repeat this comparison and movement until you reach a position where the current node has no
left or right child.
3) At this position, insert the new node as the left child if the value of the new node is less than the
current node, or as the right child if the value is greater.
4) Exit the insertion operation.
6
Insertion operation in Binary Tree
7
Deletion operation in Binary Tree
The deletion operation in a binary tree involves removing a specific node from the tree while maintaining
the binary tree's structure and properties. The steps for deleting a node from a binary tree are as follows:
1) Start at the root node and traverse the tree to find the node to be deleted.
2) Once the node is found, there are different cases to consider for deletion:
a. Node has no children (leaf node):
i. Simply remove the node from the tree.
b. Node has only one child:
i. Replace the node with its child.
c. Node has two children:
i. Either, replace the node with the rightmost node of left subtree ie, maximum value of left
subtree
ii. Or, replace the node with leftmost node of right subtree ie, minimum value of right subtree
8
Deletion operation in Binary Tree
9
Search in Binary Tree
Searching in a binary search tree (BST) is an efficient operation that allows us to find a specific node or
determine whether a given value exists in the tree. Here's how the search operation works in a binary search
tree:
2) Compare the search value with the value of the current node.
i. If the search value matches the value of the current node, we have found the desired node. The
search operation is successful.
ii. If the search value is less than the value of the current node, move to the left child of the current
node.
iii. If the search value is greater than the value of the current node, move to the right child of the
current node.
3) Repeat the comparison and movement steps until one of the following conditions is met:
i. The search value matches the value of a node, indicating a successful search.
4) If we reach a leaf node without finding a match, the search value does not exist in the BST.
10
Search in Binary Tree
Determine Node : 92
11
Binary Tree Traversals
Binary tree traversals are methods used to visit and process each node in a binary tree. There are three common
types of binary tree traversals:
Examples :
Y Pre-order : yxz
In-order : xyz
X Z Post-order: xzy
12
Binary Tree Traversals
13
Balanced Trees
A balanced tree is a type of tree data structure in which the heights of the left and right subtrees
of any node differ by at most a specific value (usually 1).
Maintaining balance in a tree can help ensure efficient operations and reduce the worst-case
time complexity.
14
AVL Balanced Trees
AVL trees are balanced binary search trees that ensure the heights of the left and right subtrees
of any node differ by at most 1.
15
AVL Balanced Trees
To balance an AVL tree, we need to identify the specific scenario where the balance property is
violated and perform the necessary rotations to restore balance. There are four possible cases
that may require rotation:
16
AVL Balanced Trees
Shortcuts :
L L -> R
R R -> L
L R -> L R
R L -> R L
17
Construct AVL Balanced Trees with data items :
b) 1,2,3,4,5,6,7
18
Huffman Algorithm
The Huffman algorithm is a data compression technique used to reduce the size of data by assigning variable-
length codes to different symbols based on their frequency of occurrence.
Algorithm :
1) Create a frequency table by counting the occurrence of each symbol in the input data.
2) Initialize a priority queue or min-heap with the symbols and their frequencies.
3) While there is more than one node in the priority queue:
a) Remove the two nodes with the lowest frequencies.
b) Create a new internal node with the sum of their frequencies.
c) Set the two nodes as the left and right child of the new node.
d) Insert the new node back into the priority queue.
4) The remaining node in the priority queue is the root of the Huffman tree.
5) Traverse the Huffman tree, assigning a '0' for a left branch and a '1' for a right branch.
6) Generate the Huffman code by concatenating the assigned '0' and '1' for each symbol.
7) Output the Huffman code as the compressed representation of the input data.
19
Huffman Algorithm
1). BCCABBDDAECCBBAEDDCC
2).BCAADDDCCACACAC
20
Applications of Tree
21
Expression Tree
3*6+4/2
Game Tree
• A game tree is a data structure that represents the possible moves and outcomes
of a game.
• Game trees are used in a wide range of games, including chess, tic-tac-toe, poker,
and many others.
23
B Tree
24