0% found this document useful (0 votes)
1 views78 pages

Topic 2.2: Non-Linear Data Structures

The document provides an overview of non-linear data structures, focusing on trees and binary trees, including their definitions, properties, and types. It explains various operations such as search, insert, and delete in binary search trees, along with complexities and traversal methods. Additionally, it discusses balanced search trees like AVL trees and their rotation mechanisms for maintaining balance.
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)
1 views78 pages

Topic 2.2: Non-Linear Data Structures

The document provides an overview of non-linear data structures, focusing on trees and binary trees, including their definitions, properties, and types. It explains various operations such as search, insert, and delete in binary search trees, along with complexities and traversal methods. Additionally, it discusses balanced search trees like AVL trees and their rotation mechanisms for maintaining balance.
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/ 78

Topic 2.

Non-linear
Non-linear Data
Data
Structures
Structures

By: Er.Bhavneet Kaur


Master Subject Co-ordinator
CSE 2nd Year
What is Non Linear Data Structure?

• Non-linear data structures are those where data


items are not arranged in a sequential manner,
unlike linear data structures.
• Elements are stored in a hierarchical or a
network-based structure that does not follow a
sequential order.
• Allow efficient searching, insertion, and deletion
of elements from the structure.

Examples of Non Linear Data Structures:


Trees
Graphs, etc.
ROOTED TREES

• A rooted tree is a tree with a special vertex labelled as the "root" the of tree.
• The root serves as a point of reference for other vertices in the tree. In
diagrams, we usually keep the root at the top and list other vertices below it.
• This notion is particularly useful in computer science for working with tree-
based data structures.
• In the figure, the root vertex is shown with a black border.
• Below are some useful terms associated with rooted trees.
• Branch is just another name given to edges of the tree.
• Depth of a vertex is the number of branches in the path from root to the
vertex. So depth of the root itself is zero.
Level of a vertex is number of vertex in the path from root to
the vertex. This is just one more than the depth of the vertex.
Level of root is 1.
Note: There can be multiple childs of a vertex, but parent of a
vertex is unique. Root is the only vertex in a tree without any
parent.
A leaf is a vertex without any child.
Height of tree is the maximum value of depth for any vertex in
the tree.
BINARY TREE

• A binary tree is a hierarchal data structure in which


each node has at most two children. The child nodes
are called the left child and the right child.
• To start with, let’s describe the linked list
representation of a binary tree in which each node
has three fields:
• Pointer to store the address of the left child
• Data element
• Pointer to store the address of the right child
PROPERTIES

• A binary tree can have a maximum of nodes at level if the level of the root
is zero.
• When each node of a binary tree has one or two children, the number of leaf
nodes (nodes with no children) is one more than the number of nodes that
have two children.
• There exists a maximum of nodes in a binary tree if its height is , and the
height of a leaf node is one.
• If there are leaf nodes in a binary tree, then it has at least and at
most levels.
• A binary tree of nodes has minimum number of levels or minimum height.
• The minimum and the maximum height of a binary tree having nodes
are and , respectively.
• A binary tree of nodes has null references.
HOW TO IMPLEMENT A BINARY TREE?

We’ll use an auxiliary Node class that will store int values. And
keep a reference to each child:
class Node {
int value;
Node leftChild;
Node rightChild; Node(int value) {
this.value = value;
rightChild = null;
leftChild = null;
}
}
TYPES OF BINARY TREES

• Full Binary Tree


• A binary tree is said to be a full binary tree when each
internal node has zero or two children:
• Perfect Binary Tree
• A perfect binary tree is a special type of binary tree in
which all the leaf nodes are at the same level, and each
internal node has two children:
• Complete Binary Tree
• A binary tree is referred to as a complete binary tree when all of
its levels are completely filled. The only exception is possibly
the lowest level in which the nodes must lean as left as possible:
• Degenerate or Pathological Tree
A degenerate or pathological tree is a type of binary tree in
which each internal node has a single child, either the left
child or the right child:
• Skewed Binary Tree
• A binary tree is said to be a skewed binary tree if all of its
internal nodes have exactly one child, and either left
children or right children dominate the tree. In particular,
there exist two types of skewed binary trees: left-skewed
binary tree and the right-skewed binary tree:
• Balanced Binary Tree
• A balanced binary tree is also a special type of binary tree in
which the difference of height between the left and the right
subtrees for each node is at most one:
BINARY SEARCH TREE

• Binary search tree, also known as an ordered or sorted


