Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 51

Unit Seven

Trees
Introduction
• A tree data structure is a hierarchical structure that is used to represent and
organize data in a way that is easy to navigate and search. It is a collection of
nodes that are connected by edges and has a hierarchical relationship between the
nodes.
• The topmost node of the tree is called the root, and the nodes below it are called
the child nodes. Each node can have multiple child nodes, and these child nodes
can also have their own child nodes, forming a recursive structure.
• This data structure is a specialized method to organize and store data in the
computer to be used more effectively. It consists of a central node, structural
nodes, and sub-nodes, which are connected via edges. We can also say that tree
data structure has roots, branches, and leaves connected with one another.
Basic Terminologies
• Node: A node is an entity that contains a key or value and pointers to its child nodes. The last
nodes of each path are called leaf nodes or external nodes that do not contain a link/pointer
to child nodes. The node having at least a child node is called an internal node.
• Edge: It is the link between any two nodes.

• Root: It is the topmost node of a tree.


• Height of a Node: The height of a node is the number of edges from the node to the deepest
leaf (ie. the longest path from the node to a leaf node).
• Depth of a Node: The depth of a node is the number of edges from the root to the node.
• Height of a Tree: The height of a Tree is the height of the root node or the depth of the
deepest node.

• Degree of a Node: The degree of a node is the total number of branches of that node.
• Degree of a tree: It is the maximum degree of nodes in a given tree
• Path: The path is the sequence of consecutive edges from source node to destination node
Basic Operation Of Tree Data Structure:
• Create – create a tree in the data structure.
• Insert − Inserts data in a tree.
• Search − Searches specific data in a tree to check whether it is present or not.
• Traversal:
• Preorder Traversal – perform Traveling a tree in a pre-order manner in the data
structure.
• In order Traversal – perform Traveling a tree in an in-order manner.
• Post-order Traversal –perform Traveling a tree in a post-order manner.
Types of Tree
1. Binary Tree
• Binary Trees are general trees in which the root node can only hold up to maximum 2
subtrees: left subtree and right subtree. Based on the number of children, binary trees are
divided into three types.
Full Binary Tree
• A full binary tree is a binary tree type where every node has either 0 or 2 child nodes.
Complete Binary Tree
• A complete binary tree is a binary tree type where all the leaf nodes must be on the same
level. However, root and internal nodes in a complete binary tree can either have 0, 1 or 2
child nodes.
Perfect Binary Tree
• A perfect binary tree is a binary tree type where all the leaf nodes are on the same level and
every node except leaf nodes have 2 children.
Perfect Binary Tree

Complete Binary Tree


2. Binary Search tree:
Binary search tree is a non-linear data structure in which one node is connected to n number
of nodes. It is a node-based data structure. A node can be represented in a binary search tree
with three fields, i.e., data part, left-child, and right-child. A node can be connected to the
utmost two child nodes in a binary search tree, so the node contains two pointers (left child
and right child pointer).
Every node in the left subtree must contain a value less than the value of the root node, and
the value of each node in the right subtree must be bigger than the value of the root node.
3. AVL Tree
• AVL Tree is invented by GM Adelson - Velsky and EM Landis in 1962. The tree is named AVL
in honour of its inventors.
• AVL Tree can be defined as height balanced binary search tree in which each node is associated
with a balance factor which is calculated by subtracting the height of its right sub-tree from that
of its left sub-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.
• Balance Factor (k) = height (left(k)) - height (right(k))
• 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.
• An AVL tree is given in the following figure. We can see that, balance factor associated
with each node is in between -1 and +1. therefore, it is an example of AVL tree.
4. General Tree
• The general tree is one of the types of tree data structure. In the general tree, a node can
have either 0 or maximum n number of nodes. There is no restriction imposed on the
degree of the node (the number of nodes that a node can contain). The topmost node in a
general tree is known as a root node. The children of the parent node are known
as subtrees.

