0% found this document useful (0 votes)
61 views125 pages

Trees

The document defines and explains key concepts related to tree data structures. It defines a tree as a non-linear data structure where data is organized in a hierarchical manner. Key terms defined include root, child, parent, leaf nodes, internal nodes, and different tree traversal methods like preorder, inorder and postorder traversals. Binary tree representations using arrays and linked lists are also discussed.

Uploaded by

Harsimran Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views125 pages

Trees

The document defines and explains key concepts related to tree data structures. It defines a tree as a non-linear data structure where data is organized in a hierarchical manner. Key terms defined include root, child, parent, leaf nodes, internal nodes, and different tree traversal methods like preorder, inorder and postorder traversals. Binary tree representations using arrays and linked lists are also discussed.

Uploaded by

Harsimran Kaur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 125

Trees

Definition
In linear data structure, data is organized
in sequential order and in non-linear data
structure, data is organized in random
order.
Tree is a very popular data structure used
in wide range of applications.
A tree data structure can be defined as
follows...
A treedata structure can also be defined as
follows...

In tree data structure, every individual element is


called as Node.
Node in a tree data structure, stores the actual
data of that particular element and link to next
element in hierarchical structure.

In a tree data structure, if we have N number of


nodes then we can have a maximum of N-
1 number of links.
Example
Terminology
1. Root

In a tree data structure, 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.
We never have multiple root nodes in a
tree.
2. Edge

In a tree data structure, the connecting


link between any two nodes is called
as EDGE.
In a tree with 'N' number of nodes there
will be a maximum of 'N-1' number of
edges.
3. Parent

In a tree data structure, the node which is


predecessor of any node is called
as PARENT NODE.
In simple words, the node which has
branch from it to any other node is called
as parent node.
Parent node can also be defined as "The
node which has child / children".
4. Child

In a tree data structure, the node which is


descendant of any node is called as CHILD
Node.
In simple words, the node which has a link
from its parent node is called as child node.
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 data structure, nodes which


belong to same Parent are called
as SIBLINGS.
In simple words, the nodes with same
parent are called as Sibling nodes.
6. Leaf

Ina tree data structure, the node which does not


have a child is called as LEAF Node. In simple
words, a leaf is a node with no child. 

In a tree data structure, the leaf nodes are also


called as External Nodes.
External node is also a node with no child. In a
tree, leaf node is also called as 'Terminal' node.
7. Internal Nodes

In a tree data structure, the node which has


atleast one child is called as INTERNAL Node.
In simple words, an internal node is a node with
atleast one child. 
In a tree data structure, nodes other than leaf
nodes are called as Internal Nodes. 
The root node is also said to be Internal
Node if the tree has more than one node. 
Internal nodes are also called as 'Non-
Terminal' nodes.
8. Degree

In a tree data structure, 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.
The highest degree of a node among all
the nodes in a tree is called as 'Degree of
Tree'
9. Level

In a tree data structure, 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...
In simple words, in a tree each step from
top to bottom is called as a Level and the
Level count starts with '0' and
incremented by one at each level (Step).
10. Height

In a tree data structure, the total number


of egdes from leaf node to a particular
node in the longest path is called
as HEIGHT of that Node. 
In a tree, height of the root node is said to
be height of the tree.
In a tree, height of all leaf nodes is '0'.
11. Depth

In a tree data structure, the total number of


egdes from root node to a particular node is
called as DEPTH of that Node. 
In a tree, the total number of edges from root
node to a leaf node in the longest path is said
to be Depth of the tree.
In simple words, the highest depth of any leaf
node in a tree is said to be depth of that tree.
In a tree, depth of the root node is '0'.
12. Path

In a tree data structure, the sequence of Nodes


and Edges from one node to another node is
called as PATH between that two Nodes. 
Length of a Path is total number of nodes in
that path. 
In below example the path A - B - E - J has
length 4.
13. Sub Tree

In a tree data structure, each child from a


node forms a subtree recursively.
Every child node will form a subtree on
its parent node.
Tree Representations
Left Child - Right Sibling Representation

In this representation, we use 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.
Graphical representation of that node is as
follows...
In this representation, every node's data
field stores the actual value of that node.
If that node has left child, then left
reference field stores the address of that
left child node otherwise that field stores
NULL.
If that node has right sibling then right
reference field stores the address of right
sibling node otherwise that field stores
NULL. 
Example
Binary Tree
In a normal tree, every node can have any
number of children.
Binary tree is a special type of tree data structure
in which every node can have a maximum of 2
children.
One is known as left child and the other is
known as right child.

In a binary tree, every node can have either 0


