Unit Seven
Unit Seven
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.
• 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
• 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
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
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.
● 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)