CACS201 Unit 7 - Trees
CACS201 Unit 7 - Trees
Trees
Contents
• Introduction
• Basic Operation in Binary Tree
• Tree Search and Insertion/Deletion
• Binary Tree Traversal (pre-order, post-order and in-order)
• Tree Height, Level and Depth
• Balanced Tree: AVL Balanced Trees, Balancing Algorithm
• The Huffman Algorithm
• Game tree
• B-Tree
Introduction
• A tree is a data structure that is defined as a set of one or more nodes which
allows us to associate a parent-child relationship.
• In trees, one node is designated as the root node or parent node, and all the
remaining nodes can be partitioned into non-empty sets, each of which is a
subtree of the root.
• Unlike natural trees, a tree data structure is upside down, having a root at the top
and leaves at the bottom.
• Also, there is no parent of the root node.
• A root node can only have child nodes.
• On the contrary, leaf nodes or leaves are those that have no children.
• When there are no nodes in the tree, then the tree is known as a null tree or
empty tree.
• Trees are widely used in various day to day applications.
• Also, the recursive programming of trees makes the programs optimized and
easily understandable.
• Trees are also used to represent the structure of mathematical formulas.
Definition
• Node – A node is the main component of the tree data structure. It stores the actual data along
with the links to the other nodes.
• Root – The root node is the topmost node of the tree. It does not have a parent node. If the root
node is empty, then the tree is empty.
• Parent – The parent of a node is the immediate predecessor of that node.
• Child – The child nodes are the immediate successors of a node. They must have a parent node. A
child node placed at the left side is called the left child, and similarly, a child node placed at the
right side is called a right child.
• Leaf/ Terminal nodes – A leaf node is one which does not have any child nodes.
• Subtrees – The nodes B, X, and C form the left subtree of root A. Similarly, the nodes D, E and Z
form the right subtree of A.
• Path – It is a unique sequence of consecutive edges which is required to be followed to reach the
destination from a given source. The path from root node A to B is given as A-X, X-B.
• Level number of a node – Every node in the tree is assigned a level number. The root is at level 0,
the children of the root node are at level 1, and so on.
Definition
• Height – The height of the tree is the maximum level of the node + 1. The height of a
tree containing a single node will be 1. Similarly, the height of an empty tree will be 0.
• Ancestors – The ancestors of a node are any predecessor nodes on the path between the
root and the destination. There are no ancestors for the root node. The nodes A and X
are the ancestors of node B.
• Descendants – The descendants of a node are any successor nodes on the path between
the given source and the leaf node. There are no descendants of the leaf node. Here, B,
X, and C are the descendants of node A.
• Siblings – The child nodes of a given parent node are called siblings. B and C are the
siblings of X.
• Degree of a node – It is equal to the number of children that a node has.
• Out-degree of a node – It is equal to the number of edges leaving that node.
• In-degree of a node – It is equal to the number of edges arriving at that node.
• Depth – It is given as the length of the path from the root node to the destination node.
Binary Tree
• A binary tree is a collection of nodes where each node
contains three parts, that is, left pointer, right pointer,
and the data item.
• The left pointer points to the left subtree and the
right pointer points to the right subtree.
• The topmost element of the binary tree is known as a
root node.
• The root pointer points to the root node.
• As the name suggests, a binary tree can have at most
two children, i.e., either a parent can have zero, one,
or at most two children.
• Also, if root = NULL, then it means that the tree is
empty.
Types of Binary Trees
• Strictly binary tree or full binary tree or proper binary tree
• A full binary tree or a proper binary tree or a strictly binary tree is a
tree in which each node other than the leaves has two children.
• This means each node in the binary tree will have either 0 or 2 children
is said to be a full binary tree or strictly binary tree.
• Complete binary tree or perfect binary tree
• A complete binary tree is a binary tree in which every level, except
possibly the last, is filled, and all nodes are as far left as possible.
• All complete binary trees are strictly binary trees, but all strictly binary
trees are not complete binary trees.
• A complete binary tree is the first strictly binary tree with the height
difference of only 1 allowed between the left and right sub-tree.
• All leaf nodes are either at the same level or only a difference of 1 level
is permissible.
Types of Binary Trees
• Binary search tree
• If a binary tree has the property that all elements in the left subtree
of a node n are lesser than the contents of n and all elements in the
right sub-tree are greater than or equal to the contents of n, such a
binary tree is called a binary search tree (BST).
• In a BST, if the same elements are inserted, then the same element is
inserted on the right side of the node, having the same value.
• Almost complete binary tree
• A binary tree of depth d is called an almost complete binary tree if
and only if:
1. Each leaf node in the tree is either at level d or at level d-1, which means
the difference between levels of leaf nodes is only one is permissible.
2. At depth d, that is the last level, if a node is present, then all the nodes to
the left of that node should also be present first, and no gap between the
two nodes from the left to the right side of level d.
Types of Binary Trees
• Skewed binary tree
• If a tree is dominated by the left child node or the right child node, it is said to
be a skewed binary tree.
• In a left-skewed tree, most of the nodes have a left child without a
corresponding right child.
• In a right-skewed tree, most of the nodes have the right child without a
corresponding left child.
Binary Tree Representation
• There are two binary tree representation techniques:
1. Sequential representation or array representation
2. Linked representation or linked list representation
Array Representation of Binary Trees
• Array or Sequential representation of binary trees is done using one-
dimensional (1-D) arrays.
• This type of representation is static and hence it is inefficient, as the size
must be known in advance and it thus requires a lot of memory space.
• The following rules are used to decide the location of each node in the
memory:
• The root node of the tree is stored in the first location.
• If the parent node is present at location k, then the left child is stored at location 2k,
and the right child is stored at location (2k + 1).
• The maximum size of the array is given as (2h – 1), where h is the height of the tree.
Array Representation of Binary Trees
Array Representation of Binary Trees
• Advantages of sequential representation:
• Direct access to any node can be possible.
• Finding parent or finding left, the right child of any particular node is faster.
• Disadvantages of sequential representation:
• Wastage of memory: If a binary tree is not a complete binary tree, then array
size increased.
• In this type of representation, prior knowledge of the depth of a tree is
required because on that basis we have to fix the size of the array.
• Insertion and deletion of any node in the tree will be costlier as other nodes
have to be adjusted at appropriate positions.
Linked Representation of Binary Trees
• A binary tree can also be represented using a linked list in a computer’s memory.
• This type of representation is dynamic as memory is dynamically allocated, that
is, when it is needed, and thus it is efficient and avoids wastage of memory space.
• In linked representation, every node has three parts:
• The 1st part is called the left pointer, which contains the address of the left subtree.
• The 2nd part is called the data part, which contains the information of the node.
• The 3rd part is called the right pointer, which contains the address of the right subtree.
• The structure of the node is declared as follows:
struct node
{
struct node *leftchild ;
int information ;
struct node *rightchild ;
}
Linked Representation of Binary Trees
Linked Representation of Binary Trees
• Advantages of Linked list representation:
• There is no wastage of memory.
• There is no need to have prior knowledge of the depth of the tree. Using the
dynamic memory management concept, we can create as much memory or nodes as
required.
• Insertion and deletion of nodes that are the most common operations can be done
without moving other nodes.
• Disadvantages of Linked list representation:
• This representation gives no direct access to a node, and special algorithms are
required.
• This representation needs additional space in each node to store left and right sub-
tree addresses.
• The direct address of the left and right child does not get in linked representation.
Basic Operation in Binary Tree
• To create a binary tree.
• To display data in each node of binary tree that is tree traversal.
• To insert any node in the binary tree as the left child or right child of
any node.
• To delete a node from a binary tree.
Insertion in Binary Tree
To insert a new node, we need to
know where to insert it, i.e., we need
to find:
• Which node will be the parent node
• Whether this node is the left child or
the right child, it is easy to find. If
the parent has a left child, this node
will be the right child, otherwise the
left child.
Insertion in Binary Tree – Complexity Analysis
• The space complexity of the algorithm is O(max(B, W)), where B is the
breadth of the tree and W is the width of the tree.
• For time complexity, we need to check the time complexity of all the
operations we are performing:
• The worst-case time complexity of inserting an element at the end of the parent list
is O(1).
• The worst-case time complexity of deleting an element from the start of the parent
list is O(1).
• The worst-case time complexity of creating a new node is O(1).
• The worst-case time complexity of associating the created node as a left child or a
right child is O(1).
• All the operations we are performing have a worst-case time complexity of
O(1). Therefore, the worst-case time complexity of insertion is O(1).
Deletion in Binary Tree
• The nature of the binary tree is such
that the last inserted node will always
be a leaf node, maintaining the desired
property if deleting this node.
• If the node that we want to delete is the
last inserted node itself, we need not do
much. We can delete this node.
• If the node to delete is not the last
inserted node, we need to make it the
last inserted node. To achieve this, we
can swap the node's value with the last
inserted node’s value, and the deletion
operation changes to delete the last
inserted node.
Deletion in Binary Tree
Deletion in Binary Tree - Algorithm
Deletion in Binary Tree – Complexity Analysis
• We use the possible parent list in the deletion, so the space complexity of
the algorithm is O(max(B, W)), where B is the breadth of the tree and W is
the width of the tree.
• For time complexity, we need to check the time complexity of all the
operations we are performing:
• All the operations on the parent list have worst-case time complexity O(1).
• Swapping the values is done in O(1) time complexity.
• Deleting a node is done in O(1) time complexity.
• Since we do not have a strong ordering between the values of the parent and child
nodes, we have to iterate on all the nodes to find the desired node. Therefore, the
worst-case time complexity is O(N).
• Based on all the operations, the worst-case time complexity of deletion is
O(N).
Search in Binary Tree
• To find whether a node exists with a value or not, we have to iterate
through all the nodes. Since there is no order, we cannot skip any
node.
Search in Binary Tree – Complexity Analysis
• Space and time complexity of the search algorithm depends on the
kind of traversal we are using.
• For example, we used the pre-order traversal here. The space
complexity of the algorithm is O(H), where H is the height of the tree.
• If we do level-order traversal, the space complexity of the algorithm
changes to O(max(B, W)), where B is the breadth of the tree and W is
the width of the tree.
• The worst-case time complexity of the search is always O(N) because
we are visiting each node to find the given value.
Binary Tree Traversal
• Traversing the tree means visiting each node in the tree exactly once.
• There are six ways to traverse a tree.
• For these traversals, we will use some notations as:
• L means move to left child.
• R means move to right child.
• R’ means move to root or parent node.
• Therefore, there are six different combinations of L, R, R’ nodes as LR’R, LRR’, R’LR, R’RL, RR’L and
RLR’.
• However, from there three traversing methods are important, which are as follows:
• Left-Root-Right (LR’R): In-order traversal.
• Root-Left-Right (R’LR): Preorder traversal.
• Left-Right-Root (LRR’): Post-order traversal.
• These all above traversing technique is also called as Depth First searching (DFS) or Depth First
Traversal.
• A tree is typically traversed in two ways:
• Breadth first traversal (BFS) or level order traversal
• Depth first traversal (DFS)
Breadth First Traversal or Level Order Traversal
• The items are visited level-wise.
• Therefore, the first node A is visited, then at the second level,
first the nodes B then C are visited, and then at the third
level, first nodes D, then F and then G are visited.
• Level Order Traversal is as follows: A - B - C - D - F – G
• Algorithm for Level Order Traversal in Binary tree
1. Create an empty queue q.
2. temp_node = root // Start from the root node and assign the value
of root to temp_node
3. while temp_node is not NULL repeat the steps
3.1. Print temp_node->data.
3.2.Enqueue temp_node’s children from first left then right children to q.
3.3. Dequeue a node from q and assign its value to temp_node.
4. Stop.
Breadth First Traversal – Complexity Analysis
• Time Complexity of Breadth First Traversal or Level Order Traversal: O
(n) because we visit every node exactly once.
• Extra space required for level order traversal is O (w) where w is the
maximum width of a binary tree. In breadth first traversal, a queue
data structure is required. In breadth first traversal, queue data
structure stores one by one node of different levels.
Depth First Traversal
• Depth first traversal has following three types of traversing:
• In-order traversal
• Preorder traversal
• Post-order traversal
• In-order Traversal
• The basic principle is to traverse the left sub-tree, then root and then the
right sub-tree.
• In-order traversal is as follows: D - B - A - F - C – G
• Preorder Traversal
• The basic principle is to traverse the first root, then the left sub-tree and
then the right sub-tree.
• Preorder traversal is as follows: A - B - D - C - F – G
• Post-order Traversal
• The basic principle is to traverse the first left sub-tree, then the right sub-
tree and then root.
• Post-order traversal is as follows: D - B - F - G - C – A
Recursive Algorithm for In-order Traversal
In-order traversing of a binary tree T with root R.
Algorithm in-order (root):
1. Traverse the left sub-tree of root R, that is, call in-order (left sub-
tree).
2. Visit the root, R and process.
3. Traverse the right sub-tree of root R, that is, call in-order (right sub-
tree).
Recursive Algorithm for Preorder Traversal
Preorder traversing of binary tree T with root R.
Algorithm preorder (root):
1. Visit the root, R and process.
2. Traverse the left sub-tree of root R, that is, call preorder (left sub-
tree)
3. Traverse the right sub-tree of root R, that is, call preorder (right sub-
tree)
Recursive Algorithm for Postorder Traversal
Postorder traversing of binary tree T with root R.
Algorithm Postorder (Root)
1. Traverse the left sub-tree of root R, that is, call postorder (left sub-
tree)
2. Traverse the right sub-tree of root R, that is, call postorder (right
sub-tree)
3. Visit the root, R and process.
WAP in C for implementation of simple binary tree
and performing recursive insertion and traversing
operations on a simple binary tree.
Binary Search Tree
• A binary search tree (BST) is a variant of a binary tree.
• The special property of a binary search tree is that all the
nodes in the left subtree have a value less than that of a
root node.
• Similarly, all the nodes in the right subtree have a value
more than that of a root node.
• Hence, the binary search tree is also known as an ordered
binary tree, because all the nodes in a binary search tree
are ordered.
• Also, the left and the right subtrees are also binary search
trees and thus, the same property is applicable on every
subtree in the binary search tree.
• Binary search trees are very efficient regarding searching
an element. These trees are already sorted in nature. Thus,
these trees have a low time complexity.
Operations on Binary Search Trees
• Searching a node/key in the binary search tree
• Inserting a node/key in the binary search tree
• Deleting a node/key from the binary search tree
• Deleting the entire binary search tree
• Finding the mirror image of the binary search tree
• Finding the smallest node in the binary search tree
• Finding the largest node in the binary search tree
• Determining the height of the binary search tree
Searching a node/key in the binary search tree
• The searching operation is one of the most common operations performed in the binary
search tree.
• This operation is performed to find whether a given key exists in the tree or not.
• The searching operation starts at the root node.
• First, it will check whether the tree is empty or not. If the tree is empty, then the
node/key for which we are searching is not present in the tree, and the algorithm
terminates there by displaying the appropriate message.
• If the tree is not empty and the nodes are present in it, then the search function checks
the node/value to be searched and compares it with the key value of the current node.
• If the node/key to be searched is less than the key value of the current node, then in that
case, we will recursively call the left child node.
• On the other hand, if the node/key to be searched is greater than the key value of the
current node, then we will recursively call the right child node.
Algorithm for searching for a node/key in the binary search tree
SEARCH(ROOT, VALUE)
Step 1: START
Step 2: IF(ROOT == NULL)
Return NULL
Print “Empty Tree”
ELSE IF(ROOT -> INFO == VALUE)
Return ROOT
ELSE IF(ROOT -> INFO > VALUE)
SEARCH(ROOT -> LCHILD, VALUE)
ELSE IF(ROOT -> INFO < VALUE)
SEARCH(ROOT -> RCHILD, VALUE)
ELSE
Print “Value not found”
[End of IF]
[End of IF]
[End of IF]
[End of IF]
Step 3: END
Inserting a node/key in the binary search tree
• The insertion operation also starts at the root node.
• First, it will check whether the tree is empty or not. If the tree is
empty, then we will allocate the memory for the new node.
• If the tree is not empty, then we will compare the key value to be
inserted with the value stored in the current node.
• If the node/key to be inserted is less than the key value of the current
node, then the new node is inserted in the left subtree.
• On the other hand, if the node/key to be inserted is greater than the
key value of the current node, then the new node is inserted in the
right subtree.
Algorithm for inserting a node/key in the binary search tree
INSERT(ROOT, VALUE)
Step 1: START
Step 2: IF(ROOT == NULL)
Allocate memory for ROOT node
Set ROOT -> INFO = VALUE
Set ROOT -> LCHILD = ROOT -> RCHILD = NULL
[End of IF]
Step 3: IF(ROOT -> INFO > VALUE)
INSERT(ROOT -> LCHILD, VALUE)
ELSE
INSERT(ROOT -> RCHILD, VALUE)
[End of IF]
Step 4: END
Deleting a node/key from the binary search tree
The deletion operation is divided into three cases as follows:
• Case 1: Deleting a node having no children: This is the simplest case of
deletion, as we can directly remove or delete a node which has no children.
• Case 2: Deleting a node having one child: In this case of deletion, the node
which is to be deleted, the parent node, is simply replaced by its child
node.
• Case 3: Deleting a node having two children: In this case, the node which
is to be deleted is simply replaced by its in-order predecessor, that is, the
largest value in the left subtree, or by its in-order successor, that is, the
smallest value in the right subtree. Also, the in-order predecessor or in-
order successor can be deleted using any of the two cases.
Algorithm for deleting a node/key from the binary search tree
Deleting the entire binary search tree
• First, we will delete all the nodes present in the left subtrees followed
by the nodes present in the right subtree.
• Finally, the root node is deleted, and the entire tree is deleted.
Finding the mirror image of the binary search tree
• The mirror image of the binary search tree means interchanging the
right subtree with the left subtree at each and every node of the tree.
Finding the smallest node in the binary search tree
• It is the basic property of the binary search tree that the smallest
value always occurs in the extreme left of the left subtree.
• If there is no left subtree, then the value of the root node will be the
smallest.
• Hence, to find the smallest value in the binary search tree, we will
simply find the value of the node present at the extreme left of the
left subtree.
Finding the largest node in the binary search tree
• The basic property of the binary search tree that the largest value
always occurs in the extreme right of the right subtree.
• If there is no right subtree, the value of the root node will be the
largest.
• Hence, to find the largest value in the binary search tree, we will
simply find the value of the node present at the extreme right of the
right subtree.
Determining the height of the binary search tree
• We will first calculate the heights of the left subtree and the right
subtree.
• Whichever height is greater, 1 is added to that height.
Applications of Binary Tree and BST
• BST is used to implement dictionary.
• BST is used to implement multilevel indexing in the database.
• BST is used to implement the searching algorithm.
• Heaps is a special type of binary tree: Used in implementing efficient priority queues, which in turn are used for scheduling
processes in many operating systems, quality-of-service in routers and the path-finding algorithm used in artificial intelligence
applications, including robotics and video games. It is also used in heap-sort.
• Syntax tree: In the syntax analysis phase of the compiler, parse tree or syntax tree is drawn for parsing expressions either top-
down parsing or bottom-up parsing.
• BST is used in Unix kernels for managing a set of virtual memory areas (VMAs). Each VMA represents a section of memory in a
Unix process.
• Each IP packet sent by an Internet host is stamped with a 16-bit id that must be unique for that source-destination pair. The
Linux kernel uses an AVL tree or height-balanced tree for an indexed IP address.
• BST is used to efficiently store data in a sorted form in order to access and search stored elements quickly.
• BST is used in dynamic sorting.
• Huffman Code Construction: A method for the construction of minimum redundancy codes. Huffman code is a type of optimal
prefix code that is commonly used for lossless data compression. The algorithm has been developed by David A. Huffman. The
technique works by creating a binary tree of nodes. The nodes count depends on the number of symbols.
• Binary trees are a good means of expressing arithmetical expressions. The leaves or exterior nodes are operands, and the
interior nodes are operators. Two usual types of expressions that a binary expression tree can describe are algebraic and
Boolean. These binary trees can represent expressions that incorporate both unary and binary operators.
Balanced Binary Tree
• A balanced binary tree, also referred to as a height-balanced binary
tree, is defined as a binary tree in which the height of the left and
right subtree of any node differ by not more than 1.
• Following are the conditions for a height-balanced binary tree:
• difference between the left and the right subtree for any node is not more
than one
• the left subtree is balanced
• the right subtree is balanced
AVL Balanced Trees
• The AVL tree was invented by Adelson-Velski and Landis in 1962. The AVL
tree is so named in honor of its inventors.
• The AVL tree was the first balanced binary search tree. It is a self-balancing
binary search tree.
• The AVL tree is also known as a height-balanced tree because of its
property that the heights of the two subtrees of a node can differ at most
by one.
• AVL trees are very efficient in performing searching, insertion, and deletion
operations, as they take O(log n) time to perform all these operations.
• The balance factor is calculated as follows:
• Balance Factor = Height(Left sub-tree) – Height(Right sub-tree)
AVL Rotations
• Rotation is done when the balance factor of the node
becomes disturbed after inserting a new node.
• We know that the new node which is inserted will always
have a balance factor of 0, as it will be the leaf node.
• Hence, the nodes whose balance factors will be disturbed
are the ones which lie in the path of the root node to the
newly inserted node.
• So, we will perform the rotation process only on those nodes
whose balance factors will be disturbed.
• In the rotation process, our first work is to find the critical
node in the AVL tree.
• The critical node is the nearest ancestor node from the
newly inserted node to the root node which does not has a
balance factor of -1, 0, or 1.
AVL Rotations
• There are four types of rotations which are:
1. Left-Left Rotation (LL Rotation) – New node is inserted in the left subtree of
the left subtree of the critical node.
2. Right-Right Rotation (RR Rotation) – New node is inserted in the right
subtree of the right subtree of the critical node.
3. Right-Left Rotation (RL Rotation) – New node is inserted in the left subtree
of the right subtree of the critical node.
4. Left-Right Rotation (LR Rotation) – New node is inserted in the right
subtree of the left subtree of critical node.
LL Rotation
• The LL rotation is also known as the Left-Left rotation, as the new
node is inserted in the left subtree of the left subtree of the critical
node.
• It is a single rotation.
RR Rotation
• The RR rotation is also known as a Right-Right rotation, as the new
node is inserted in the right subtree of the right subtree of the critical
node.
• It is also a single rotation.
RL Rotation
• The RL rotation is also known as a Right-Left rotation, as the new
node is inserted in the left subtree of the right subtree of the critical
node.
• It is a double rotation.
LR Rotation
• The LR rotation is also known as a Left-Right rotation, as the new
node is inserted in the right subtree of the left subtree of the critical
node.
• It is also a double rotation.
Operations on an AVL Tree
1. Search a node in an AVL tree
2. Insertion of a node in an AVL tree
3. Deletion of a node from an AVL tree.
Search Operation in AVL Tree
• In the AVL tree, the search operation is executed • Step 6: If the search element is smaller,
with a time complexity O (log n). then continue the search process in the left
sub-tree.
• The search operation is conducted in the same
way as the search operation of the BST. • Step 7: If the search element is larger, then
continue the search process in the right
• Steps: sub-tree.
• Step 1: Start • Step 8: Repeat the same as long as we find
• Step 2: Read the search element from the the exact element or we finish with a leaf
user. node.
• Step 3: Next, compare the search element • Step 9: If we reach the node with a search
to the root node value in the tree. value, then display “Element is Searched”
• Step 4: If both are equal, then display and terminate the function.
“Element is Searched” and wind up the • Step 10: If we reach a leaf node and it is
search function. also not matching, then display “Element is
• Step 5: If both are not equal, then verify not Searched” and terminate the function.
whether a search element is smaller or • Step 11: Stop.
greater than that of the parent node value.
Insertion Operation in AVL Tree
• In the AVL tree, the insertion activity is carried out with O (log n) time
complexity.
• In the AVL Tree, the new node is always inserted as a leaf node.
• Steps:
• Step 1: Start
• Step 2: Insert the new item in the tree with the BST insertion logic.
• Step 3: Once inserted, check the balance factor for each node.
• Step 4: If the balance factor of every node is 0 or 1 or −1, then go to step 6.
• Step 5: If the balance factor of a node is not 0 or 1 or −1, then the tree is said to be
unbalanced. Then perform the appropriate rotation technique to achieve balance. In
addition, get on with the next operation.
• Step 6: Stop.
Example: Insertion of the following nodes in an AVL tree:
55, 66, 77, 15, 11 and 33
Deletion Operation in AVL Tree
• In the AVL Tree, the deletion operation is similar to the deletion
operation in BST.
• However, after each deletion operation, we must necessary to check
with the balance factor condition.
• If the tree is balanced after deletion, then go to the next operation
otherwise perform the proper rotation to make the tree balanced.
Advantages/Disadvantages of AVL Tree
• Advantages
• When compared to the number of insertion and deletions, the number of
times a search operation is carried out is relatively large. The use of an AVL
tree always results in superior performance.
• The main objective here is to keep the tree balanced at all times. Such
balancing causes the depth of the tree to remain at its minimum, reducing the
overall cost of search.
• AVL tree gives the best performance for dynamic tables.
• Disadvantages
• Insertion and deletion of any node from an AVL tree become complicated.
Huffman Code Construction
• A method for the construction of minimum redundancy codes.
• Huffman code is a type of optimal prefix code that is commonly used
for lossless data compression.
• The algorithm has been developed by David A. Huffman.
• The technique works by creating a binary tree of nodes.
• The nodes count depends on the number of symbols.
• The main idea is to transform plain input into variable-length code.
• As in other entropy encoding methods, most common symbols are
generally represented using fewer bits than less common symbols.
Huffman Encoding
• A type of variable-length encoding that is based on the
actual character frequencies in a given document.
• Huffman encoding uses a binary tree:
• To determine the encoding of each character.
• To decode an encoded file that is to decompress a compressed
file, putting it back into ASCII.
• Leaf nodes are characters.
• Left branches are labeled with a 0, and right branches are
labeled with a 1.
• If you follow a path from root to leaf, you get the encoding
of the character in the leaf.
• Example: 101 = ‘j’ and 01 = ‘m’.
• Characters that appear more frequently are nearer to the
root node, and thus their encodings are shorter and require
less memory.
Building Huffman Tree
1. First reading through the text, determine the frequencies of each
character.
2. Create a list of nodes with character and frequency pairs for each
character within the text sorted by frequency.
3. Remove and combining the nodes with the two minimum frequencies,
creating a new node that is their parent.
– The left child with the lowest frequency node.
– The right child with the second lowest frequency node.
– The frequency of the parent node has the sum of the frequencies of its children,
which is the left and right child.
4. Add the parent to the list of nodes maintaining the sorted order of the list.
5. Repeat steps 3 and 4 as long as there is only a single node in the list, which
will be the root of the Huffman tree.
B-Tree
• A B-tree is a specialized multi-way tree which is widely used for disk access.
• The B-tree was developed in 1970 by Rudolf Bayer and Ed McCreight.
• In a B-tree each node may contain large number of keys.
• A B-tree is designed to store a large number of keys in a single node so that the
height remains relatively small.
• A B-tree of order m has all the properties of a multi-way search tree.
• In addition, it has the following properties:
• 1. All leaf nodes are at the bottom level or at the same level.
• 2. Every node in a B-tree can have at most m children.
• 3. The root node can have at least two children if it is not a leaf node, and it can obviously
have no children if it is a leaf node.
• 4. Each node in a B-tree can have at least (m/2) children except the root node and the leaf
node.
• 5. Each leaf node must contain at least ceil [(m/2) – 1] keys.
B-Tree
• For example – A B-tree of order 5 can have at least ceil [5/2] = 3
children and ceil [(5/2) – 1] = 2 keys. Obviously, the maximum number
of children a node can have is 5. Each leaf node must contain at least
2 keys.
Operations on a B-Tree
• A B-tree stores sorted data, and we can perform various operations
on it which are:
• Inserting a new element in a B-tree
• Deleting an element from a B-tree
• Search Operation in B-Tree
Insertion in a B-Tree
• Insertions in a B-tree are done at the leaf-node level.
• The following are the steps to insert an element in a B-tree:
• Step 1 – In Step 1, we will search the B-tree to find the leaf node where the
new value or key is to be inserted.
• Step 2 – Now, if the leaf node is full, that is, it already contains (m – 1) keys,
then follow these steps:
• i. Insert the new key into the existing set of keys in order.
• ii. Now, the node is split into two halves.
• iii. Finally, push the middle (median) element upward to its parent node. Also, if the
parent node is full, then split the parent node by following these steps.
• Step 3 – If the leaf node is not full, that is, it contains (m – 1) keys, then insert
the new key into the node, keeping the elements of the node in order.
Construct a B-tree of order 5 and insert the following values into it:
Values to be inserted – B, N, G, A, H, E, J, Q, M, D, V, L, T, Z
• Since order = 5, thus we can store at least 3 values and at most 4 values in
a single node. Hence, we will insert B, N, G, A into the B-tree in sorted
order.
• Finally, Z is to be inserted. It will be inserted into the right subtree. Hence, the last node will split
into two halves, and the middle element, that is, T, will push up to the root node.
Consider the following B-tree of order 5 and
insert 81, 7, 49, 61 and 30 in it.
Deletion in a B-Tree
• Deletion of keys in a B-tree also first requires traversal in the B-tree,
that is, after reaching a particular node, we can come across two
cases which are:
• 1. Node is a leaf node.
• 2. Node is not a leaf node.
Deletion in a B-Tree - Node is a leaf node
• If the node has more than minimum number of keys, then deletion
can be done very easily.
• But if the node has a minimum number of keys, then first we will
check the number of keys in the adjacent leaf node.
• If the number of keys in the adjacent node is more than the minimum
number of keys, then the first key of the adjacent leaf node will go to
the parent node, and the key present in the parent node will be
combined together in a single node.
• Now if the parent node also has less than the minimum number of
keys, then the same steps will be repeated until we get a node which
has more than the minimum number of keys present in it.
Deletion in a B-Tree - Node is not a leaf node
• In this case the key from the node is deleted, and its place will be
occupied by either its successor or predecessor key.
• If both predecessor and successor nodes have keys less than the
minimum number, then the keys of the successor and predecessor
are combined.
Consider the following B-tree of order 5 and
delete the values 95, 200, 176, and 70 from it.
Search Operation in B-Tree
• In a B-tree, the search function is the • Step 4: If both are equal, then display
same as that of BST. “Given node is searched” and wind up the
function.
• In a B-Tree, the search process starts from • Step 5: If both are not matching, then verify
the root node, but every time we make whether a search element is smaller or
an n-way decision where n is the total larger than that key value.
number of children that node has. • Step 6: If the search element is smaller,
then continue the search process in the left
• In a B-tree, the search operation is sub-tree.
performed with O (log n) time complexity. • Step 7: If the search element is larger, then
• The search procedure is carried out as compare with the next key value in the
same node and repeat steps 3, 4, 5 and 6
follows: until we found an exact match or
• Step 1: Start. comparison completed with the last key
• Step 2: First, read the search element from value in a leaf node.
the end user. • Step 8: If we completed with last key value
• Step 3: Then compare, the search element in a leaf node, then display “Element is not
with the first key value of the root node in found” and terminate the function.
the tree. • Step 9: Stop.
Application of a B-Tree
• The main application of a B-tree is the organization of large amount of
data or huge collection of records into a file structure. A B-tree should
search the records very efficiently and all the operations such as
insertion, deletion, searching, and so on should be done very
efficiently; therefore, the organization of records should be very good.
Game Tree
• A game tree is a type of recursive search function that examines all
possible moves of a strategy game, and their results, in an attempt to
ascertain the optimal move.
• They are very useful for Artificial Intelligence in scenarios that do not
require real-time decision making and have a relatively low number of
possible choices per play.
• The most commonly-cited example is chess, but they are applicable
to many situations.
Game Tree
• Game trees are generally used in board games to determine the best possible move.
• The idea is to start at the current board position, and check all the possible moves the
computer can make.
• Then, from each of those possible moves, to look at what moves the opponent may
make. Then to look back at the computer’s.
• Ideally, the computer will flip back and forth, making moves for itself and its opponent,
until the game's completion.
• It will do this for every possible outcome, effectively playing thousands (often more) of
games.
• From the winners and losers of these games, it tries to determine the outcome that gives
it the best chance of success.
• This can be likened to how people think during a board game - for example "if I make this
move, they could make this one, and I could counter with this" etc.
Game Tree