children or 1 child or 2 children but not more
than 2 children.
Example
Types of Binary Tree
There are different types of binary trees
and they are...
1. Strictly Binary Tree
◦ In a binary tree, every node can have a
maximum of two children.
◦ But in strictly binary tree, every node should
have exactly two children or none.
◦ That means every internal node must have
exactly two children.
Strictly
binary tree is also called as Full
Binary Tree or Proper Binary Tree or 2-
Tree

Example:
Strictly binary tree data structure is used
to represent mathematical expressions.
Example:
2. Complete Binary Tree
In a binary tree, every node can have a
maximum of two children.
But in strictly binary tree, every node should
have exactly two children or none and in
complete binary tree all the nodes must have
exactly two children and at every level of
complete binary tree there must be
2level number of nodes.
For example at level 2 there must be 22 = 4
nodes and at level 3 there must be 23 = 8
nodes.
Example:
3. Extended Binary Tree
A binary tree can be converted into Full
Binary tree by adding dummy nodes to
existing nodes wherever required.
Example:

In above figure, a normal binary tree is converted


into full binary tree by adding dummy nodes (In pink
colour).
Binary Tree Representations

A binary tree data structure is represented


using two methods. Those methods are as
follows...
1. Array Representation
2. Linked List Representation
Consider the following binary tree...
1. Array Representation
In array representation of binary tree, we use
a one dimensional array (1-D Array) to
represent a binary tree.
Consider the above example of binary tree
and it is represented as follows...

To represent a binary tree of depth 'n' using


array representation, we need one
dimensional array with a maximum size
of 2n+1 - 1.(parent=ith,left child=2*ith,right
child=2*i +1th)
Limitations of Array Representation:
Ifthe binary tree is not complete, then
necessary to leave blank spaces
Wastage of space
Space complexity increases.
2. Linked List Representation

We use double linked list to represent a


binary tree. In a double linked list, every
node consists of three fields.
First field for storing left child address,
second for storing actual data and third for
storing right child address.
In this linked list representation, a node
has the following structure...
The above example of binary tree
represented using Linked list
representation is shown as follows...
Binary Tree Traversals

When we wanted to display a binary tree,


we need to follow some order in which all
the nodes of that binary tree must be
displayed.
In any binary tree displaying order of
nodes depends on the traversal method.
Types of Traversals
There are three types of binary tree
traversals.
1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal
Consider the following binary tree...
1. In - Order Traversal ( leftChild - root - rightChild )

In In-Order traversal, the root node is visited


between left child and right child.
In this traversal, the left child node is visited first,
then the root node is visited and later we go for
visiting right child node.
This in-order traversal is applicable for every root
node of all subtrees in the tree.
This is performed recursively for all nodes in the
tree.
In-Order Traversal for above example of binary
tree is 
I-D-J-B-F-A-G-K-C-H
2. Pre - Order Traversal ( root - leftChild - rightChild )

In Pre-Order traversal, the root node is


visited before left child and right child
nodes.
In this traversal, the root node is visited first,
then its left child and later its right child.
This pre-order traversal is applicable for
every root node of all subtrees in the tree. 
Pre-Order Traversal for above example
binary tree is 
A-B-D-I-J-F-C-G-K-H
2. Post - Order Traversal ( leftChild - rightChild - root )

In Post-Order traversal, the root node is


visited after left child and right child.
In this traversal, left child node is visited
first, then its right child and then its root
node.
This is recursively performed until the
right most node is visited.
Post-Order Traversal for above
example binary tree is 
I -J-D-F-B -K-G -H -C-A
Program to Create Binary Tree and display using In-Order
Traversal.
Examples:
Solution:
Traversal Applications
• Determine height.
• Determine number of nodes.
Binary Search Tree

In a binary tree, every node can have maximum of


two children but there is no order of nodes based
on their values.
In binary tree, the elements are arranged as they
arrive to the tree, from top to bottom and left to
right.
A binary tree has the following time
complexities...
◦ Search Operation - O(n)
◦ Insertion Operation - O(1)
◦ Deletion Operation - O(n)
To enhance the performance of binary
tree, we use special type of binary tree
known as Binary Search Tree. Binary
search tree mainly focus on the search
operation in binary tree. Binary search
tree can be defined as follows...
In a binary search tree, all the nodes in
left subtree of any node contains smaller
values and all the nodes in right subtree of
that contains larger values as shown in
following figure...
Example
The following tree is a Binary Search Tree. In this tree,
left subtree of every node contains nodes with smaller
values and right subtree of every node contains larger
values.

Every Binary Search Tree is a binary tree but all


the Binary Trees need not to be binary search trees.
Example
Construct a Binary Search Tree by
inserting the following sequence of
numbers...
10,12,5,4,20,8,7,15 and 13
Above elements are inserted into a Binary
Search Tree as follows...
Operations on a Binary Search Tree