binary tree in computer science, is a rooted binary tree data
structure in which each internal node stores a key that is
larger than all keys in the node’s left subtree but less than
those in the node’s right subtree.
What is the difference between a Binary tree and a
Binary Search Tree?
• A Binary Tree is a simple structure with the simple constraint
that no parent can have more than two offspring, but a
Binary Search Tree is a version of the binary tree that follows
a certain order in which the nodes should be ordered.
• Binary search trees. For any node x,
the keys in the left subtree of x are
at most x:key, and the keys in the
right subtree of x are at least x:key.
Different binary search trees can
represent the same set of values.
The worst-case running time for
most search-tree operations is
proportional to the height of the
tree.

(a) A binary search tree on 6 nodes


with height 2. The top figure
shows how to view the tree
conceptually, and the bottom
figure shows the left, right, and p
attributes in each node

(b) A less efficient binary search tree,


• It is called a binary tree because each tree node has a
maximum of two children.
• It is called a search tree because it can be used to search for
the presence of a number in O(log(n)) time.
• The properties that separate a binary search tree from a
regular binary tree is
• All nodes of left subtree are less than the root node
• All nodes of right subtree are more than the root node
• Both subtrees of each node are also BSTs i.e. they have the
above two properties.
• Wrong
representation
as 2 should be in
the left subtree
of root 3.

• Correct
representation
of BST.
SEARCH OPERATION

• Algorithm:
If root == NULL return NULL; If number == root-
>data return root->data; If number < root-
>data return search(root->left) If number >
root->data return search(root->right)
INSERT OPERATION

• Algorithm:
If node == NULL return createNode(data) if (data
< node->data) node->left = insert(node->left,
data); else if (data > node->data) node->right
= insert(node->right, data); return node;
DELETION OPERATION

• Case I

In the first case, the node to be deleted is the leaf node.


In such a case, simply delete the node from the tree.
• Case II

In the second case, the node to be deleted lies has a


single child node. In such a case follow the steps
below:
Replace that node with its child node.
Remove the child node from its original position.
• Case III
In the third case, the node to be deleted has two
children. In such a case follow the steps below:
Get the inorder successor of that node.
Replace the node with the inorder successor.
Remove the inorder successor from its original
position.
BINARY SEARCH TREE COMPLEXITIES

• Time Complexity
Here, n is the number of nodes in the
tree.
Space Complexity
The space complexity for all the
operations is O(n)
TRAVERSAL OF TREES

• 1. Depth First Search Traversal


It is a traversal algorithm in which, starting from the Root, it
explores all nodes along each branch as deeply as possible
until we get to a leaf node and then return to the “trunk” of
the tree (backtracking).
INORDER

POSTORDE
PREORDER
R
Stack is used to implement depth-first search traversal, follows
the “LIFO” (Last In First Out) or “FILO” (First In Last Out)
• In-order Traversal
• If a binary tree is traversed in-order, the
output will produce sorted key values in an
ascending order.
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.
D→B→E→A→F→C→G
• Pre-order Traversal
• In this traversal method, the root node is visited
first, then the left subtree and finally the right
subtree.
• Step 1 − Visit root node.
• Step 2 − Recursively traverse left subtree.
• Step 3 − Recursively traverse right subtree.
• A→B→D→E→C→F→G
• 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.
• Step 1 − Recursively traverse left subtree.
• Step 2 − Recursively traverse right subtree. Step 3
− Visit root node.
D→E→B→F→G→C→A
2. BREADTH FIRST SEARCH TRAVERSAL
(LEVEL ORDER TRAVERSAL)

• It is a traversal algorithm in which, starting from


Root, it visits all nodes of the current level from
left to right before moving to the next level in
the tree.
• Queue is used to implement Breadth First
Search/ Level Order Traversal, follows First-In-
First-Out (FIFO) principle
• Level order traversal: A B C D E
 Add the root node to the queue.
 Iterate while the queue is not empty and print the
value of the first node in the queue.
 Add the left child of a node to the queue (if it exists).
 Add the right child of a node to the queue (if it exists).
BALANCED SEARCH TREES

• A binary tree is balanced if the height of the tree is


O(Log n) where n is the number of nodes. For Example,
the AVL tree maintains O(Log n) height by making sure
that the difference between the heights of the left
and right subtrees is at most 1.
• balance = height(left subtree) – height(right
subtree)
• Balanced Binary Search trees are performance-wise
good as they provide O(log n) time for search, insert
and delete.
A balanced binary tree is a binary tree that
follows the 3 conditions:

• The height of the left and right tree for any node does not
differ by more than 1.
• The left subtree of that node is also balanced.
• The right subtree of that node is also balanced.
• A single node is always balanced. It is also referred to as a
height-balanced binary tree.
• It is a type of binary tree in which the difference
between the height of the left and the right subtree for
each node is either 0 or 1. In the figure above, the root
node having a value 0 is unbalanced with a depth of 2
units.
Advantages of Balanced Binary Tree:
• Non Destructive updates are supported by a Balanced
Binary Tree with the same asymptotic effectiveness.
• Range queries and iteration in the right sequence are
made feasible by the balanced binary tree.
TYPES OF BALANCED SEARCH
TREE

AVL trees Red-Black trees Splay trees


• Self-adjusting type of
BST with the additional
• BSTs that • Binary version of a 2-3- property
maintain height 4 tree. that recently accessed
balance. • Here the the nodes elements are quick to
have a ‘color’ be access again.
• For for each node, the attribute: BLACK or RE
difference in height of D. The tree maintains • The basic operations
its two subtrees is in the a balance measure such as insertion, look-
range -1 to 1 called up and removal takes
the BLACK height place in O(log n)
amortized time.
Insert/find always
rotates node to the
root.
BALANCING A TREE

• The depth of a typical node in an AVL tree approaches the


optimal value possible which is log Consequently, all
searching operations in an AVL tree have logarithmic worst-
case bounds. An update (insert or remove) in an AVL tree
could destroy the balance. Therefore we go about rebalancing
before before the operation can be considered complete.
• After an insertion, only nodes that are on the path from the
insertion point to the root can have their balances altered.
This affects the way we carry out rebalancing.
ROTATIONS

• Insertion or deletion operation involves adding or deleting


only a single node at a time. This essentially means that the
height of some sub-tree can change by at most 1.
• If the AVL tree property is violated at a node x, it means that
the heights of left(x) and right(x) differ by exactly 2.
• Now rotations will have to be applied to x to restore the AVL
tree property. The rotation is generally a O(1) operation.
• In order to restore the balance of an unbalanced tree, three
adjacent nodes are involved in the rotation. The deepest
unbalanced node, is the node that requires rotation to
rebalance the tree.
• This node is either the ancestor of a deleted node (in case of
deletion) or of the inserted node (in case of insertion) and is
the node whose balance factor has changed to -2 or +2. This
is shown using an example of insertion in Figure on next slide.
• Here the element 35 is
inserted. Therefore the
three nodes that take
part in the rotation are
the deepest unbalanced
Restoring balance of the
node (here 45), the
tree
ancestor of the inserted
node (40) and the
inserted node (35).
BST ORDERING PROPERTY AFTER A
ROTATION

• A rotation does not affect


the ordering property of a
BST (binary search tree).
This is explained in Figure
alongside.

• Let us assume a e α, b e β
and c eg. This implies that a
≤ A ≤ b ≤ B ≤ c according Maintaining BST Ordering
to the BST tree property. property
• It can be seen that even
after rotation the same
property is maintained. We
will explain the actual
procedure of rotation in the
next section.
ROTATION AND BST
PROPERTY

• BST ordering property


requirement means
that on the left hand
side tree T1 < x < y,
x< T2< y and x < y <
T3. Now we carry out
a right rotation of x
about y as shown in
the figure along side.
This means that the following changes are made:
• y which was the parent with x as left child (x<y) before rotation
now becomes the right child of parent x retaining the x<y
property.
• T2 which was the right subtree of x (x<T2<y) before rotation now
becomes the left sub-tree of y still maintaining the same relation.
• Therefore after rotation the same property T1 < x < y, x < T2< y
and x < y < T3is maintained.
• Same is applicable for left rotation.
DIFFERENT CASES OF
ROTATIONS

Case1. left
sub-tree of
left
child (Singl
e Right
Rotation)
Case 4. left Case
sub-tree of 2. right sub-
right tree of right
child (Double child (Single
Right – Left Left
Rotation) Rotation)
Case 3. right
sub-tree of
left
child (Double
Left – Right
Rotation)
SINGLE RIGHT ROTATION
• In this case the left sub-tree of the left child of X violates the
property. In this Figure (a) this means sub-tree A, the left
sub-tree of the left child Y of X violates the property. We
need to carry out single right rotation of X about Y where X
now becomes the right child of Y, and the right sub-tree of Y
becomes the left sub-tree of X.
• Figure (b) shows the three steps namely

• the left child x of a node y becomes y’s parent.


