Trees 1
Trees 1
We read the linear data structures like an array, linked list, stack and queue
in which all the elements are arranged in a sequential manner. The
different data structures are used for different kinds of data.
a Tree.
way.
LET'S UNDERSTAND SOME KEY POINTS
OF THE TREE DATA STRUCTURE.
a root node.
type.
known as siblings.
Leaf Node:- The node of the tree, which doesn't
have any child node, is called a leaf node.
A leaf node is the bottom-most node of the
tree.
There can be any number of leaf nodes
present in a general tree.
Leaf nodes can also be called external
nodes.
Internal nodes: A node has atleast one child
node known as an internal
Ancestor node:- An ancestor of a node is any
predecessor node on a path from the root to that
node. The root node doesn't have any ancestors. In
the tree shown in the above image, nodes 1, 2, and
5 are the ancestors of node 10.
Descendant: The immediate successor of the
given node is known as a descendant of a node. In
the above figure, 10 is the descendant of node 5.
PROPERTIES OF TREE DATA STRUCTURE
Recursive data structure: The tree is also known as
a recursive data structure.
A tree can be defined as recursively because the
distinguished node in a tree data structure is known as
a root node.
The root node of the tree contains a link to all the
roots of its subtrees.
The left subtree is shown in the yellow color in the
below figure, and the right subtree is shown in the red
color.
The left subtree can be further split into subtrees
shown in three different colors.
Recursion means reducing something in a self-similar
manner. So, this recursive property of the tree data
structure is implemented in various applications.
Number of edges: If there are n nodes, then there
would n-1 edges. Each arrow in the structure
represents the link or path. Each node, except the
root node, will have atleast one incoming link known
as an edge. There would be one link for the parent-
child relationship.
Depth of node x: The depth of node x can be
defined as the length of the path from the root to the
node x. One edge contributes one-unit length in the
path. So, the depth of node x can also be defined as
the number of edges between the root node and the
node x. The root node has 0 depth.
Height of node x: The height of node x can be
defined as the longest path from the node x to the
leaf node.
Based on the properties of the Tree data structure, trees are
classified into various categories.
1. Implementation of Tree
Binary tree: Here, binary name itself suggests two numbers, i.e., 0 and 1.
In a binary tree, each node in a tree can have utmost two child nodes. Here, utmost
means whether the node has 0 nodes, 1 node or 2 nodes.
Binary Search tree: Binary search tree is a non-linear
data structure in which one node is connected to n number of
nodes.
It is a node-based data structure. A node can be represented
in a binary search tree with three fields, i.e., data part, left-
child, and right-child.
A node can be connected to the utmost two child nodes in a
binary search tree, so the node contains two pointers (left
child and right child pointer).
Every node in the left subtree must contain a value less than
the value of the root node, and the value of each node in the
right subtree must be bigger than the value of the root node.
A node can be created with the help of a user-defined data type
known as struct, as shown below:
struct node
int data;
The above is the node structure with three fields: data field, the
second field is the left pointer of the node type, and the third
field is the right pointer of the node type.
AVL tree
In router algorithms
Syntax tree
compression algorithms.
Priority Queue is another application of
Inserting an element.
Removing an element.
Searching for an element.
Traversing an element.
TREE TRAVERSAL
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
1. In-order Traversal
2. Pre-order Traversal
3. 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 OF INORDER TRAVERSAL
Algorithm: INORD(INFO, LEFT, RIGHT,
4. Repeat Steps 5 to 7 while PTR ≠
ROOT)
A binary tree is in memory. This algorithm NULL: [Backtracking]
does an inorder traversal of T, applying an
5. Apply PROCESS to INFO[PTR]
operation PROCESS to each of its nodes. An
array STACK is used to temporarily hold the 6. [Right Child?] If RIGHT[PTR] ≠
addresses of nodes.
NULL, then :
1. [Push NULL onto STACK and initialize
PTR] Set PTR:= RIGHT[PTR].
Set TOP:=1, STACK[1]:= NULL and
Go to Step 3
PTR:=ROOT.
2. Repeat while PTR ≠ NULL: [End of If structure]
[Pushes left most path onto STACK]
7. Set PTR:= STACK[TOP] and
(a) Set TOP:= TOP+1 and
STACK[TOP]:=PTR [Saves node] TOP:=TOP-1 [Pops Nodes]
(b) Set PTR:= LEFT[PTR]
[End of Step 4 loop]
[update PTR]
[End of loop] 8. Exit
3. Set PTR:= STACK[TOP] and
TOP:=TOP-1 [Pops node from STACK]
PRE-ORDER TRAVERSAL
In this traversal method, the root node is visited first,
then the left subtree and finally the right subtree.
A→B→D→E→C→F
→G
ALGORITHM OF PREORDER TRAVERSAL
Algorithm: PREORD(INFO, LEFT, [Push on STACK]
RIGHT, ROOT) Set TOP:= TOP+1, and
A binary tree is in memory. This STACK[TOP]:=RIGHT[PTR]
algorithm does an preorder traversal of [End of If structure]
T, applying an operation PROCESS to
5. [Left Child?]
each of its nodes. An array STACK is
used to temporarily hold the addresses If LEFT[PTR] ≠ NULL, then :
of nodes. Set PTR:=LEFT[PTR]
1. [Push NULL onto STACK and Else:
initialize PTR] [Pop from stack]
Set TOP:=1, STACK[1]:= Set PTR:= STACK[TOP]
NULL and PTR:=ROOT. and TOP:=TOP-1
2. Repeat Steps 3 to 5 while PTR ≠ [End of If structure]
NULL: [End of Step 2 loop]
3. Apply PROCESS to INFO[PTR] 6. Exit
4. [Right Child?]
If RIGHT[PTR] ≠ NULL, then :
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.
D→E→B→F→G→C→A
ALGORITHM OF POSTORDER TRAVERSAL
Algorithm: POSTORD(INFO, [End of If structure]
5 Set PTR:=LEFT[PTR] [Update
LEFT, RIGHT, ROOT) pointer PTR]
A binary tree is in memory. This algorithm
[End of Step 2 loop]
does an postorder traversal of T, applying an
operation PROCESS to each of its nodes. An 6. Set PTR:=STACK[TOP] and TOP:=
array STACK is used to temporarily hold the TOP-1
addresses of nodes. [Pops node from STACK]
1. [Push NULL onto STACK and initialize 7 Repeat while PTR>0:
PTR] (a) Apply PROCESS to INFO[PTR]
Set TOP:=1, STACK[1]:= NULL and (b) Set PTR:= STACK[TOP] and
PTR:=ROOT. TOP=TOP-1
2. [Push left-most path onto STACK] [Pops node from STACK]
Repeat Steps 3 to 5 while PTR ≠ NULL: 8 If PTR<0 then:
3. Set TOP:=TOP+1 and STACK[TOP]:= PTR (a) Set PTR:= -PTR
[Pushes PR on STACK] (b) Go to Step 2
4. If RIGHT[PTR] ≠ NULL, then : [Push [End of If structure]
on STACK]
9. Exit
Set TOP:= TOP+1, and STACK[TOP]:= -
RIGHT[PTR]
BINARY SEARCH TREE
Now, the creation of binary search tree is completed. After that, let's
move towards the operations that can be performed on Binary search
tree.
We can perform insert, delete and search operations on the binary search
SEARCHING IN BINARY SEARCH
TREE
Searching means to find or locate a specific element or node in a
data structure. In Binary search tree, searching a node is easy
because elements in BST are stored in a specific order. The steps
of searching a node in Binary Search tree are listed as follows -
1. First, compare the element to be searched with the
root element of the tree.
2. If root is matched with the target element, then
return the node's location.
3. If it is not matched, then check whether the item is
less than the root element, if it is smaller than the
root element, then move to the left subtree.
4. If it is larger than the root element, then move to
the right subtree.
5. Repeat the above procedure recursively until the
match is found.
6. If the element is not found or not present in the
tree, then return NULL.
Now, let's understand the searching in binary tree using an example.
We are taking the binary search tree formed above. Suppose we have
to find node 20 from the below tree.
Step1:
Step2:
Step3:
PROCEDURE FOR SEARCHING
Procedure1: FIND(INFO, LEFT, RIGHT, ROOT, ITEM, LOC,
PAR)
A binary searh tree T is in memory and an item of information is
given. This procedure finds the location LOC of ITEM in T and
also the location PAR of the parent of ITEM. There are three
special cases:
(i) LOC =NULL and PAR=NULL will indicate that the tree is
empty.
(ii) LOC≠ NULL and PAR=NULL will indicate that ITEM is the
root of T.
(iii) LOC=NULL and PAR≠ NULL will indicate that ITEM is not
in T and can be added to T as a child of the node N with
location PAR
1. [Tree empty?]
2. After that, replace that node with the inorder successor until
the target node is placed at the leaf of tree.
3. And at last, replace the node with NULL and free up the
allocated space.
The inorder successor is required when the right child of the
node is not empty. We can obtain the inorder successor by
finding the minimum element in the right child of the node.
We can see the process of deleting a node with two children from BST in the
below image. In the below image, suppose we have to delete node 45 that is the
root node, as the node to be deleted has two children, so it will be replaced
with its inorder successor. Now, node 45 will be at the leaf of the tree so that it
can be deleted easily.
PROCEDURES & ALGORITHMS OF DELETION IN BST
Procedure2: CASEA (INFO, LEFT, RIGHT,
Else
ROOT, LOC, PAR)
Set CHILD:= RIGHT[LOC]
This procedure deletes the node N at location
LOC where N does not have two children. The [End of If structure]
pointer PAR gives the location of the parent of N 2. If PAR ≠ NULL then:
or else PAR=NULL indicates that N is the root
If LOC = LEFT[PAR], then:
node. The pointer CHILD gives the location of
Set LEFT[PAR]:= CHILD
the only child of N or else CHILD=NULL
Else:
indicates N has no children.
Set RIGHT[PAR] := CHILD
1. [Initializes CHILD]
[End of If structure]
If LEFt[LOC]= NULL and RIGHT[LOC]=
NULL then: 3. Return
INFO[NEW]:= ITEM
THE COMPLEXITY OF THE BINARY SEARCH TREE
1. Time Complexity:- Where 'n' is the number of nodes in the given tree.
Operations Best case time Average case time Worst case time
complexity complexity complexity
Insertion O(log n) O(log n) O(n)
Deletion O(log n) O(log n) O(n)
Search O(log n) O(log n) O(n)
2. Space Complexity
Insertion O(n)
Deletion O(n)
Search O(n)