TREE
TREE
TREE
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.
A binary tree is a tree-type non-linear data structure with a maximum of two children for each parent. Every node in a binary
tree has a left and right reference along with the data element. The node at the top of the hierarchy of a tree is called the root
node. The nodes that hold other sub-nodes are the parent nodes.A parent node has two child nodes: the left child and right child.
Hashing, routing data for network traffic, data compression, preparing binary heaps, and binary search trees are some of the
applications that use a binary tree.
Say a binary tree placed at a height equal to 3. In that case, the highest number of nodes for this height 3 stands equal to 15, that
is, (1+2+4+8) = 15. In basic terms, the maximum node number possible for this height h is (20 + 21 + 22+….2h) = 2h+1 -1.
Now, for the minimum node number that is possible at this height h, it comes as equal to h+1.
If there are a minimum number of nodes, then the height of a binary tree would stand aa maximum. On the other hand,
when there is a number of a node at its maximum, then the binary tree m height will be minimum. If there exists around
‘n’ number nodes in a binary tree, here is a calculation to clarify the binary tree definition.
The tree’s minimum height is computed as:
n = 2h+1 -1
n+1 = 2h+1
Taking log for both sides now,
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
h = log2(n+1) – 1
The highest height will be computed as:
n = h+1
h= n-1
In case there exists n number of nodes in any binary tree, the height stands given by log n logn. This is due to the simple reason
that for any given node in any binary tree, there will be two child nodes at the most. This drives us to an explanation to define
binary tree: for every level or every height of any binary tree, the node number must be the same as around half the node numbers
present at the next level.
In the form of an answer to define binary tree, the node number at every level is close to double the node number at its previous
level.
It means, that when a binary tree comes with height h, the number of nodes for define binary tree is n = (2 ^ 0) + (2 ^ 1) + (2 ^ 2)
+ (2 ^ 3) + ….. + (2 ^ (h-1))(20)+(21)+(22)+(23)+…..+(2(h−1))
From the `mathematical induction point for what is a binary tree, this is what we know-
(2 ^ 0) + (2 ^ 1) + (2 ^ 2) + (2 ^ 3) + ….. + (2 ^ {(h-1)}) = (2 ^ h)-1(20)+(21)+(22)+(23)+…..+(2(h−1))=(2h)−1
Hence,
(2 ^ h)-1 = n => 2 ^ h = n + 1 => h = log2(n+1)(2h)−1=n=>2h=n+1=>h=log2(n+1)
Therefore, the minimum height for a binary tree is roughly equal to log(n) roughly. This helps you better understand what is a
binary tree.Also, the minimum number of nodes that are possible at height h of the binary tree can be known by h+1.If the binary
tree comes with an L number for leaf nodes, the height is represented by L + 1.
Types of Binary Trees
1. Full Binary Tree:It is a special kind of a binary tree that has either zero children or
two children. It means that all the nodes in that binary tree should either have two child
nodes of its parent node or the parent node is itself the leaf node or the external node.
In other words, a full binary tree is a unique binary tree where every node except the
external node has two children. When it holds a single child, such a binary tree will not be
a full binary tree. Here, the quantity of leaf nodes is equal to the number of internal
nodes plus one. The equation is like L=I+1, where L is the number of leaf nodes, and I is
the number of internal nodes.
A Binary Search Tree (BST) is a special type of binary tree in which the left child of a node has a value less than the node’s value
and the right child has a value greater than the node’s value. This property is called the BST property and it makes it possible to
efficiently search, insert, and delete elements in the tree.
The root of a BST is the node that has the smallest value in the left subtree and the largest value in
the right subtree. Each left subtree is a BST with nodes that have smaller values than the root and
each right subtree is a BST with nodes that have larger values than the root.
Binary Search Tree is a node-based binary tree data structure that has the following properties:
The left subtree of a node contains only nodes with keys lesser than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
This means everything to the left of the root is less than the value of the root and
everything to the right of the root is greater than the value of the root. Due to this performing, a binary search is very
easy.
The left and right subtree each must also be a binary search tree.
There must be no duplicate nodes(BST may have duplicate values with different handling approaches)
Applications of BST:
Graph algorithms: BSTs can be used to implement graph algorithms, such as in minimum spanning tree algorithms.
Priority Queues: BSTs can be used to implement priority queues, where the element with the highest priority is at the
root of the tree, and elements with lower priority are stored in the subtrees.
Self-balancing binary search tree: BSTs can be used as a self-balancing data structures such as AVL tree and Red-
black tree.
Data storage and retrieval: BSTs can be used to store and retrieve data quickly, such as in databases, where
searching for a specific record can be done in logarithmic time.
Advantages:
Fast search: Searching for a specific value in a BST has an average time complexity of O(log n), where n is the
number of nodes in the tree. This is much faster than searching for an element in an array or linked list, which have a
time complexity of O(n) in the worst case.
In-order traversal: BSTs can be traversed in-order, which visits the left subtree, the root, and the right subtree. This
can be used to sort a dataset.
Space efficient: BSTs are space efficient as they do not store any redundant information, unlike arrays and linked
lists.
Disadvantages:
Skewed trees: If a tree becomes skewed, the time complexity of search, insertion, and deletion operations will be
O(n) instead of O(log n), which can make the tree inefficient.
Additional time required: Self-balancing trees require additional time to maintain balance during insertion and
deletion operations.
Efficiency: BSTs are not efficient for datasets with many duplicates as they will waste space.
What is recursion?
In simple words, recursion is a problem solving, and in some cases, a programming technique that has a very special and
exclusive property. In recursion, a function or method has the ability to call itself to solve the problem. The process of recursion
involves solving a problem by turning it into smaller varieties of itself.
Recursion in stack in data structure is when functions call themselves directly or indirectly.
The process in which a function calls itself could happen directly as well as indirectly. This difference in call gives rise to
different types of recursion, which we will talk about a little later. Some of the problems that can be solved using recursion
include DFS of Graph, Towers of Hanoi, Different Types of Tree Traversals, and others.
Every step repeats itself at a smaller scale in recursion. Hence, all of them are combined to solve the problem. Recursion in the
data structure is one of the most compact and improved strategies for creating function calls or deploying functions in
programming. It helps you to implement many functions and algorithms efficiently, at the same time extending clarity while
executing a recursive algorithm in a code which data structure is used.
The biggest disadvantages of recursion are that there are greater space requirements for recursive functions as compared to an
iterative program because every function call has to remain in the stack until the base condition is met, and there are greater time
requirements, too, because the stack grows after each function call, and the final answer can only be returned after popping all the
elements from the stack.
The idea of threaded binary trees is to make inorder traversal faster and do it without stack and without recursion. A binary tree
is made threaded by making all right child pointers that would normally be NULL point to the inorder successor of the node (if it
exists).
A threaded binary tree is a type of binary tree data structure where the empty left and right child pointers in a binary tree are
replaced with threads that link nodes directly to their in-order predecessor or successor, thereby providing a way to traverse the tree
without using recursion or a stack.
Threaded binary trees can be useful when space is a concern, as they can eliminate the need for a stack during traversal. However,
they can be more complex to implement than standard binary trees.
There are two types of threaded binary trees.
Single Threaded: Where a NULL right pointers is made to point to
the inorder successor (if successor exists)
Double Threaded: Where both left and right NULL pointers are made to point to inorder predecessor and inorder successor
respectively. The predecessor threads are useful for reverse inorder traversal and postorder traversal.
The threads are also useful for fast accessing ancestors of a node.
Following diagram shows an example Single Threaded Binary Tree. The dotted lines represent threads.
0 seconds of 21 secondsVolume 0%
Expression evaluation: Threaded binary trees can be used to evaluate arithmetic expressions in a way that avoids
recursion or a stack. The tree can be constructed from the input expression, and then traversed in-order or pre-order to
perform the evaluation.
Database indexing: In a database, threaded binary trees can be used to index data based on a specific field (e.g. last
name). The tree can be constructed with the indexed values as keys, and then traversed in-order to retrieve the data in
sorted order.
Symbol table management: In a compiler or interpreter, threaded binary trees can be used to store and manage symbol
tables for variables and functions. The tree can be constructed with the symbols as keys, and then traversed in-order or
pre-order to perform various operations on the symbol table.
Disk-based data structures: Threaded binary trees can be used in disk-based data structures (e.g. B-trees) to improve
performance. By threading the tree, it can be traversed in a way that minimizes disk seeks and improves locality of
reference.
Navigation of hierarchical data: In certain applications, threaded binary trees can be used to navigate hierarchical data
structures, such as file systems or web site directories. The tree can be constructed from the hierarchical data, and then
traversed in-order or pre-order to efficiently access the data in a specific order.
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.
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.
Complexity
Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are performed in the same way as they are
performed in a binary search tree. Searching and traversing do not lead to the violation in property of AVL tree. However, insertion
and deletion are the operations which can violate this property and therefore, they need to be revisited.
S Operation Description
N
1 Insertion Insertion in AVL tree is performed in the same way as it is performed in a binary search tree. However, it may
lead to violation in the AVL tree property and therefore the tree may need balancing. The tree can be balanced
by applying rotations.
2 Deletion Deletion can also be performed in the same way as it is performed in a binary search tree. Deletion may also
disturb the balance of the tree therefore, various types of rotations are used to rebalance the tree.
B Tree VS B+ Tree
S B Tree B+ Tree
N
1 Search keys can not be repeatedly stored. Redundant search keys can be present.
2 Data can be stored in leaf nodes as well as internal nodes Data can only be stored on the leaf nodes.
3 Searching for some data is a slower process since data can be Searching is comparatively faster as data can only be found
found on internal nodes as well as on the leaf nodes. on the leaf nodes.
4 Deletion of internal nodes are so complicated and time Deletion will never be a complexed process since element
consuming. will always be deleted from the leaf nodes.
5 Leaf nodes can not be linked together. Leaf nodes are linked together to make the search
operations more efficient.