• y becomes the right child of x.
• The right subtree T2 of x, if any, becomes the left child of y

• Here the notation ((T1+T2)+T3) indicates that the subtrees T1 and T2 are children of
the same parent and that the combined subtree of (T1+T2) and subtree T3 are children
of the root. Moreover the sub-trees change positions from ((T1+T2) + T3) before the
rotation to positions (T1+(T2+T3)) after the rotation still maintaining the BST property.
• Figure (c) shows the steps more in detail.
• The node k2 is the node at which the imbalance occurs and the
balance factor is violated since the height of subtree c has
height difference of 2 with the subtree A.
• The left child of k2 is given by k1=k2.left.
• The new left sub-tree of k2 is now is k1’s right sub-tree (B) that
is k2.left=k1.right
• The new right sub-tree of k1 is k2 that is k1.right=k2
• Now we return the root of the new balanced tree that is k1.
SINGLE LEFT ROTATION

In this case the right sub-tree of the right child of X violates the
property.

The above figure shows sub-tree A, the right sub-tree of the right
child Y of X that violates the property.

We need to carry out single left rotation of Y about X where X now


becomes the left child of Y, and the left sub-tree of Y becomes the
right sub-tree of X.
• The above figure shows the three steps namely
 The right child y of a node x becomes x’s parent.
 x becomes the left child of y.
 The left child T2 of y, if any, becomes the right child of x
• Moreover the sub-trees change positions from (T1 + (T2 + T3)) before the rotation
to positions ((T1 + T2) + T3)after the rotation still maintaining the BST property.
• The above figure shows the steps more in detail.
 The node k1 is the node at which the imbalance occurs and the balance factor is
violated where the height of subtree c differs with the height of subtree A by more
than one.
 The right child of k2 is given by k2=k1.right.
 The new right sub-tree of k1 is now is k2’s left sub-tree (B) that is k1.right=k2.left
(B)
 The new left sub-tree of k2 is k1 that is k2.left=k1
 Now we return the root of the new balanced tree that is k2.
DOUBLE ROTATIONS

• The other two cases of rotation are cases of two


single rotations.
• When a new item is added to the sub-tree for
an inside grandchild (left-right or right-left), the
imbalance is fixed with a double right or left
rotation.
LEFT RIGHT DOUBLE
ROTATION

• When we have the situation where we have a