• There can be n number of subtrees in a general tree. In the general tree, the subtrees are
unordered as the nodes in the subtree cannot be ordered.
Every non-empty tree has a downward edge, and these edges are connected to the nodes
known as child nodes. The root node is labeled with level 0. The nodes that have the same
parent are known as siblings.
Basic Operations in Binary Search Tree
1. Insertion

● Insertion a node into an empty tree - the node inserted into the tree is considered as the
root node.
● Inserting a node into a non-empty tree - we compare the new node to the root node of the
tree.
○ If the value of the new node is less than the value of the root node, then,
if the left subtree is empty, the new node is appended as the left leaf of the root node
Else, we search continuous down the left subtree.
○ If the value of the new node is greater than the value of the root node, then,
if the right subtree is empty, the new node is appended as the right leaf of the root
node
Else, we search continuous down the right subtree.
○ If the value of the new node is equal to the value of the root node, then print
“DUPLICATE ENTRY” and return.
Insertion Algorithm

Insert (TREE, ITEM)


1. IF TREE == NULL
a. Allocate memory for TREE
b. SET TREE->DATA = ITEM
c. SET TREE->LEFT = TREE->RIGHT = NULL
2. ELSE
a. IF ITEM < TREE->DATA
i. Insert (TREE->LEFT, ITEM)
b. ELSE
i. Insert(TREE->RIGHT, ITEM)
Insert(TREE, 95)
Assignment

Insert items in order from an empty tree to form a BST


{13,3,4,12,14,10,5,1,8,2,7,9,11,18}
2. Deletion

1. Locate the node to be deleted and its parent (The parent of root node is NULL)
2. If it is the leaf node - set NULL to its parent pointer. If node is with one child -redirect
the parent pointer to point to its child pointer.
3. If node is with two child - replace node being deleted by:
a. Leftmost node of its right subtree
b. OR, Rightmost node to its left subtree
Case 1: Node with 0 children: this is the easiest situation, we just need to delete the node
which has no further children on the right or left.
• Case 2: Node with 1 child: once you delete the node, simply connect its child node with the
parent node of the deleted value.
Case 3: Node with 2 children: it works on the following two rules:
a. In order predecessor - we need to delete the node with 2 children and replace it with
the largest value on the left subtree of the deleted node.
Case 3: Node with 2 children: it works on the following two rules:
B. In order successor - we need to delete the node with 2 children and replace it with the
smallest value on the right subtree of the deleted node.
3. Search
● Search operation is performed with O(log n) time complexity in a binary search tree.
● This operation starts from the root node.
● It is used whenever an element is to be searched.
● The following algorithm shows the search operation in binary search tree:
○ Read the element from the user.
○ Compare this element with the value of root node in a tree.
○ If element and value are matching, display "Node is Found" and terminate the
function.
○ If element and value are not matching, check whether an element is smaller or larger
than a node value.
● If an element is smaller, continue the search operation in left subtree.
● If an element is larger, continue the search operation in right subtree.
● Repeat the same process until we found the exact element.
● If an element with search value is found, display "Element is found" and terminate the
function.
● If we reach to a leaf node and the search value is not match to a leaf node, display
"Element is not found" and terminate the function.
Example - Search for element 10 in the given tree.
● Steps:
4. Traversal
● Binary tree traversing is a process of accessing every node of the tree and exactly once.
● A tree is defined in a recursive manner. So, binary tree traversal also is defined
recursively.
● All nodes are connected via edges, we always start from the root node.
● There are three ways which we can use to traverse a tree:
○ In- order traversal
○ Pre- order traversal
○ Post - order traversal
● Generally, we traverse a tree to search or locate given item or key in the tree or to print all
the values it contains.
Pre Order Traversal

● The preorder traversal is also known as depth first order.


