0% found this document useful (0 votes)
56 views23 pages

Tree

A tree is a hierarchical data structure that organizes data in a parent-child relationship. A tree has a root node at the top with child nodes below, and each child can have its own children. Binary trees are a type of tree where each node has at most two children. Trees can be traversed in different orders like preorder, inorder, and postorder.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views23 pages

Tree

A tree is a hierarchical data structure that organizes data in a parent-child relationship. A tree has a root node at the top with child nodes below, and each child can have its own children. Binary trees are a type of tree where each node has at most two children. Trees can be traversed in different orders like preorder, inorder, and postorder.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

1

Tree
A tree is a simple, acyclic and connected graph.

Tree is a non-linear data structure which organizes data in hierarchical structure.

A tree is a finite set of one or more nodes such that there is a specially designated node
called the root. The remaining nodes are partitioned into 𝑛 ≥ 0 disjoint sets 𝑇1 , 𝑇2 . . . , 𝑇𝑛 ,
where each of these sets is a tree. 𝑇1 , 𝑇2 . . . , 𝑇𝑛 are the sub-trees of the root.

A tree is hierarchical collection of nodes. One of the nodes, known as the root, is at the
top of the hierarchy. Each node can have at most one link coming into it. The node
where the link originates is called the parent node. The root node has no parent. The
links leaving a node (any number of links are allowed) point to child nodes. Trees are
recursive structures. Each child node is itself the root of a sub-tree. At the bottom of the
tree are leaf nodes, which have no children.

Advantages of trees:

1. Trees reflect structural relationships in the data.


2. Trees are used to represent hierarchies.
3. Trees provide an efficient insertion and searching.
4. Trees are very flexible data, allowing to move sub trees around with minimum
effort.

Tree terminology:

1. Root: In a tree, the first node is called as Root Node. Every tree must have root
node. We can say that root node is the origin of tree data structure. In any tree,
there must be only one root node. In above tree, a is a Root node.
2. Edge: In a tree, the connecting link between any two nodes is called as edge. A
tree with 'N' number of nodes has N-1 number of edges.

Chinmayee Pati
2

3. Parent: A node that directly precedes a node is called a parent node. In a tree
each node has exactly one parent except the root.
4. Child: One or more nodes that directly follow a node are called child nodes. In a
tree, any parent node can have any number of child nodes. In a tree, all the nodes
except root are child nodes.
5. Siblings: In a tree, the nodes that have the same parent are said to be siblings.
6. Leaf: In a tree, nodes of degree zero are called leaf nodes or external nodes. In
simple words, a leaf is a node with no child.
7. Internal nodes: A node in a tree that has one or more child nodes is called an
internal node. A non-leaf node is called an internal node.
8. Degree: In a tree, the total number of children of a node is called as degree of
that Node. In simple words, the Degree of a node is total number of children it
has.
9. Degree of Tree: The highest degree of a node among all the nodes in a tree is
called as Degree of Tree.
10. Path: In a tree, a path is a sequence of nodes, (𝑎0 , 𝑎1 , 𝑎2 , . . . , 𝑎𝑛 ) where each node
in this sequence is a child of the previous node in the sequence, i.e., 𝑎𝑘+1 is a
child of 𝑎𝑘 . Length of a Path is total number of edges in that path. In the above
tree, the path 𝑎 → 𝑐 → 𝑖 → 𝑝 has length 3.
11. Level: In a tree, the root node is said to be at Level 0 and the children of root
node are at Level 1 and the children of the nodes which are at Level 1 will be at
Level 2 and so on.
12. Depth: In a tree, the length of the path from root node to a particular node is
called as depth of that node. In a tree, depth of the root node is '0'. The depth of a
tree is the maximum level of any leaf in the tree.
13. Height: In a tree, the height of a node is the length of the longest path from that
node to a leaf. Thus all leaves are at height 0. The height of a tree is the height of
the root. The height of an empty tree (a tree containing no nodes) is –1.
14. Ancestor and descendant:
In a tree if a path exists from node A to node B then,
i) A is said to be an ancestor of B and
ii) B is said to be a descendant of A.