The following operations are performed


on a binary search tree...
1. Search
2. Insertion
3. Deletion
Searching a key
Whenever an element is to be searched,
start searching from the root node.if the
key is present at root, we return root.
Then if the data is less than the key value
of the root, search for the element in the
left subtree.
Otherwise, search for the element in the
right subtree.
Algorithm:
Search for data in the tree N
1. If (if N is NULL or N contains data)
1. Return N
2. Else If (data greater than contents of
N)
1. Return the results of Searching the N:right
3. Else If (data lesser than contents of N)
1. Return the results of Searching the N:left
Illustration to search 6 in below tree:
1. Start from root.
2. Compare the inserting element with
root, if less than root, then recurse for left,
else recurse for right.
3. If element to search is found anywhere,
return true, else return false.
Insertion of a key
A new key is always inserted at leaf.
We start searching a key from root till we hit
a leaf node.
Once a leaf node is found, the new node is
added as a child of the leaf node.
Algorithm:
1. If TPtr is NULL
1. Set TPtr= Create a Node with contents data
2. Else If data is greater than contents of
TPtr
1. Set TPtr->Right= Insert (TPtr->Right, data)
3. Else If data is lesser than contents of TPtr
1. Set TPtr->Left= Insert (TPtr->Left, data)
4. Return TPtr
Implementation:
Illustration to insert 2 in below tree:
1. Start from root.
2. Compare the inserting element with
root, if less than root, then recurse for left,
else recurse for right.
3. After reaching end,just insert that node
at left(if less than current) else right.
Time Complexity: The worst case time
complexity of search and insert operations
is O(h) where h is height of Binary Search
Tree.
In worst case, we may have to travel from
root to the deepest leaf node.
The height of a skewed tree may become
n and the time complexity of search and
insert operation may become O(n).
Deleting a Key
When we delete a node, three possibilities
arise:
1) Node to be deleted is leaf: Simply
remove from the tree.
2) Node to be deleted has only one
child: Copy the child to the node and
delete the child
3) Node to be deleted has two children: Find inorder
successor of the node. Copy contents of the inorder
successor to the node and delete the inorder successor.
Note that inorder predecessor can also be used.

 The important thing to note is, inorder successor is


needed only when right child is not empty. In this
particular case, inorder successor can be obtained by
finding the minimum value in right child of the node.
Implementation:
Implementation 2
AVL Tree
AVL tree is a self balanced binary search tree.
That means, an AVL tree is also a binary search tree
but it is a balanced tree.
A binary tree is said to be balanced, if the difference
between the heights of left and right subtrees of
every node in the tree is either -1, 0 or +1.
In other words, a binary tree is said to be balanced
if for every node, height of its children differ by at
most one.
In an AVL tree, every node maintains a extra
information known as balance factor.
The AVL tree was introduced in the year of 1962 by
G.M. Adelson-Velsky and E.M. Landis.
Balance factor of a node is the difference
between the heights of left and right subtrees
of that node.
The balance factor of a node is calculated
either height of left subtree - height of right
subtree (OR) height of right subtree - height
of left subtree.
Example:

The above tree is a binary search tree and every node is satisfying balance
factor condition. So this tree is said to be an AVL tree.
AVL Tree Rotations
In AVL tree, after performing every operation like
insertion and deletion we need to check
the balance factor of every node in the tree.
If every node satisfies the balance factor condition
then we conclude the operation otherwise we must
make it balanced.
We use rotation operations to make the tree
balanced whenever the tree is becoming
imbalanced due to any operation.

Rotation operations are used to make a tree


balanced.
There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation)
InLL Rotation every node moves one
position to left from the current position.
Single Right Rotation (RR Rotation)

InRR Rotation every node moves one


position to right from the current position.
Left Right Rotation (LR Rotation)

The LR Rotation is combination of single


left rotation followed by single right
rotation.
In LR Rotation, first every node moves
one position to left then one position to
right from the current position. 
Right Left Rotation (RL Rotation)

The RL Rotation is combination of single


right rotation followed by single left rotation.
In RL Rotation, first every node moves one
position to right then one position to left from
the current position. 
Example: Construct an AVL Tree by inserting numbers from 1 to 8.
Exercise—Do It yourself
Build an AVL tree with the following
values:
15, 20, 24, 10, 13, 7, 30, 36, 25
Solution:
Deletion Operation in AVL Tree

In an AVL Tree, the deletion operation is


