Binary Trees-Unit 4
Binary Trees-Unit 4
Data Structure
A data structure is a technique of storing and organizing the data in such a way that the data
can be utilized in an efficient manner.
Based on the organizing method of data structure, data structures are divided into two types.
Linear data structure:A linear data structure is a structure in which the elements
are stored sequentially, and the elements are connected to the previous and the next
element. As the elements are stored sequentially, so they can be traversed or accessed
in a single run. The implementation of linear data structures is easier as the elements
are sequentially organized in memory. The data elements in an array are traversed one
after another and can access only one element at a time.
Non Linear Data Structures: The data structure where data elements are not
organized sequentially is called non linear data structure. In other words, A data elements
of the non linear data structure could be connected to more than one elements to reflect a
special relationship among them. All the data elements in non linear data structure can
not be traversed in single run.
A tree is collection of nodes where these nodes are arranged hierarchically and form a
parent child relationships. A Graph is a collection of a finite number of vertices and an
edges that connect these vertices. Edges represent relationships among vertices that
stores data elements.
Binary Trees:-Binary Tree is a special data structure used for data storage
purposes. A binary tree has a special condition that each node can have a maximum of
two children. A binary tree has the benefits of both an ordered array and a linked list
as search is as quick as in a sorted array and insertion or deletion operation are as fast
as in linked list.
Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root per tree and one
path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child − The node below a given node connected by its edge downward is called its child
node.
• Leaf/External node − The node which does not have any child node is called the leaf node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root node is at level 0,
then its next child node is at level 1, its grandchild is at level 2, and so on.
• keys − Key represents a value of a node based on which a search operation is to be carried
out for a node.
• Internal node: Node with atleast one children.
• Height of a node: Number of edges from the node to the deepest leaf. Height of the tree is
the height of the root.
• Sibling: The nodes that have the same parent are known as siblings.
• Ancestor node:- An ancestor of a node is any predecessor node on a path from the root to
that node. The root node doesn't have any ancestors. In the tree shown in the above image,
nodes B, and D are the ancestors of node I.
Properties of a Tree
• A tree can contain no nodes or it can contain one special node called
the root with zero or more subtrees.
• Every edge of the tree is directly or indirectly originated from the root.
• Every child has only one parent, but one parent can have many children.
Types of Trees
1. General tree
2. Binary tree
3. Binary search tree
4. Extended Binary Tree (OR) 2-Tree
6. AVL tree
7. 2-3 Tree
8. Heap Tree
9. Forest Tree
10. Red-black tree
11.B-tree
1.General tree:-
• A general tree is defined as a non-empty finite set T of elements called nodes.
• the tree contains a root element or a root node.
• the remaining elements of tree from an order collection of zero or disjoint trees
T1,T2,T3 ......Tm.
• The trees T1,T2,T3 ......Tm are called subtrees of the root element T.
• And the roots of T1,T2,T3.......Tm are called successors of
the roots.
As show the figuer ,A is root node or parent node which has B,C,D are the chiled
nodes or subtrees and E,F,G,H,I,J,K,M,N are know as the successors of the root A.
Examples Family tree, Folder Structure.
2.Binary tree:- Binary tree is the type of tree in which each parent can have at
most two children. The children are referred to as left child or right child. This is one
of the most commonly used trees. When certain constraints and properties are
imposed on Binary tree it results in a number of other widely used trees like BST
(Binary Search Tree), AVL tree, RBT tree etc.
3. Binary search tree:- Binary Search Tree (BST) is an extension of Binary tree with
some added constraints. In BST, the value of the left child of a node must be smaller
than or equal to the value of its parent and the value of the right child is always larger
than or equal to the value of its parent. This property of Binary Search Tree makes it
suitable for searching operations as at each node we can decide accurately whether the
value will be in left subtree or right subtree. Therefore, it is called a Search Tree.
This structure enables one to search for and find on element with an average running
time=o(log2n).
Fig 3: Binary Search Tree.
• A binary tree can be converted to an extended binary tree by adding new nodes to
its leaf nodes or to the nodes which have only one child.
• these new nodes are added in such a way that all the nodes in the resultant tree
have either zero or two children.
ii) Every node in a tree has exactly one parent except the root node.
iv) For a binary tree of height 'h' the maximum number of nodes can be (2h+1 -
1).
• Using threaded binary tree representation, we can reuse that empty links by making
some threads.
• if one node has some vacant left or right child area that will be used as thread.
• In left threaded node if some node has no left child, then the left pointer will point
to its inorder predecessor.
• In Right threaded node if some node has no Right child, then the Right pointer will
point to its inorder Successor.
6.AVL tree:- AVL tree is a self-balancing binary search tree. The name AVL is
given on the name of its inventors Adelson-Velshi and Landis. This was the first
dynamically balancing tree. In AVL tree, each node is assigned a balancing factor
based on which it is calculated whether the tree is balanced or not. In AVL tree, the
heights of children of a node differ by at most 1. The valid balancing factor in AVL tree
are 1, 0 and -1. When a new node is added to the AVL tree and tree becomes
unbalanced then rotation is done to make sure that the tree remains balanced. The
common operations like lookup, insertion and deletion takes O(log n) time in AVL
tree. It is widely used for Lookup operations.
Properties:-
2. Self-balancing.
3. Each node stores a value called a balance factor which is the difference in height
between its left subtree and right subtree.
After performing insertions or deletions, if there is at least one node that does not have a
balance factor of -1, 0 or 1 then rotations should be performed to balance the tree (self-
balancing).
7.2-3 Trees:-
• The insertion and deletion in an AVL tree involves many rotations to make it a
balanced tree .This makes the entire operation complicated.
• To eliminate this complication, a data structure called 2-3 tree can be used.
• A 2-3 tree is defined as a tree data structure where every node with children (internal
node) has either two children (2-node) as well as one data element or three children (3-
nodes) as well as two data elements.
• If the internal node has one data element and two children then itis called as 2-node.
• If the internal node has two data elements and one child then it is called as 3-nodes.
8.Heap Tree:-
• Heap is a complete binary Tree.
i) if the value present at any node is greater than all its children, then the tree is
called the max-heap or descending heap.
ii)if the value present at any node is smaller than all its children, then the tree is
called the min-heap or ascending heap.
9.Forest Tree:-
• A forest is a set of several trees that are not linked to each other.
Example:-
Properties:-
2. Self-balancing.
7. Every path from a given node to any of its leaf nodes must go through the same
number of black nodes.
11. B-tree:- B-tree is a balanced m-way tree where m defines the order of the tree.
Till now, we read that the node contains only one key but b-tree can have more than
one key, and more than 2 children. It always maintains the sorted data. In binary tree,
it is possible that leaf nodes can be at different levels, but in b-tree, all the leaf nodes
must be at the same level.
The root node must contain minimum 1 key and all other nodes must contain
atleast ceiling of m/2 minus 1 keys..
B-tree
Property-01
Example-
Consider the following binary tree-
Here,
• Number of leaf nodes = 3
• Number of nodes with 2 children = 2
Property-02
Example-
Maximum number of nodes in a binary tree of height 3
= 23+1 – 1
= 16 – 1
= 15 nodes
Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted =
15.
Property-03
In any binary tree, the maximum number of nodes on level l is 2l where l≥0
Proof:
• In binary tree , length of the binary tree is l . The root node contains any one node
on level 0.
• Hence , the maximum number of nodes on level 0 is 1→2°=1
• The maximum number of nodes on level 1 is 2→2¹=2
• The maximum number of nodes on level 2 is 4→2²=4
• Same like these. The maximum number of nodes on level l=i is 2i⇒2l=2i
• Hence , the maximum number of nodes on level l is 2l.
Fig:
Property-04
For any non-empty binary tree, if n is the number of nodes and e is the number of
edges .
Then, n=e+1
Proof:
• For n=1, e=0 then, n=e+1=0+1 ⇒n=1
• For n=2, e=1 then , n=e+1=1+1⇒n=2
• For n=3, e=2 then, n=e+1=2+1 ⇒n=3
Property -05
For any non-empty binary tree T , if n0 is the number of leaf nodes (degree=0) and n2
is the number of internal nodes(degree=2), then n0=n2+1
Proof:
• Let n be the total number of nodes in binary tree T . ni be the number of nodes
having degree i.
Since 0≤i≤2
we have,
⇒ n=n0+n1+n2 ........ (i)
If e is the number of edges in T , then
⇒ e=n0×0+n1×1+n2×2
⇒ e=n1+2n2 ......... (ii)
Since . n=e+1 ................ (iii)
• For non-empty binary tree T, if n is number of nodes and e is number of edges.
1. Array Representation
2. Linked List Representation
To store binary tree in a linear array, you need to consider the positional indexes of the nodes. This
indexing must be considered starting with 1 from the root node going from left to right as you go
down from one level to other
Assigning of indexes is done in this way-
These rules are used to store the tree of the above example in an array
If a binary tree contains less number of elements by is deep In structure, the memory
underutilization is a major
To represent a binary tree of depth 'n' using array representation, we need one dimensional array
with a maximum size of 2n + 1.
In linked and dynamic representation, the linked list data structure is used. Each node
constitutes of a data part and two link parts. The two link parts store address of left
and right child nodes. Data part is used to store the information about the binary tree
element. This is a better representation as nodes can be added or deleted at any
location. Memory utilization is better in this binary tree representation.
Operations on a Binary Search Tree:-
Following are the basic operations of a tree −
• Search − Searches an element in a tree.
• Insert − Inserts an element in a tree.
• Pre-order Traversal − Traverses a tree in a pre-order manner.
• In-order Traversal − Traverses a tree in an in-order manner.
• Post-order Traversal − Traverses a tree in a post-order manner.
• Deletion-Deletion an element in tree.
Node
Define a node having some data, references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
creates an empty tree:-Initially an empty tree without any nodes is created. The
variable/identifier which must point to the root node is initialized with a NULL value.
Insert Operation:-
The very first insertion creates the tree. Afterwards, whenever an element is to be
inserted, first locate its proper location. Start searching from the root node, then if the
data is less than the key value, search for the empty location in the left subtree and
insert the data. Otherwise, search for the empty location in the right subtree and
insert the data.
Algorithm:-
1. Create a new BST node and assign values to it.
2. insert(node, key)
i) If root == NULL,
return the new node to the calling function.
Deletion:-
To delete the given node from the binary search tree(BST), we should follow the below rules.
1.Leaf Node
If the node is leaf (both left and right will be NULL), remove the node directly and free its
memory.
If the node has only right child (left will be NULL), make the node points to the right node and
free the node.
If the node has only left child (right will be NULL), make the node points to the left node and
free the node.
Algorithm:-
* If the node becomes NULL, it will return NULL
* Two possible ways which can trigger this case
* 1. If we send the empty tree. i.e root == NULL
* 2. If the given node is not present in the tree.
if(root == NULL)
return NULL;
* If root->key < val. val must be present in the right subtree
* So, call the above remove function with root->right
else
* Case 1: Leaf node. Both left and right reference is NULL * replace
the node with NULL by returning NULL to the calling pointer.
* free the node
else
int rightMin = getRightMin(root->right);
root->key = rightMin;
root->right = removeNode(root->right,rightMin);
In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the right sub-tree. We
should always remember that every node may represent a subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an ascending
order.
We start from A, and following in-order traversal, we move to its left subtree B. B is also traversed in-order.
The process goes on until all the nodes are visited. The output of inorder traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm:-
If tree! =0 then
Inorder(tree->leftchild);
Visit(tree);
Inorder(tree->rightchild);
Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally the right
subtree.
We start from A, and following pre-order traversal, we first visit A itself and then move to its left
subtree B. B is also traversed pre-order. The process goes on until all the nodes are visited. The
output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.
Algorithm:-
If tree! =0 then
Visit(tree);
Preorder(tree->leftchild);
Preorder(tree->rightchild);
Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we traverse the left
subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left subtree B. B is also
traversed post-order. The process goes on until all the nodes are visited. The output of post-order
traversal of this tree will be −
D→E→B→F→G→C→A
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.
Algorithm:-
If tree! =0 then
Input
The tree which will be created after inputting the values is given below −
Output
Count the Number of Binary Search Trees present in a Binary Tree are: 6
Explanation
we are given with an array of integer values that is used to form a binary tree and we will check
whether there is a binary search tree present in it. Every root node represents the binary search tree
so in the given binary tree we can see that there are 4 leaf nodes and two subtrees which are
forming the BST therefore the count is 6.
The properties that separate a binary search tree from a regular binary .
tree is All nodes of left subtree are less than the root node
All nodes of right subtree are more than the root node.
Both subtrees of each node are also BSTs i.e. they have the above two properties
The binary tree on the right isn't a binary search tree because the right subtree of the
node "3" contains a value smaller than it.
There are three basic operations that you can perform on a binary
search tree: Search, Insert, Delete
Search Operations
If the value is below the root, we can say for sure that the value is not in the right
subtree; we need to only search in the left subtree and if the value is above the
root, we can say for sure that the value is not in the left subtree; we need to only
search in the rightsubtree.
if root== NULL
Print number not
found Else
Print number found at root
Insertion
Insert function is used to add a new element in a binary search tree at appropriate
location. Insert function is to be designed in such a way that, it must node violate
the property of binary search tree at each value.
Set the data part to the value and set the left and right pointer of node to NULL.
If the item to be inserted, will be the first element of the tree, then the left and
right of this node will point to NULL.
Else, check if the item is less than the root element of the tree, if this is true, then
recursively perform this operation with the left of the root.
If this is false, then perform this operation recursively with the right sub-tree of the
root.
Delete:-
Delete function is used to delete the specified node from a binary search tree.
However, we must delete a node from a binary search tree in such a way, that the
property of binary search tree doesn't violate. There are three situations of deleting a
node from binary search tree.
It is the simplest case, in this case, replace the leaf node with the NULL and simple
free the allocated space.
In the following image, we are deleting the node 85, since the node is a leaf node,
therefore the node will be replaced with NULL and allocated space will be freed.
In the following image, the node 12 is to be deleted. It has only one child. The node
will be replaced with its child node and the replaced node 12 (which is now leaf node)
will simply be deleted.
The node to be deleted has only two child
It is a bit complexed case compare to other two cases. However, the node which is
to be deleted, is replaced with its in-order successor or predecessor recursively
until the node value (to be deleted) is placed on the leaf of the tree. After the
procedure, replace the node with NULL and free the allocatedspace.
In the following image, the node 50 is to be deleted which is the root node of
the tree. The in-order traversal of the tree given below.
replace 50 with its in-order successor 52. Now, 50 will be moved to the leaf of the
tree, which will simply be deleted.
Delete (Tree,Item)
IF TREE = NULL
Write "item not found in the tree"
ELSE IF ITEM < TREE -> DATA
Delete(TREE->LEFT, ITEM)
ELSE IF ITEM > TREE -> DATA
Delete(TREE -> RIGHT, ITEM)
ELSE IF TREE -> LEFT AND TREE -> RIGHT
SET TEMP = find min value Node(TREE -> RIGHT)
SET TREE ->DATA = TEMP ->DATA
Delete(TREE ->LEFT, TEMP ->DATA)
ELSE
SET TEMP = TREE
IF TREE -> LEFT = NULL AND TREE -> RIGHT = NULL
SET TREE = NULL
ELSE IF TREE -> LEFT != NULL
SET TREE = TREE -> LEFT ELSE
SET TREE = TREE -> RIGHT.