Tree
Tree
Binary Tree
A binary Tree is defined as a Tree data structure with at most 2 children. Since each element in
a binary tree can have only 2 children, we typically name them the left and right child.
Tree is a hierarchical data structure. Main uses of trees include maintaining hierarchical data,
providing moderate access and insert/delete operations.
A Binary tree is represented by a pointer to the topmost node (commonly known as the “root”) of the
tree. If the tree is empty, then the value of the root is NULL. Each node of a Binary Tree contains the
following parts:
1. Data
2. Pointer to left child / address of left child
3. Pointer to right child / address of right child
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.
Implementation of Binary Tree:
OPERATION ON BT
1. Create
2. Insert
3. Remove / Delete
4. Search
5. Traversal
1.Depth First Search or DFS
Inorder Traversal
Preorder Traversal
Postorder Traversal
2.Level Order Traversal or Breadth First Search or BFS
3.Boundary Traversal
4.Diagonal Traversal
Auxiliary Operation On Binary Tree:
Finding the height of the tree
Find the level of a node of the tree
Finding the size of the entire tree.
Each node in a binary tree can have at most two child nodes: In a binary tree, each
node can have either zero, one, or two child nodes. If a node has zero children, it is
called a leaf node. If a node has one child, it is called a unary node. If a node has two
children, it is called a binary node.
The node at the top of the tree is called the root node: The root node is the first node
in a binary tree and all other nodes are connected to it. All other nodes in the tree are
either child nodes or descendant nodes of the root node.
Nodes that do not have any child nodes are called leaf nodes: Leaf nodes are the
endpoints of the tree and have no children. They represent the final result of the tree.
The height of a binary tree is defined as the number of edges from the root node to
the deepest leaf node: The height of a binary tree is the length of the longest path from
the root node to any of the leaf nodes. The height of a binary tree is also known as its
depth.
In a full binary tree, every node except the leaves has exactly two children: In a full
binary tree, all non-leaf nodes have exactly two children. This means that there are no
unary nodes in a full binary tree.
In a complete binary tree, every level of the tree is completely filled except for the
last level, which can be partially filled: In a complete binary tree, all levels of the tree
except the last level are completely filled. This means that there are no gaps in the tree
and all nodes are connected to their parent nodes.
In a balanced binary tree, the height of the left and right subtrees of every node
differ by at most 1: In a balanced binary tree, the height of the left and right subtrees
of every node is similar. This ensures that the tree is balanced and that the height of the
tree is minimized.
The in-order, pre-order, and post-order traversal of a binary tree are three
common ways to traverse the tree: In-order, pre-order, and post-order are three
different ways to traverse a binary tree. In-order traversal visits the left subtree, the
node itself, and then the right subtree. Pre-order traversal visits the node itself, the left
subtree, and then the right subtree. Post-order traversal visits the left subtree, the right
subtree, and then the node itself.
2.On the basis of completion of levels, the binary tree can be divided into following types:
1. Complete Binary Tree
2. Perfect Binary Tree
3. Balanced Binary Tree