The strict ancestors of a node are all ancestors of a node other than the node
itself. Similarly, the strict descendants of a node are all descendants of a node
other than the node itself.

15. Sub Tree: A sub-tree of a tree is a tree consisting a node of the tree and all of its
descendants in the tree

Recursive Definition of a Tree:

A recursive definition of a tree is as follows:

1. A degree-0 node is a tree, and


Chinmayee Pati
3

2. A node of degree n is a tree if it has n children, all of which are trees and none of
which have overlapping descendants.

Binary Tree:

A tree in which every node can have a maximum of two children is called as Binary Tree.
In a binary tree, every node can have either 0 children or 1 child or 2 children but not
more than 2 children.

Properties of binary Tree:

1. The maximum number of nodes on level 𝑖 of a binary tree is 2𝑖 , 𝑖 ≥ 1

Proof:

Number of nodes at level 0 = 1 (Root)= 20

Since, root has at most 2 children, maximum number of vertices at level 1 = 2 =


2 and so on.
0

2. The maximum number of nodes in a binary tree of height h is 2ℎ+1 − 1.

Proof:

Maximum number of nodes at level 0 = 1 = 20


Maximum number of nodes at level 1 = 21
Maximum number of nodes at level 2 = 22
... .... .... ... ... ... ... ... ... ...
Maximum number of nodes at level ℎ = 2ℎ
∴ The maximum number of nodes in a binary tree of height h
= 20 + 21 + ⋯ 2ℎ = 2ℎ+1 − 1.
3. Minimum number of nodes in a binary tree of height ℎ = ℎ + 1

Proof:

Minimum number of nodes at level 0 = 1


Minimum number of nodes at level 1 = 1
Chinmayee Pati
4

Minimum number of nodes at level 2 = 1


... .... .... ... ... ... ... ... ... ...
Minimum number of nodes at level ℎ = 1
∴ The minimum number of nodes in a binary tree of height ℎ = 1 + 1 + ⋯ + 1 =
ℎ+1

Different types of binary trees:

1. Strictly Binary Tree: A binary tree in which every node has either two or zero
number of children is called Strictly Binary Tree. Strictly binary tree is also called
as Full Binary Tree or Proper Binary Tree.

2. Complete Binary Tree: A binary tree in which every internal node has exactly
two children and all leaf nodes are at same level is called Complete Binary Tree.
Complete binary tree is also called as Perfect Binary Tree.

3. Almost Complete Binary tree: An almost complete binary tree is a binary tree
that satisfies the following 2 properties-
i) All the levels are completely filled except possibly the last level.

Chinmayee Pati
5

ii) The last level must be strictly filled from left to right.

4. Extended Binary Tree: An extended binary tree is a transformation of any


binary tree into a full binary tree. The full binary tree obtained by adding dummy
nodes to a binary tree is called as Extended Binary Tree.

5. Balanced Binary Tree: A balanced binary tree is also known as height balanced
tree. A height balanced binary tree is a binary tree in which the height of the left
and right sub-tree of any node differs by not more than 1.

Memory Representation of binary tree:

The memory representation of binary trees can be classified as

1. Array representation
2. Linked representation.

Chinmayee Pati
6

Array representation:

In array representation of binary tree, we use a one dimensional array (1-D Array) to
represent a binary tree. To represent a binary tree T of depth ’d’ using array
representation, we need one dimensional array with a maximum size of 2𝑑 − 1. If the
tree T is a complete binary tree, its nodes are represented sequentially and this method
wastes no space. This method uses a single linear array T as follows:

1. The root of the tree is stored in 𝑇[0]


2. If a node occupies T[i], then its left child is stored in 𝑇[2 × 𝑖 + 1] and its right
child is stored in 𝑇[2 × 𝑖 + 2] and its parent is stored in 𝑇[(𝑖 − 1)/2]

Example:

𝑇[ ] A B C D E F G H I J K L
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Example:

𝑇[ ] a b c d e f g
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Chinmayee Pati
7

Linked Representation:

In linked representation each node in a binary tree has three fields:

