You are on page 1of 18

Tree represents the nodes connected by edges.

We will discuss binary tree or binary


search tree specifically.
Binary Tree is a special datastructure 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.

Important Terms
Following are the important terms with respect to tree.
 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 − 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.

Binary Search Tree Representation


Binary Search tree exhibits a special behavior. A node's left child must have a value
less than its parent's value and the node's right child must have a value greater than its
parent value.

We're going to implement tree using node object and connecting them through
references.

Tree Node
The code to write a tree node would be similar to what is given below. It has a data part
and references to its left and right child nodes.
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};

In a tree, all nodes share common construct.


BST Basic Operations
The basic operations that can be performed on a binary search tree data structure, are
the following −
 Insert − Inserts an element in a tree/create a tree.
 Search − Searches an element in a tree.
 Preorder Traversal − Traverses a tree in a pre-order manner.
 Inorder Traversal − Traverses a tree in an in-order manner.
 Postorder Traversal − Traverses a tree in a post-order manner.

Height of a Node

The height of a node is the number of edges from the node to the deepest leaf
(ie. the longest path from the node to a leaf node).

Depth of a Node
The depth of a node is the number of edges from the root to the node.

Height of a Tree

The height of a Tree is the height of the root node or the depth of the deepest
node.

Height and depth of each node in a tree

Degree of a Node

The degree of a node is the total number of branches of that node.

Forest

A collection of disjoint trees is called a forest.


Creating forest from a tree

Traversing Binary Trees (Pre order, Post order and In order)

Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −

 In-order Traversal
 Pre-order Traversal
 Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to
print all the values it contains.

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
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

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
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

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
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node.

Binary Search Tree Operations-


 
Commonly performed operations on binary search tree are-
 

 
1. Search Operation
2. Insertion Operation
3. Deletion Operation
 

1. Search Operation-
 

Search Operation is performed to search a particular element in the


Binary Search Tree.

Rules-
 
For searching a given key in the BST,

 Compare the key with the value of root node.


 If the key is present at the root node, then return the root node.
 If the key is greater than the root node value, then recur for the root node’s right
subtree.
 If the key is smaller than the root node value, then recur for the root node’s left
subtree.
 

Example-
 
Consider key = 45 has to be searched in the given BST-
 
 

 We start our search from the root node 25.


 As 45 > 25, so we search in 25’s right subtree.
 As 45 < 50, so we search in 50’s left subtree.
 As 45 > 35, so we search in 35’s right subtree.
 As 45 > 44, so we search in 44’s right subtree but 44 has no subtrees.
 So, we conclude that 45 is not present in the above BST.
 

2. Insertion Operation-
 

Insertion Operation is performed to insert an 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.
 

Example-
 
Consider the following example where key = 40 is inserted in the given BST-
 

 We start searching for value 40 from the root node 100.


 As 40 < 100, so we search in 100’s left subtree.
 As 40 > 20, so we search in 20’s right subtree.
 As 40 > 30, so we add 40 to 30’s right subtree.
 

3. Deletion Operation-
 

Deletion Operation is performed to delete a particular element from


the Binary Search Tree.

 
When it comes to deleting a node from the binary search tree, following three cases are
possible-
 
Case-01: Deletion Of A Node Having No Child (Leaf Node)-
 
Just remove / disconnect the leaf node that is to deleted from the tree.
 
Example-
 
Consider the following example where node with value = 20 is deleted from the BST-
 

 
Case-02: Deletion Of A Node Having Only One Child-
 
Just make the child of the deleting node, the child of its grandparent.
 
Example-
 
Consider the following example where node with value = 30 is deleted from the BST-
 
 
Case-02: Deletion Of A Node Having Two Children-
 
A node with two children may be deleted from the BST in the following 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.
 
Example-
 
Consider the following example where node with value = 15 is deleted from the BST-
 
 
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.
 
Example-
 
Consider the following example where node with value = 15 is deleted from the BST-
 
 
Heap is a special case of balanced binary tree data structure where the root-node key
is compared with its children and arranged accordingly. If α has child node β then −
key(α) ≥ key(β)
As the value of parent is greater than that of child, this property generates  Max Heap.
Based on this criteria, a heap can be of two types −
For Input → 35 33 42 10 14 19 27 44 26 31
Min-Heap − Where the value of the root node is less than or equal to either of its
children.

Max-Heap − Where the value of the root node is greater than or equal to either of its
children.
Both trees are constructed using the same input and order of arrival.

Max Heap Construction Algorithm


We shall use the same example to demonstrate how a Max Heap is created. The
procedure to create Min Heap is similar but we go for min values instead of max
values.
We are going to derive an algorithm for max heap by inserting one element at a time.
At any point of time, heap must maintain its property. While insertion, we also assume
that we are inserting a node in an already heapified tree.
Step 1 − Create a new node at the end of heap.
Step 2 − Assign new value to the node.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.
Note − In Min Heap construction algorithm, we expect the value of the parent node to
be less than that of the child node.
Let's understand Max Heap construction by an animated illustration. We consider the
same input sample that we used earlier.

Max Heap Deletion Algorithm


Let us derive an algorithm to delete from max heap. Deletion in Max (or Min) Heap
always happens at the root to remove the Maximum (or minimum) value.
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with its parent.
Step 4 − If value of parent is less than child, then swap them.
Step 5 − Repeat step 3 & 4 until Heap property holds.

Application of trees

1. Heap is a tree data structure which is implemented using arrays and used to
implement priority queues.
2. B-Tree and B+ Tree : They are used to implement indexing in databases.
3. Syntax Tree: Used in Compilers.
4. K-D Tree: A space partitioning tree used to organize points in K dimensional
space.
5. Trie : Used to implement dictionaries with prefix lookup.
6. Suffix Tree : For quick pattern searching in a fixed text.
As per Wikipedia, following are the common uses of tree.
1.Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms

Infix Expression Prefix Expression Postfix Expression

A+B +AB AB+

A+B*C +A*BC ABC*+


Infix Expression Prefix Expression Postfix Expression

Infix Expression Prefix Expression Postfix Expression

(A + B) * C *+ABC AB+C*

Infix Expression Prefix Expression Postfix Expression

A+B*C+D ++A*BCD ABC*+D+

(A + B) * (C + D) *+AB+CD AB+CD+*

A*B+C*D +*AB*CD AB*CD*+

A+B+C+D +++ABCD AB+C+D+

You might also like