Topic 2.2: Non-Linear Data Structures
Topic 2.2: Non-Linear Data Structures
Non-linear
Non-linear Data
Data
Structures
Structures
• 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 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
• 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
• 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
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)
• 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
• 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
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
• 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.
• 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 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?