● The preorder traversal of a nonempty binary tree is defined as follows:
○ Visit the root node
○ Traverse the left sub-tree in preorder
○ Traverse the right sub-tree in preorder
Order : root=>left=>right
Preorder traversal:
A->B->D->H->I->E->
C->F->G
In order Traversal

● The in-order traversal of a non-empty binary tree is defined as:


○ Traverse the left sub-tree in-order
○ Visit the root node
○ Traverse the right sub-tree in in-order
Traversal order=>
left->root->right
H->D->I->B->E->A->F->C->G
Post Order Traversal

● The post-order traversal of a non-empty binary tree is defined as:


○ Traverse the left sub-tree post-order
○ Traverse the right sub-tree in post-order
○ Visit the root node
Traversal order=>
left->right->root
H->I->D->E->B->F->G->C->A
Assignment
● Perform Preorder, In order and Post order traversal
Applications of Binary Tree
• File Systems: Binary trees are used in file systems to organise and store files. Each node in the
tree represents a file or a directory, and the left child of a node represents a subdirectory or a
file that is smaller than the node, while the right child represents a subdirectory or a file that is
larger than the node.
• Search Engines: Binary trees are used in search engines to organise and index web pages.
Each node in the tree represents a web page, and the left child of a node represents a web page
ranked lower than the node, while the right child represents a web page ranked higher than the
node.
• Searching and Sorting Algorithms: It is commonly used in searching and sorting algorithms,
including binary search, binary insertion sort, and quicksort.
• Expression Trees: Binary trees can also be used to represent math's expressions, such as
equations. These are known as expression trees. Expression trees are basically used in
evaluating and simplifying expressions and generating code.
• Huffman Coding: It is a data compression algorithm that uses binary trees to encode
characters. The binary tree used in Huffman coding is called a Huffman tree, and it is a binary
tree in which the nodes represent the characters to be encoded. The algorithm assigns
variable-length characters to each character, and the most frequent characters receive short
Basic Operations of AVL Tree
Construction
● AVL Tree is constructed by using the same process as applied to BST.
● The only thing we need to remember that it follow the property of height balance tree.
● The balance of each node should be -1,0 or 1.
● While operation on AVL - like insertion or deletion is performed, the reee becomes unbalanced and violate
the property of AVL tree. So, we need to have to apply different rotation on the AVL Tree.

Note:
BF is -1, if the right sub-tree is higher than the left sub-tree [ right heavy]
BF is 1, if the left subtree is higher than the right sub-tree[left heavy]
BF is 0 ie, Both sub-tree are on same height
Rotations

When BST becomes unbalanced, due to node insertion into right subtree, the we perform RR
rotation.
● RR rotation is an clockwise rotation and applied on the edge below a node having balance
factor 2.
Double Rotations

● Double rotations are complicated than single rotation and should be applied carefully.
● LR Rotation = LL Rotation + RR Rotation
● First LL rotation is performed on subtree
● Then RR rotation is performed on full tree,
● Full tree means the first node from the path of inserted node whose balance factor is
other than -1, 0 or 1.
RL Rotation
● RL Rotation = RR Rotation + LL Rotation
● First RR rotation is performed on subtree
● Then LL rotation is performed on full tree,
● Full tree means the first node from the path of inserted node whose balance factor is
other than -1, 0 or 1.
Balance Tree (AVL Tree)
1. Insert the new element into the tree using Binary Search Tree insertion logic.
2. After insertion, check the Balance Factor of every node.
3. If the Balance Factor of every node is 0, 1 or -1, then go for next operation.
4. If the balance factor of any node is other than 0,1 or -1 then that tree is said to be
imbalanced. In this case, perform suitable Rotation to make it balanced and go for next
operation.

● IF BF(node) = +2 and BF(node->left) = +1, perform RR rotation.


