Beruflich Dokumente
Kultur Dokumente
using ’C’
Presented By
Sudhanshu pathak
Page 1
Unit 3
• Trees: Basic terminology, Binary Trees, Binary tree
representation, algebraic Expressions, Complete
Binary Tree. Extended Binary Trees, Array and
Linked Representation of Binary trees, Traversing
Binary trees.
Page 2
Unit 4
• Sorting: Insertion Sort, Bubble Sort, Quick Sort,
Two Way Merge Sort, Heap Sort, Sorting on
Different Keys.
Page 3
TREE
• In computer science, a tree is a widely
used abstract data type (ADT) or data
structure implementing this ADT that simulates a
hierarchical tree structure, with a root value and
subtrees of children, represented as a set of linked
nodes.
Page 4
Tree Terminology
• Nodes: the elements in the tree
• Edges: connections between nodes
• Root: the distinguished element that is the origin of the
tree. There is only one root node in a tree
• Leaf node: a node without an edge to another node
• Interior node: a node that is not a leaf node
• Empty tree has no nodes and no edges
Page 5
Tree Terminology
• Parent or predecessor: the node directly above in
the hierarchy. A node can have only one parent
• Child or successor: a node directly below in the
hierarchy
• Siblings: nodes that have the same parent
• Ancestors of a node: its parent, the parent of its
parent, etc.
Page 6
Height of a Tree
• Height of a (non-empty) tree : length of the
longest path from the root to a leaf
Page 7
Level of a Node
• Level of a node : number of edges between
root and node
• It can be defined recursively:
• Level of root node is 0
• Level of a node that is not the root node is
level of its parent + 1
Page 8
Level of a Node
Level 0
Level 1
Level 2
Level 3
Page 9
Subtrees
• Subtree of a node: consists of a child node
and all its childs
• A subtree is itself a tree
• A node may have many subtrees
Page 10
Subtrees
Subtrees of
the root node
Page 11
Subtrees Subtrees of the
node labeled E
Page 12
More Tree Terminology
• Degree of a node: the number of children it
has
Page 13
Binary Trees
• General tree: a tree each of whose nodes may
have any number of children
• Binary tree: a tree each of whose nodes may
have no more than 2 children
• i.e. a binary tree is a tree with degree 2
• The children (if present) are called the left
child and right child
Page 14
Binary Tree
B C
D E F G
H I
Page 15
A null tree
is a tree
with no
nodes
Page 16
Tree Traversals
• A traversal of a tree requires that each node of
the tree be visited once
• Example: a typical reason to traverse a tree is
to display the data stored at each node of the
tree
• Standard traversal orderings:
• preorder
• inorder
• postorder
Page 17
Preorder Traversal
• Start at the root
• Visit each node, followed by its children; we will
choose to visit left child before right
Page 18
Inorder Traversal
• Start at the root
• Visit the left child of each node, then the node,
then any remaining nodes
Page 19
Postorder Traversal
• Start at the root
• Visit the children of each node, then the node
Page 20
Page 21
Binary Tree for Expressions
Page 22
22
Binary Tree Representation
• Array representation
• Linked representation
Page 23
Array Representation of Binary
Tree
• The binary tree is represented in an array
by storing each element at the array
position corresponding to the number
assigned to it.
Page 24
24
Incomplete Binary Trees
Page 25
Linked Representation of Binary
Tree
• The most popular way to present a binary tree
• Each element is represented by a node that has
two link fields (leftChild and rightChild) plus an
element field
• Each binary tree node is represented as an object
whose data type is binaryTreeNode
• The space required by an n node binary tree is
n * sizeof(binaryTreeNode)
Page 26
26
Binary Tree Node
• A binary tree node will contain
• a reference to a data element
• references to its left and right children
left and right children are binary tree nodes themselves
Page 27
10-
Linked Representation of Binary
Tree
Page 28
28
Complete Binary Tree
Page 29
Binary search tree
• In computer science, a binary search tree (BST),
sometimes also called an ordered or sorted binary
tree, is a node-based binary tree data structure
which has the following properties
• The left subtree of a node contains only nodes with
keys less than the node's key.
• The right subtree of a node contains only nodes with
keys greater than the node's key..
• There must be no duplicate nodes.
Page 30
Example Of BST
Page 31
AVL tree
• In computer science, an AVL tree is a self-balancing
binary search tree, and it was the first such data
structure to be invented. In an AVL tree, the heights
of the two child subtrees of any node differ by at
most one; if at any time they differ by more than
one, rebalancing is done to restore this property
Page 32
Example Of AVL
The tree is AVL because each
node has a balancing factor
either 0 or -1 or 1
Page 33
AVL Rotations
• Left Left
• Right Right
• Left Right
• Right Left
Page 34
Threaded Binary Tree
Page 35
Example Threaded binary Tree
Page 36
Linear search
• linear search or sequential search is a method for
finding a particular value in a list, that consists of
checking every one of its elements, one at a time
and in sequence, until the desired one is found.
Page 37
Linear search
1. Repeat step 2 while i<n
2. if array[i]=item then
found=1
exit while
end if
End of loop[step -1]
3.If found =1 then
print “Found”
else
print "Not found”
end if
4.exit
Page 38
Binary Search
• A binary search or half-interval search algorithm finds
the position of a specified value (the input "key")
within a sorted array .In each step, the algorithm
compares the input key value with the key value of
the middle element of the array. If the keys match,
then a matching element has been found so its index,
or position, is returned.
Page 39
int binarySearch(int *arr, int value, int left, int right) {
while (left <= right) {
int middle = (left + right) / 2;
if (arr[middle] == value)
return middle;
else if (arr[middle] > value)
right = middle - 1;
else
left = middle + 1;
}
return -1;
}
Page 40
int binarySearch(int array[], int value, int left, int right) {
if (left > right)
return -1;
int middle = (left + right) / 2;
if (array[middle] == value)
return middle;
else if (array[middle] > value)
return binarySearch(array, value, left, middle - 1);
else
return binarySearch(array, value, middle + 1, right);
}
Page 41
Quicksort
Page 43
Merge sort
mergesort( int [] a, int left, int right)
{
if (right > left)
{
middle = left + (right - left)/2;
mergesort(a, left, middle);
mergesort(a, middle+1, right);
merge(a, left, middle, right);
}
}
Page 44
Merge sort Example
Page 45
Insertion sort
function insertionSort(array:Array)
{
for(var i:int = 1; i < length;i++)
{
var value = array[i];
var j:int = i-1;
while(j >= 0 && array[j] > value)
{
array[j+1] = array[j];
j--;
}
array[j+1] = value;
}
return array;
}
Page 46
Insertion sort Example
Page 47
Bubble Sort
Page 48
Bubble Sort
Page 49
Heap Sort
Page 50
Example Heap Sort
Page 51
Hash Table
Page 52
Hash table
Page 53
Hash Functions
• Division method
• Mid-square method
• Folding method
Page 54
Collision Resolution
• Hash collisions are practically unavoidable when
hashing a random subset of a large set of possible
keys.
• Therefore, most hash table implementations have
some collision resolution strategy to handle such
events.
Page 55
Collision Resolution Strategy
Page 56