Bnrytree
Bnrytree
\\A binary tree is a tree data structure in which each node can have at most two children, which
are referred to as the left child and the right child.
\\The topmost node in a binary tree is called the root, and the bottom-most nodes are called leaves.
A binary tree can be visualized as a hierarchical structure with the root at the top and the leaves
at the bottom.
Binary trees have many applications in computer science, including data storage and retrieval,
expression evaluation, network routing, and game AI. They can also be used to implement various
algorithms such as searching, sorting, and graph algorithms.
-Data
In C, we can represent a tree node using structures. In other languages, we can use classes as
part of their OOP feature. Below is an example of a tree node with integer data .
Below is an example of a
left-skewed tree:
2. RIGHT SKEWED BINARY TREE:
Below is an example of
a right-skewed tree:
Real-time applications of Binary Trees:
DOM in HTML.
File explorer.
APPLICATIONS, ADVANTAGES AND Used as the basic data structure in
DISADVANTAGES OF BINARY TREE Microsoft Excel and spreadsheets.
Editor tool: Microsoft Excel and
spreadsheets.
Evaluate an expression
Routing Algorithms
--Sorting algorithms: Binary trees can be used to implement efficient sorting algorithms, such as binary
search tree sort and heap sort.
--Database systems: Binary trees can be used to store data in a database system, with each node
representing a record. This allows for efficient search operations and enables the database system to
handle large amounts of data.
--File systems: Binary trees can be used to implement file systems, where each node represents a directory
or file. This allows for efficient navigation and searching of the file system.
--Compression algorithms: Binary trees can be used to implement Huffman coding, a compression algorithm
that assigns variable-length codes to characters based on their frequency of occurrence in the input data.
--Decision trees: Binary trees can be used to implement decision trees, a type of machine learning
algorithm used for classification and regression analysis.
--Game AI: Binary trees can be used to implement game AI, where each node represents a possible move in
the game. The AI algorithm can search the tree to find the best possible move.
TREE TRAVERSAL TECHNIQUES
Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to
traverse them, trees can be traversed in different ways.
A Tree Data Structure can be traversed in following ways:
-Inorder Traversal
-Preorder Traversal
-Postorder Traversal
-Boundary Traversal
-Diagonal Traversal
TREE TRAVERSAL - INORDER, PREORDER AND POSTORDER
Algorithm Inorder(tree)
1.Traverse the left subtree, i.e., call Inorder(left->subtree)
2.Visit the root.
3.Traverse the right subtree, i.e., call Inorder(right->subtree)
Inorder traversal
First, visit all the nodes in the left subtree
Then the root node
Visit all the nodes in the right subtree
Algorithm Preorder(tree)
Preorder traversal
Visit root node
Visit all the nodes in the left subtree
Visit all the nodes in the right subtree
Uses of Preorder:
Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to
get prefix expressions on an expression tree.
PREORDER TRAVERSAL
POSTORDER TRAVERSAL
Algorithm Postorder(tree)
1.Traverse the left subtree, i.e., call Postorder(left->subtree)
2.Traverse the right subtree, i.e., call Postorder(right->subtree)
3.Visit the root
Postorder traversal
Visit all the nodes in the left subtree
Visit all the nodes in the right subtree
Visit the root node
Uses of Postorder:
Postorder traversal is used to delete the tree. Postorder traversal is also useful to get the
postfix expression of an expression tree
Below is the implementation of the above traversal methods:
POSTORDER TRAVERSAL
LEVEL ORDER TRAVERSAL (BREADTH FIRST SEARCH OR BFS) OF BINARY
TREE
Level Order Traversal technique is defined as a method to traverse a Tree such that all nodes
present in the same level are traversed completely before traversing the next level.
Example:
Input: Output: 1
23
45
LEVEL ORDER TRAVERSAL (NAIVE Find height of tree. Then for each level, run a recursive function by
maintaining current height. Whenever the level of a node matches, print
APPROACH): that node.
We need to visit the nodes in a lower level before any node in a higher level, this idea is quite similar to that of a
queue. Push the nodes of a lower level in the queue. When any node is visited, pop that node from the queue and push
the child of that node in the queue.
This ensures that the node of a lower level are visited prior to any node of a higher level.
Given a binary tree, delete a node from it by making sure that the tree shrinks from the bottom
(i.e. the deleted node is replaced by the bottom-most and rightmost node). This is different from
BST(binary search tree) deletion. Here we do not have any order among elements, so we replace
them with the last element.
Examples :
DELETION IN A BINARY TREE
Algorithm:
--Starting at the root, find the deepest and rightmost node in the binary tree and the node
which we want to delete.
--Replace the deepest rightmost node’s data with the node to be deleted.
--Then delete the deepest rightmost node.
DELETION IN A BINARY TREE