1. Left Child (LChild): It holds address of the left child node of the parent node.
2. Information of the Node (Info): It holds information of every node.
3. Right Child (RChild): It holds address of the right node of the parent node.

If any sub-tree is empty then the corresponding pointer’s LChild and RChild will store a
NULL value.

Node
LChild Info RChild
Example:

Traversing Binary Tree:

A tree traversal is a method of visiting every node in the tree. It is a way in which each
node in the tree is visited exactly once in a systematic manner. There are three standard
ways of traversing a binary tree. They are:

1. Pre Order Traversal (Node-left-right)


2. In order Traversal (Left-node-right)
3. Post Order Traversal (Left-right-node)

Preorder Traversal:

In a preorder traversal, each root node is visited before its left and right subtrees are
traversed. Preorder search is also called backtracking.

The steps for traversing a binary tree in preorder traversal are:

1. Visit the root.


2. Traverse the left subtree, using preorder.

Chinmayee Pati
8

3. Traverse the right subtree, using preorder.

Algorithm for Preorder:

void preorder (node * root)

if (root != NULL)

printf (“%d\n”, root → info);

preorder(root → lchild);

preorder(root →rchild);

Inorder Traversal:

In the case of inorder traversal, the root of each subtree is visited after its left subtree
has been traversed but before the traversal of its right subtree begins.

The steps for traversing a binary tree in inorder traversal are:

1. Traverse the left subtree, using inorder.


2. Visit the root.
3. Traverse the right subtree, using inorder.

Algorithm for Inorder:

void inorder (node *root)

if (root != NULL)

inorder(root → lchild);

printf (“%d\n”, root → info);

inorder(root → rchild);

Chinmayee Pati
9

Postorder Traversal:

In a postorder traversal, each root is visited after its left and right subtrees have been
traversed.

The steps for traversing a binary tree in postorder traversal are:

1. Traverse the left subtree, using postorder.


2. Traverse the right subtree, using postorder
3. Visit the root.

Algorithm for postorder:

void postorder (node *root)

if (root != NULL)

postorder (root → lchild);

postorder(root → rchild);

printf (“%d\n”, root →info);

Level order Traversal:

In a level order traversal, the nodes are visited level by level starting from the root, and
going from left to right. The level order traversal requires a queue data structure. So, it
is not possible to develop a recursive procedure to traverse the binary tree in level
order. This is nothing but a breadth first search technique.

Algorithm:

void levelorder()

int j;

for(j = 0; j < ctr; j++)

if(tree[j] != NULL)

Chinmayee Pati
10

print tree[j] -> info;

Example:

Example:

Preorder Traversal: 𝑎, 𝑏, 𝑒, 𝑗, 𝑐, 𝑓, 𝑔, ℎ

Inorder Traversal: 𝑏, 𝑗, 𝑒, 𝑎, 𝑓, 𝑐, 𝑔, ℎ

Postorder Traversal: 𝑗, 𝑒, 𝑏, 𝑓, ℎ, 𝑔, 𝑐, 𝑎

Level order Traversal: 𝑎, 𝑏, 𝑐, 𝑒, 𝑓, 𝑔, 𝑗, ℎ

Chinmayee Pati
11

Construction of binary Tree:

A binary tree can be constructed if any of the following two traversals are given:
 Preorder and inorder
 Postorder and inorder

Basic principle for construction:


 If the preorder traversal is given, then the first node is the root node.
 If the postorder traversal is given then the last node is the root node.
 Once the root node is identified, all the nodes in the left sub-trees and
right sub-trees of the root node can be identified using inorder.

Same technique can be applied repeatedly to form sub-trees.

Construction of a binary Tree from its preorder and inorder traversal:

Construct a binary tree from the following Preorder and inorder sequence.

Preorder: 𝐴𝐵𝐷𝐸𝐻𝐶𝐹𝐺𝐼𝐾𝐽
Inorder: 𝐷𝐵𝐻𝐸𝐴𝐹𝐶𝐾𝐼𝐺𝐽

From Preorder sequence 𝐴 𝐵 𝐷 𝐸 𝐻 𝐶 𝐹 𝐺 𝐼 𝐾 𝐽, the root is: A


