Sie sind auf Seite 1von 56

Data Structures

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.

• Searching and Hashing: Sequential search, binary


search,, Hash Table, Hash Functions, Collision
Resolution Strategies, Hash Table Implementation.

Page 2
Unit 4
• Sorting: Insertion Sort, Bubble Sort, Quick Sort,
Two Way Merge Sort, Heap Sort, Sorting on
Different Keys.

• Binary Search Trees: Binary Search Tree (BST),


Insertion and Deletion in BST, Path Length, AVL
Trees

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

Height of the tree


is 3

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

• Degree of a tree: the maximum of the


degrees of the tree’s nodes

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

• Recursive algorithm for preorder traversal:


• If tree is not empty,
• Visit root node of tree
• Perform preorder traversal of its left subtree
• Perform preorder traversal of its right subtree

Page 18
Inorder Traversal
• Start at the root
• Visit the left child of each node, then the node,
then any remaining nodes

• Recursive algorithm for inorder traversal


• If tree is not empty,
• Perform inorder traversal of left subtree of root
• Visit root node of tree
• Perform inorder traversal of its right subtree

Page 19
Postorder Traversal
• Start at the root
• Visit the children of each node, then the node

• Recursive algorithm for postorder traversal


• If tree is not empty,
• Perform postorder traversal of left subtree of root
• Perform postorder traversal of right subtree of root
• Visit root node of tree

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

• A complete binary tree is a binary tree in which


every level, except possibly the last, is
completely filled.

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

• "A binary tree is threaded by making all right


child pointers that would normally be null point to
the inorder successor of the node, and all left
child pointers that would normally be null point to
the inorder predecessor of the node."

Page 35
Example Threaded binary Tree

A threaded binary tree makes it


possible to traverse the values in
the binary tree via a linear
traversal that is more rapid than
a recursive in-order traversal.

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

quicksort( void *a, int low, int high )


{
int pivot; /* Termination condition! */
if ( high > low )
{
pivot = partition( a, low, high );
quicksort( a, low, pivot-1 );
quicksort( a, pivot+1, high );
}
} Page 42
Quicksort Example

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

When humans manually sort


something (for example, a deck of
playing cards), most use a method that
is similar to insertion sort.

Page 47
Bubble Sort

void bubble_sort(int *a, int n)


{
int j, t = 1;
while (n-- && t)
for (j = t = 0; j < n; j++)
{
if (a[j] <= a[j + 1])
continue;
t = a[j], a[j] = a[j + 1], a[j + 1] = t;
t=1;
}
}

Page 48
Bubble Sort

Page 49
Heap Sort

Page 50
Example Heap Sort

Page 51
Hash Table

• In computing, a hash table (also hash map) is a data


structure used to implement an array, a structure
that can map keys to values. A hash table uses
a hash function to compute an index into an array of
buckets or slots, from which the correct value can be
found.

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

• Linear addressing (open hashing)


• Double hashing
• Chaining

Page 56

Das könnte Ihnen auch gefallen