● IF BF(node) = -2 and BF(node->right) = -1, perform LL rotation.
● IF BF(node) = -2 and BF(node->right) = +1, perform RL rotation.
● IF BF(node) = +2 and BF(node->left) = -1, perform LR rotation.
Huffman Coding
● In 1951, David Huffman found the “most efficient method of representing numbers,
letters, and other symbols using binary code”. Now standard method used for data
compression.
● In Huffman Algorithm, a set of nodes assigned with values if fed to the algorithm.
Initially 2 nodes are considered and their sum forms their parent node.
● When a new element is considered, it can be added to the tree. Its value and the
previously calculated sum of the tree are used to form the new node which in turn
becomes their parent.
● Let us take any four characters and their frequencies, and sort this list by increasing
frequency.
● Since to represent 4 characters the 2 bit is sufficient thus take initially two bits for each
character this is called fixed length character.
Here before using Huffman algorithm the total number of bits
required is nb=3*2+5*2+7*2+10*2=06+10+14+20 = 50 bits
● Thus after using Huffman algorithm the total number of bits required is
nb = 3*3+5*3+7*2+10*1=09+15+14+10=48 bits
● Reduction of bits is (50-48)/50 * 100% = 4%
● In real life, we have large information with a lot of characters and their frequencies are
also significantly large, se we can save a lot of space.
● Ex: For the characters with given frequencies, construct the Huffman coding tree and find
the number of bits saved by using the Huffman coding.
Game Tree
• Game tree is a graph representing all possible game states within such a game.
• Such games include well-known ones such as chess, checkers, Go, and tic-tac-toe.
• This can be used to measure the complexity of a game, as it represents all the possible
ways a game can pan out.
• It is a graph consisting of nodes and edges that represent all the possible legal moves of a
(board) game.
• The nodes are positions in a game and the edges are the moves.
• It is a representation of the play of a game where the root or first node is the initial state
of the game, the first level is all the possible moves of the player, and the content of the
nodes are the resulting states of the game; the leaf nodes represent when the game is over.
• Game trees are used to represent two-player games.
• Alternate moves in the game are represented by alternate levels in the tree (plies).
• Nodes in the tree represent positions.
• Edges between nodes represent moves.
• Leaf nodes represent won, lost or drawn positions.
• For most games, the game tree can be enormous.
Example
B Tree
• B-tree is a special type of self-balancing search tree in which each node can contain more
than one key and can have more than two children. It is a generalized form of the binary
search tree.
• It is also known as a height-balanced m-way tree.
Need of B Tree
• The need for B-tree arose with the rise in the need for lesser time in
accessing physical storage media like a hard disk. The secondary
storage devices are slower with a larger capacity. There was a need for
such types of data structures that minimize the disk access.
• Other data structures such as a binary search tree, avl tree, red-black
tree, etc can store only one key in one node. If you have to store a
large number of keys, then the height of such trees becomes very
large, and the access time increases.
B-Tree Properties
• For each node x, the keys are stored in increasing order.
• In each node, there is a Boolean value x.leaf which is true if x is a leaf.
• If n is the order of the tree, each internal node can contain at most n - 1 keys
along with a pointer to each child.
• Each node except root can have at most n children and at least n/2 children.
• All leaves have the same depth (i.e. height-h of the tree).
• The root has at least 2 children and contains a minimum of 1 key.
• If n ≥ 1, then for any n-key B-tree of height h and minimum degree t ≥ 2, h ≥
logt (n+1)/2.
Construct a B-Tree of order 4 with the following values: [1,6,8,2,9,12,15,7,18,3,4,20]
Now Add 3,4 & 20
to obtain final B-Tree
Expression Tree

● A binary tree in which the operators are stored in the interior nodes and the operands are
stored in the leaves.
● Used to evaluate an expression.
● Used to convert an infix expression to either prefix or postfix notations.
● Expression tree structure is based on the order in which the operators are evaluated.
● Operators in lower-level nodes are evaluated first.
● The last operator evaluated is in the root node.
Assignment
● Construct the expression into expression tree:
(A+B)*C-D$(E+F)

You might also like