From Inorder sequence 𝐷 𝐵 𝐻 𝐸 𝐴 𝐹 𝐶 𝐾 𝐼 𝐺 𝐽, we get the left and right sub trees:
Left sub tree is: D B H E
Right sub tree is: F C K I G J
The Binary tree up to this point looks like:

DBHE FCKIGJ

To find the root, left and right sub trees for D B H E:


From the preorder sequence B D E H, the root of tree is: B
From the inorder sequence D B H E, we can find that D is to the left of B and H and E are
to the right of B.
The Binary tree up to this point looks like:

D HE

Chinmayee Pati
12

Continuing in the same procedure we obtain the following binary tree:

DBHE
B C FCKIGJ

HE

KIGJ
D E F G

KI
I J
H

Construction of a binary Tree from its preorder and inorder traversal:

Construct a binary tree from a given postorder and inorder sequence:

Postorder: 𝐺𝐷𝐵𝐻𝐼𝐸𝐹𝐶𝐴
Inorder: 𝐷𝐺𝐵𝐴𝐻𝐸𝐼𝐶𝐹

From Postorder sequence G D B H I E F C A, the root is: A


From Inorder sequence D G B A H E I C F, we get the left and right sub trees:
Left sub tree is: D G B
Right sub tree is: H E I C F
The Binary tree up to this point looks like:

DGB HEICF

To find the root, left and right sub trees for D G B:


From the postorder sequence G D B, the root of tree is: B
From the inorder sequence D G B, we can find that D G are to the left of B and there is
no right subtree for B.
The Binary tree up to this point looks like:

B HEICF
Chinmayee Pati

DG
13

Continuing in the same procedure we obtain the following binary tree:

B C

D E F

G H I

Binary Search Tree:


A Binary Search Tree is a binary tree, which is either empty or satisfies the following
properties:
1. Every node has a value and no two nodes have the same value (i.e., all the values
are unique).
2. For all nodes x and y, if y belongs to the left subtree of x, then the value at y is less
than the value at x, and if y belongs to the right subtree of x, then the value at y is
greater than the value at x.
3. The left and right sub trees are also binary trees.

Operations on Binary Search Tree:

The operations performed on binary tree can also be applied to Binary Search Tree with
following three operations:

 Inserting a node
 Searching a node
 Deleting a node

Inserting a node:

Chinmayee Pati
14

A BST is constructed by the repeated insertion of new nodes to the tree structure.
Inserting a node in to a tree is achieved by performing two separate operations.

1. The tree must be searched to determine where the node is to be inserted.


2. Then the node is inserted into the tree.

Suppose a “DATA” is the information to be inserted in a BST.

1. Compare DATA with root node information of the tree


i) If (DATA < ROOT → Info)
Proceed to the left child of ROOT
ii) If (DATA > ROOT → Info)
Proceed to the right child of ROOT
2. Repeat the Step 1 until we meet an empty sub tree, where we can insert the
DATA in place of the empty sub tree by creating a new node.
3. Exit

Example:

Insert 16 to the following BST:

Algorithm:

NEWNODE is a pointer variable to hold the address of the newly created node. DATA is
the information to be pushed.

1. Input the DATA to be pushed and ROOT node of the tree.


2. NEWNODE = Create a New Node.

Chinmayee Pati
15

3. If (ROOT == NULL)
ROOT=NEW NODE
4. Else if (DATA < ROOT → Info)
a) ROOT = ROOT → Lchild
b) GoTo Step 4
5. Else If (DATA > ROOT → Info)
a) ROOT = ROOT → Rchild
b) GoTo Step 4
6. If (DATA < ROOT → Info)
ROOT → LChild = NEWNODE
7. Else If (DATA > ROOT → Info)
ROOT → RChild = NEWNODE
8. Else
a) Display (“DUPLICATE NODE”)
b) EXIT
9. NEW NODE → Info = DATA
10. NEW NODE → LChild = NULL
11. NEW NODE → RChild = NULL
12. EXIT

Searching a node:

Algorithm:

1. Input the DATA to be searched and assign the address of the root node to ROOT.
2. If (DATA == ROOT → Info)
a) Display “The DATA exist in the tree”
b) GoTo Step 6
3. If (ROOT == NULL)
a) Display “The DATA does not exist”
b) GoTo Step 6
4. If(DATA > ROOT→Info)
a) ROOT = ROOT→RChild
b) GoTo Step 2
5. If(DATA < ROOT→Info)
a) ROOT = ROOT→Lchild
b) GoTo Step 2
6. Exit

Deleting a node:

To delete a DATA of information from a binary search tree, first search and locate the
node to be deleted. Then any one of the following conditions arises:

1. The node to be deleted has no children: Suppose the node to be deleted is N. If N


has no children then simply delete the node and place its parent node by the
NULL pointer.

Chinmayee Pati
16

2. The node has exactly one child (or one sub tree, left or right sub tree): If N has one
child, check whether it is a right or left child. If it is a right child, then find the
smallest element from the corresponding right sub tree. Then replace the
smallest node information with the deleted node. If N has a left child, find the
largest element from the corresponding left sub tree. Then replace the largest
node information with the deleted node.
3. The node has two children (or two sub tress, left and right sub tree): The same
process is repeated if N has two children, i.e., left and right child. The node which
is to be deleted is replaced with its in-order successor or predecessor recursively
until the node value (to be deleted) is placed on the leaf of the tree. After the
procedure, replace the node with NULL and free the allocated space.

Example:

Chinmayee Pati
17

Algorithm:

NODE is the current position of the tree, which is in under consideration. LOC is the
place where node is to be replaced. DATA is the information of node to be deleted.

1. Find the location NODE of the DATA to be deleted.


2. If (NODE = NULL)
a) Display “DATA is not in tree”
b) Exit
3. If(NODE → Lchild = NULL)
a) LOC = NODE
b) NODE = NODE → RChild
4. If(NODE → RChild= =NULL)
a) LOC = NODE
b) NODE = NODE → LChild
5. If((NODE → Lchild not equal to NULL) && (NODE → Rchild not equal to NULL))
LOC = NODE → RChild
6. While(LOC → Lchild not equal to NULL)
LOC = LOC → Lchild
7. LOC → Lchild = NODE → Lchild
8. LOC → RChild= NODE → RChild
9. Exit

Balanced Binary Tree:

A balanced binary tree is also known as height balanced tree. A height balanced binary
tree is a binary tree in which the heights of the left and right sub-tree of any node differ
by not more than 1. (Example: AVL Tree)

AVL Tree:
An AVL tree is a binary search tree in which the left and right sub tree of any node may
differ in height by at most 1, and in which both the sub trees are themselves AVL Trees.
Each node in the AVL Tree possesses any one of the following properties:

Chinmayee Pati
18

1. A node is called left heavy, if the largest path in its left sub tree is one level larger
than the largest path of its right sub tree.
2. A node is called right heavy, if the largest path in its right sub tree is one level
larger than the largest path of its left sub tree.
3. The node is called balanced, if the largest paths in both the right and left sub
trees are equal.

 In an AVL tree, balance factor of every node is either -1, 0 or +1.


 𝐵𝑎𝑙𝑎𝑛𝑐𝑒 𝑓𝑎𝑐𝑡𝑜𝑟 = ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝐿𝑒𝑓𝑡 𝑆𝑢𝑏𝑡𝑟𝑒𝑒 − ℎ𝑒𝑖𝑔ℎ𝑡 𝑜𝑓 𝑅𝑖𝑔ℎ𝑡 𝑆𝑢𝑏𝑡𝑟𝑒𝑒

Not an AVL Tree An AVL Tree

Note: Every AVL Tree is a binary search tree but every Binary Search Tree need not be
AVL tree.

AVL Tree Rotations:

Rotation is the process of moving nodes either to left or to right to make the tree
balanced.

Chinmayee Pati
19

Classification of Rotation:

Left Rotation (LL Rotation)


Single Rotation
Right Rotation (RR Rotation)

Rotations
Left Right Rotation (LR Rotation)
Double Rotation
Right Left Rotation (RL Rotation)