similar to deletion operation in BST.
But after every deletion operation we
need to check with the Balance Factor
condition.
If the tree is balanced after deletion then
go for next operation otherwise perform
the suitable rotation to make the tree
Balanced.
Multiway Trees/M-way Trees
A multiway tree is a tree that can have
more than two children.
A multiway tree of order m (or an m-
way tree) is one in which a tree can have
m children.
As with the other trees that have been
studied, the nodes in an m-way tree will
be made up of key fields, in this case m-1
key fields, and pointers to m children.
multiway tree of order 5
Multiway search tree/M-way search
tree
To make the processing of m-way trees easier some
type of order will be imposed on the keys within each
node, resulting in a multiway search tree of order
m ( or an m-way search tree).
By definition an m-way search tree is a m-way tree in
which:
1. Each node has m children and m-1 key fields
2. The keys in each node are in ascending order.
3. The keys in the first i children are smaller than the ith
key
4. The keys in the last m-i children are larger than the ith
key
4-way search tree
M-way search trees give the same
advantages to m-way trees that binary
search trees gave to binary trees - they
provide fast information retrieval and
update.
However, they also have the same
problems that binary search trees had -
they can become unbalanced, which
means that the construction of the tree
becomes of vital importance.
B - Trees
In a binary search tree, AVL Tree, etc., every
node can have only one value (key) and
maximum of two children but there is another
type of search tree called B-Tree in which a
node can store more than one value (key) and
it can have more than two children.
B-Tree was developed in the year of 1972
by Bayer and McCreight with the
name Height Balanced m-way Search Tree.
Later it was named as B-Tree.
Here, number of keys in a node and number of children for a node is depend
on the order of the B-Tree. Every B-Tree has order.
B-Tree of Order m has the following properties...
Property #1 - All the leaf nodes must be at same level.
Property #2 - All nodes except root must have at least [m/2]-1 keys and
maximum of m-1 keys.
Property #3 - All non leaf nodes except root (i.e. all internal nodes) must
have at least m/2 children.
Property #4 - If the root node is a non leaf node, then it must have at least
2 children.
Property #5 - A non leaf node with n-1 keys must have n number of
children.
Property #6 - All the key values within a node must be in Ascending
Order.
For example, B-Tree of Order 4 contains maximum 3 key values in a node
and maximum 4 children for a node.
Example
Operations on a B-Tree

The following operations are performed


on a B-Tree...
Search
Insertion
Deletion
Search Operation in B-Tree
In a B-Tree, the search operation is similar
to that of Binary Search Tree.
In a Binary search tree, the search process
starts from the root node and every time we
make a 2-way decision (we go to either left
subtree or right subtree).
In B-Tree also search process starts from
the root node but every time we make n-
way decision where n is the total number of
children that node has.
In a B-Ttree, the search operation is
performed with O(log n) time complexity.
Algorithm:
Step 1: Read the search element from the user
Step 2: Compare, the search element with first key value of root
node in the tree.
Step 3: If both are matching, then display "Given node found!!!" and
terminate the function
Step 4: If both are not matching, then check whether search element
is smaller or larger than that key value.
Step 5: If search element is smaller, then continue the search process
in left subtree.
Step 6: If search element is larger, then compare with next key value
in the same node and repeate step 3, 4, 5 and 6 until we found exact
match or comparision completed with last key value in a leaf node.
Step 7: If we completed with last key value in a leaf node, then
display "Element is not found" and terminate the function.
Insertion Operation in B-Tree
Step 1: Check whether tree is Empty.
Step 2: If tree is Empty, then create a new node with new
key value and insert into the tree as a root node.
Step 3: If tree is Not Empty, then find a leaf node to which
the new key value cab be added using Binary Search Tree
logic.
Step 4: If that leaf node has an empty position, then add the
new key value to that leaf node by maintaining ascending
order of key value within the node.
Step 5: If that leaf node is already full, then split that leaf
node by sending middle value to its parent node. Repeat
tha same until sending value is fixed into a node.
Step 6: If the spilting is occuring to the root node, then the
middle value becomes new root node for the tree and the
height of the tree is increased by one.
Example

Construct a B-Tree of Order 3 by


inserting numbers from 1 to 10.
B+ Tree
A B+ tree is the same as a B tree; the only
difference is that, in the B+ tree there is an
additional level added at the bottom with
linked leaves. 
Problem With B Trees
Accessing keys from B-tree in sorted
order Requires Backtracking.
Solution: B+ Trees
B+ Trees:
Facilitate Sequential Operations
 String all leaf nodes together
Replicate keys from non-leaf nodes to make
sure each key appears at leaf level.
The leaf pages are maintained in sequential order
 AND a doubly linked list connects each leaf
page with its sibling page(s)
 This doubly linked list speeds data movement as
the pages grow and contract.

You might also like