Binary, Trees
Binary, Trees
The node which has a branch from it to any other node is called as a parent
node.
In other words, the node which has one or more children is called as a parent
node.
In a tree, a parent node can have any number of child nodes.
Total number of edges from root node to a particular node is called as depth
of that node.
Depth of a tree is the total number of edges from root node to a leaf node in
the longest path.
Depth of the root node = 0
The terms “level” and “depth” are used interchangeably.
Depth of node A = 0
Depth of node B = 1
Depth of node C = 1
Depth of node D = 2
Depth of node E = 2
Depth of node F = 2
Depth of node G = 2
Depth of node H = 2
Depth of node I = 3
Depth of node J = 3
Subtree-
In a tree, each child from a node forms a subtree recursively.
Every child node forms a subtree on its parent node.
Tree Representations
❖ List Representation
❖ Left Child - Right Sibling Representation
1. List Representation
In this representation, two types of nodes one for representing the node with
data called 'data node' and another for representing only references called
'reference node’.
Start with a 'data node' from the root node in the tree.
Then it is linked to an internal node through a 'reference node' which is
further linked to any other node directly.
This process repeats for all the nodes in the tree.
Left Child - Right Sibling Representation
In this representation, use a list with one type of node which consists of three
fields namely Data field, Left child reference field and Right sibling reference
field.
Data field stores the actual value of a node, left reference field stores the
address of the left child and right reference field stores the address of the
right sibling node.
BINARY TREE
Binary tree is a special tree data structure in which each node can have at
most 2 children.
Thus, in a binary tree,Each node has either 0 child or 1 child or 2 children.
Unlabelled Binary Tree
Labelled Binary Tree
Types of Binary Trees
1. Rooted Binary Tree-
A rooted binary tree is a binary tree that satisfies the following 2 properties-
A binary tree in which every node has either 0 or 2 children is called as a Full
binary tree.
Full binary tree is also called as Strictly binary tree.
Almost Complete Binary Tree-
An almost complete binary tree is a binary tree that satisfies the following 2
properties-
❖ All the levels are completely filled except possibly the last level.
❖ The last level must be strictly filled from left to right.
Skewed Binary Tree-
A skewed binary tree is a binary tree that satisfies the following 2 properties-
❖ All the nodes except one node has one and only one child.
❖ The remaining node has no child.
OR
A skewed binary tree is a binary tree of n nodes such that its depth is (n-1).
Tree Traversal-
Tree Traversal refers to the process of visiting each node in a tree data
structure exactly once.
1. Preorder Traversal-
Algorithm-
Application-
Inorder traversal is used to get infix expression
3. Postorder Traversal-
Algorithm-
Left → Right → Root
1. Traverse the left sub tree
2. Traverse the right sub tree
3. Visit the root Postorder Traversal Shortcut
Pluck all the leftmost leaf nodes one by one.
Applications-
Breadth First Traversal of a tree prints all the nodes of a tree level by level.
Breadth First Traversal is also called as Level Order Traversal.
Application-
Level order traversal is used to print the data in the same order as stored in the array
representation of a complete binary tree.
Binary Search Tree
Binary Search Tree is a special kind of binary tree in which nodes are
arranged in a specific order
In a binary search tree (BST), each node contains-
❖ Only smaller values in its left sub tree
❖ Only larger values in its right sub tree
If three distinct keys are A, B and C, then 5 distinct binary search trees are-
1. Search Operation-
Search Operation is performed to search a particular element in the Binary Search Tree.
Rules-
❖ The insertion of a new key always takes place as the child of some leaf node.
❖ For finding out the suitable leaf node,
❖ Search the key to be inserted from the root node till some leaf node is
reached.
❖ Once a leaf node is reached, insert the key as child of that leaf node.
key = 40 is inserted in the given BST-
Deletion Operation-
To delete a particular element from the Binary Search Tree.
Just make the child of the deleting node, the child of its grandparent.
Deletion Of A Node Having Two Children-
A node with two children may be deleted from the BST in two ways-
Method-01:
❖ Visit to the right subtree of the deleting node.
❖ Pluck the least value element called as inorder successor.
❖ Replace the deleting element with its inorder successor.
❖ An inorder successor of a node in the BST is the next node
in the inorder sequence
Method-02:
❖ Visit to the left subtree of the deleting node.
❖ Pluck the greatest value element called as inorder predecessor.
❖ Replace the deleting element with its inorder predecessor.
M Way Search Tree
A multiway tree is defined as a tree that can have more than two children.
If a multiway tree can have maximum m children, then this tree is called as
multiway tree of order m (or an m-way tree).
As with the other trees that have been studied, the nodes in an m-way tree
will be made up of m-1 key fields and pointers to children.