Single Left Rotation (LL Rotation):

In LL Rotation, every node moves one position to left from the current position.

Single Right Rotation (RR Rotation):

In RR Rotation, every node moves one position to right from the current position.

Chinmayee Pati
20

Left Right Rotation (LR Rotation):

The LR Rotation is a sequence of single left rotation followed by a single right rotation.
In LR Rotation, at first, every node moves one position to the left and one position to
right from the current position.

Right Left Rotation (RL Rotation):

The RL Rotation is sequence of single right rotation followed by single left rotation. In
RL Rotation, at first every node moves one position to right and one position to left from
the current position.

Insertion Operation in AVL Tree:

In AVL Tree, a new node is always inserted as a leaf node. It is performed in the same
way as it is performed in a binary search tree. However, it may lead to violation in the
AVL tree property and therefore the tree may need balancing.

Algorithm:

1. Insert the new element into the tree using Binary Search Tree insertion logic.
2. After insertion, check the Balance Factor of every node.
Chinmayee Pati
21

3. If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.


4. If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to
be imbalanced. In this case, perform suitable Rotation to make it balanced and go
for next operation.

Deletion Operation in AVL Tree:

Deleting a node from an AVL tree is similar to that in a binary search tree. Deletion may
disturb the balance factor of an AVL tree and therefore the tree needs to be rebalanced
in order to maintain the AVLness.

Algorithm:

1. Delete a node from the tree using Binary Search Tree deletion logic.
2. After deletion, check the Balance Factor of every node.
3. If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
4. If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said to
be imbalanced. In this case, perform suitable Rotation to make it balanced and go
for next operation.

Multiway Search Tree:

An m-way search tree is a tree in which, for some integer m called the order of the tree,
each node has at most m children. If 𝑘 ≤ 𝑚 is the number of children, then the node
contains exactly 𝑘 − 1 keys, which partition all the keys into k subsets consisting of all
the keys less than the first key in the node, all the keys between a pair of keys in the
node, and all keys greater than the largest key in the node.

B-Tree:

A B-tree of order m is a multi-way search tree with the following properties:

1. Each node has at most m children and m-1 keys.


𝑚
2. Each internal node (except possibly the root) has at least 𝑏 = ⌈ 2 ⌉ children
and 𝑏 − 1 keys.
3. The root is a leaf or has at least two children.
4. All leaf nodes are at the same level.

Chinmayee Pati
22

Example of B-tree of order 3:

Example of B-tree of order 4:

Insertion into a B-Tree:

Insertions are done at the leaf node level.

Algorithm:

1. Traverse the B Tree in order to find the appropriate leaf node at which the node
can be inserted.
2. If the leaf node contain less than m-1 keys then insert the element in the
increasing order.
3. Else, if the leaf node contains m-1 keys, then
a) Insert the new element in the increasing order of elements.
b) Split the node into the two nodes at the median.

Chinmayee Pati
23

c) Push the median element up to its parent node.


d) If the parent node also contain m-1 number of keys, then split it too by
following the same steps.

Deletion from a B-Tree:

Deletion is also performed at the leaf nodes. The node which is to be deleted can either
be a leaf node or an internal node.

Algorithm:

1. Locate the leaf node.


2. If there are more than m/2 keys in the leaf node then delete the desired key from
the node.
3. If the leaf node doesn't contain m/2 keys then complete the keys by taking the
element from eight or left sibling.
a) If the left sibling contains more than m/2 elements then push its largest
element up to its parent and move the intervening element down to the
node where the key is deleted.
b) If the right sibling contains more than m/2 elements then push its
smallest element up to the parent and move intervening element down to
the node where the key is deleted.
4. If neither of the sibling contain more than m/2 elements then create a new leaf
node by joining two leaf nodes and the intervening element of the parent node.
5. If parent is left with less than m/2 nodes then, apply the above process on the
parent too.

If the the node which is to be deleted is an internal node, then replace the node with its
in-order successor or predecessor. Since, successor or predecessor will always be on the
leaf node hence, the process will be similar as the node is being deleted from the leaf
node.

Chinmayee Pati

You might also like