left inside grandchild (left-right) case we have
left right double rotation.
• A left-right double rotation is equivalent to a
sequence of two single rotations where the first
rotation on the original tree is a left rotation
between X’s left-child and grandchild and the
second rotation on this new tree is
a right rotation between X and its new left child.
• In this case the right sub-tree of the left child
of X violates the property.
• First we need to rotate the tree LEFT about X’s
left child (Y) and grandchild (Z) as shown in the
above figure which is similar to single left
rotation.
• Now we need to carry out the single right
rotation of z about x in the modified tree that
is we rotate the tree RIGHT about X and its
new left child (Z) ,which is clearly shown in the
above figure.
• The given figure shows the steps more in detail.
 The first pivot for the left rotation is the left child (v) of the deepest unbalanced node (x).
 We carry out Single Left Rotation of w about the first pivot v
 Moreover during this single left rotation the sub-trees change positions from ((T1 + ((T2+T3) + T4) before
the first rotation to positions (((T1 + T2)+T3) + T4) after the rotation still maintaining the BST property.
 The second pivot for the right rotation is the deepest unbalanced node itself x.
 We carry out Single Right Rotation of w about x
 Moreover during this single right rotation the sub-trees change positions from (((T1 + T2)+T3) + T4) before
the first rotation to positions ((T1 + T2)+ (T3+ T4)) after the rotation still maintaining the BST property.
RIGHT-LEFT DOUBLE ROTATION

• When we have the situation where we have a


right inside grandchild (right-left) then we have
right left double rotation.
• A right-left double rotation is equivalent to a
sequence of two single rotations where the first
rotation on the original tree is a right rotation
between X’s right-child and grandchild and the
second rotation on this new tree is a left rotation
between X and its new right child.
• The figure shows the details of case 4.
• The left sub-tree of the right child of X violates the
property. First we need to rotate the tree RIGHT
about X’s right child (Y) and grandchild (Z) as
shown which is similar to single right rotation.
• Now we need to carry out the single left rotation
of z about x in the modified tree that is we rotate
the tree LEFT about X and its new right child (Z) .
• Figure shows the steps more in detail.
 The first pivot for the right rotation is right child (w) of the deepest unbalanced node (x)
 We carry out Single Right Rotation of v about the first pivot w
 Moreover during this single left rotation the sub-trees change positions from (T1 +
((T2+T3) + T4)) before the first rotation to positions (T1 + (T2 (T3 + T4)) after the
rotation still maintaining the BST property.
 The second pivot for the right rotation is the deepest unbalanced node itself x.
 We carry out Single Left Rotation of v about x
 Moreover during this single right rotation the sub-trees change positions from (T1 + (T2
(T3 + T4)) before the first rotation to positions ((T1 + T2) + (T3 + T4)) after the rotation
still maintaining the BST property.
B-TREE

• Properties of B-Tree:
• All leaves are at the same level.
• B-Tree is defined by the term minimum degree ‘t‘.
The value of ‘t‘ depends upon disk block size.
• Every node except the root must contain at least t-
1 keys. The root may contain a minimum of 1 key.
• All nodes (including root) may contain at most (2*t
– 1) keys.
• Number of children of a node is equal to the number of keys in it
plus 1.
• All keys of a node are sorted in increasing order. The child between
two keys k1 and k2 contains all keys in the range from k1 and k2.
• B-Tree grows and shrinks from the root which is unlike Binary
Search Tree. Binary Search Trees grow downward and also shrink
from downward.
• Like other balanced Binary Search Trees, the time complexity to
search, insert, and delete is O(log n).
• Insertion of a Node in B-Tree happens only at Leaf Node.
• Following is an example of a B-Tree of minimum
order 5 .
HEAP

• Heap is a complete binary tree data structure with the property that the value of a
parent node is either greater than or equal to (max heap) or less than or equal to
(min-heap) the values of its children.
• Heaps are used for efficient algorithms in sorting, selection, and as a priority queue.
• Each node of the tree corresponds to an element of the array. The tree is completely
filled on all levels except possibly the lowest, which is filled from the left up to a point.
• An array A[1:n] that represents a heap is an object with an attribute A:heap-size,
which represents how many elements in the heap are stored within array A. That is,
although A[1:n] may contain numbers, only the elements in A[1:n,heap-size], where
0<=A.heap-size <= n, are valid elements of the heap.
• If A:heap-size = 0, then the heap is empty. The root of the tree is A[1] and given the
index i of a node, there’s a simple way to compute the indices of its parent, left child,
and right child with the one-line procedures PARENT, LEFT, and RIGHT.
HEAP REPRESENTATIONS

MAX
HEA • A[Parent(i)>
=A[i]]
P
MIN • A[Parent(i),=A[i]]
HEA
P
MAINTAINING THE HEAP
PROPERTY

• The procedure MAX-HEAPIFY on the facing page maintains


the max-heap property.
• Its inputs are an array A with the heap-size attribute and an
index i into the array.
• MAX-HEAPIFY assumes that the binary trees rooted at LEFT.[i]
and RIGHT[i] are max-heaps, but that A[i] might be smaller
than its children, thus violating the max-heap property.
• MAX-HEAPIFY lets the value at A[i] lets the value float down
in the max-heap so that the subtree rooted at index i obeys
the maxheap property.
EXAMPLE

• The action of MAX-HEAPIFY.(A,2) where A:heap-size = 10. The node that potentially
violates the max-heap property is shown in blue.
• (a) The initial configuration, with A[2] at node i =2 violating the max-heap property since
it is not larger than both children.
• The max-heap property is restored for node 2 in (b) by exchanging A[2]with A[4], which
destroys the max-heap property for node 4.
• The recursive call MAX-HEAPIFY.(A,4) now has i =4.
• After A[4] and A[9] are swapped, as shown in (c), node 4 is fixed up, and the recursive call
MAX-HEAPIFY A[9] yields no further change to the data structure.
T (n)= O(lg n) (By case
2 of master theorem)
WHAT IS HEAP SORT IN DATA
STRUCTURE?

• Heap sort is a comparison-based sorting algorithm


that sorts an array in ascending or descending order
by transforming it into a binary heap data structure.
• It uses the concepts of max-heap and min-heap,
where in max-heap, the largest element is the root
node, and in min-heap, the smallest element is the
root node.
• The algorithm uses the binary heap structure to
efficiently sort the elements.
• The HEAPSORT procedure takes O(n lg n) time, since the call
to BUILD-MAXHEAP takes O(n) time and each of the n-1 calls
to MAX-HEAPIFY takes O(lg n) time.
REFERENCE MATERIAL

You might also like