0% found this document useful (0 votes)
17 views76 pages

Week 5-6-7-8 DS

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

Week 5-6-7-8 DS

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

Trees

Instructor: Sameer Malik


Searching Algorithms
• Necessary components to search a list of data
• Array containing the list
• Length of the list
• Item for which you are searching

• After search completed


• If item found, report “success,” return location in array
• If item not found, report “not found” or “failure”
Cont….
• Suppose that you want to determine whether 27 is in the list
• First compare 27 with list[0]; that is, compare 27 with 35 .
• Because list[0] ≠ 27, you then compare 27 with list[1]
• Because list[1] ≠ 27, you compare 27 with the next element in the list.
• Because list[2] = 27, the search stops
• This search is successful!
Cont…..
• Let’s now search for 10.
• The search starts at the first element in the list; that is, at list[0].
• Proceeding as before, we see that this time the search item, which is 10, is
compared with every item in the list
• Eventually, no more data is left in the list to compare with the search item;
this is an unsuccessful search.
Linear Search Algorithm
int lineSearch(int [ ] list, int listLength, int key) {
int loc; boolean found = false;
for(loc = 0; loc < listLength; loc++) {
if ( list[loc] == key ) {
found = true;
break;
}
}
If ( found )
return loc;
else
return -1;
}
Cont….
int linSearch( int[ ] list, int listLength, int key) {
int loc;

for( loc = 0; loc < listLength; loc++ ) {


if ( list[loc] == key )
return loc;
}

return -1;
}
Cont….
• Using a while (or a for) loop, the definition of the method seqSearch can also be
written without the break statement as:

int linSearch(int[] list, int listLength, int key) {


int loc = 0; boolean found = false;
while( loc < listLength && !found ) {
if( list[loc] == key )
found = true;
else
loc++
}
if ( found )
return loc;
else
return -1;
}
Performance of the Linear Search
• Suppose that the first element in the array list contains the variable key, then we have
performed one comparison to find the key.

• Suppose that the second element in the array list contains the variable key, then we
have performed two comparisons to find the key.

• Carry on the same analysis till the key is contained in the last element of the array list.
In this case, we have performed N comparisons (N is the size of the array list) to find
the key.

• Finally if the key is NOT in the array list, then we would have performed N
comparisons and the key is NOT found and we would return -1.
Binary Search Algorithm

• Can only be performed on a sorted list !!!

• Uses divide and conquer technique to search list


Cont….
• Search item is compared with middle element of list

• If search item < middle element of list, search is restricted to


first half of the list

• If search item > middle element of list, search second half of the
list

• If search item = middle element, search is complete


Cont….
Determine whether 75 is in the list
Cont….
int binarySearch(int[] list, int listLength, int key) {
int first = 0, last = listLength ;
int mid;
boolean found = false;
while (first <= last && !found) {
mid = (first + last) / 2;
if (list[mid] == key)
found = true;
else
if(list[mid] > key)
last = mid - 1;
else
first = mid + 1; }
if (found)
return mid;
else
return –1;
} //end binarySearch
Cont….

key = 89

key = 34
Cont….

key = 22
Performance of Binary Search Algorithm

key ≠ List[499] key < List[499]


Cont….

key > List[249]


Cont…..
• Suppose that A is a list of size 1,048,576.

• To determine whether an element is in A, binary search makes at most


item comparisons

• In general, if A is a sorted list of size N, to determine whether an element


is in A, the binary search makes at most 2log2N + 2 key (item)
comparisons
Basic Tree Concepts
• A tree consists of finite set of elements, called nodes, and a finite set of
directed lines called branches, that connect the nodes.

• The number of branches associated with a node is the degree of the


node.
Cont….
Cont….
• When the branch is directed toward the node, it is indegree branch.

• When the branch is directed away from the node, it is an outdegree


branch.

• The sum of the indegree and outdegree branches is the degree of the node.

• If the tree is not empty, the first node is called the root.
Cont….
• The indegree of the root is, by definition, zero.

• With the exception of the root, all of the nodes in a tree must have an
indegree of exactly one; that is, they may have only one predecessor.

• All nodes in the tree can have zero, one, or more branches leaving them;
that is, they may have outdegree of zero, one, or more.
Cont….
• A leaf is any node with an outdegree of zero, that is, a node with no
successors.

• A node that is not a root or a leaf is known as an internal node.

• A node is a parent if it has successor nodes; that is, if it has outdegree


greater than zero.

• A node with a predecessor is called a child.


Cont….
• Two or more nodes with the same parents are called siblings.

• An ancestor is any node in the path from the root to the node.

• A descendant is any node in the path below the parent node; that is, all
nodes in the paths from a given node to a leaf are descendants of that
node.
Cont….
• A path is a sequence of nodes in which each node is adjacent to the next
node.

• The level of a node is its distance from the root. The root is at level 0, its
children are at level 1, etc. …

• The height of the tree is the level of the leaf in the longest path from the
root plus 1. By definition the height of any empty tree is -1.

• A subtree is any connected structure below the root. The first node in the
subtree is known is the root of the subtree.
Cont….
Cont….
Recursive definition of a tree
A tree is a set of nodes that either:

• is empty or

• has a designated node, called the root, from which hierarchically descend
zero or more subtrees, which are also trees.
Tree Representation

• General Tree – organization chart format

• Indented list – bill-of-materials system in which a parts list represents the


assembly structure of an item
Cont….
Cont….
Binary Trees
A binary tree can have no more than two descendents. In this
section we discuss the properties of binary trees, four different
binary tree traversals

• Properties
• Binary Tree Traversals
• Expression Trees
• Huffman Code
Cont….
• A binary tree is a tree in which no node can have more than two subtrees;
the maximum outdegree for a node is two.

• In other words, a node can have zero, one, or two subtrees.

• These subtrees are designated as the left subtree and the right subtree.
Cont….
Cont….
Some Properties of Binary Trees
• The minimum height of a binary tree is determined as follows:

H min = [ log 2 N ] + 1

For instance, if there are three nodes to be stored in the binary tree (N=3) then
Hmin=2.
Cont….
• The formula for the maximum number of nodes is derived from the fact that
each node can have only two descendents. Given a height of the binary tree,
H, the maximum number of nodes in the tree is given as follows:

N max = 2 - 1 H
Cont….
• The children of any node in a tree can be accessed by following only one
branch path, the one that leads to the desired node.

• The nodes at level 1, which are children of the root, can be accessed by
following only one branch; the nodes of level 2 of a tree can be accessed
by following only two branches from the root, etc.

• The balance factor of a binary tree is the difference in height between its
left and right subtrees:
Balance of the Tree
Some Properties of Binary Trees
• In the balanced binary tree (definition of Russian mathematicians
Adelson-Velskii and Landis) the height of its subtrees differs by no more
than one (its balance factor is -1, 0, or 1), and its subtrees are also
balanced.
Complete & Nearly Complete Binary Tress

• A complete tree has the maximum number of entries for its height. The
maximum number is reached when the last level is full.

• A tree is considered nearly complete if it has the minimum height for its
nodes and all nodes in the last level are found on the left
Cont….
Binary Search Tree Property
• Binary search tree property:
• If y is in left subtree of x,
then key [y] ≤ key [x]

• If y is in right subtree of x,
then key [y] ≥ key [x]

3 7
2 5 9
Cont….
• Support many dynamic set operations
• SEARCH, MINIMUM, MAXIMUM, PREDECESSOR, SUCCESSOR, INSERT,
DELETE

• Running time of basic operations on binary search trees


• On average: Q(lgn)
• The expected height of the tree is lgn

• In the worst case: Q(n)


• The tree is a linear chain of n nodes
Traversing a BST
• Inorder tree walk:
Root is printed between the values of its left and right subtrees: left, root, right
• Preorder tree walk:
Root printed first: root, left, right
• Postorder tree walk:
Root printed last: left, right, root

3 7
2 5 9
Searching for a Key
Given a pointer to the root of a tree and a key k:
• Return a pointer to a node with key k if one exists
• Otherwise return NIL

Idea: Starting at the root, trace down a path by comparing k with the key of the current node.
ü if the keys are equal: we have found the key
ü if k < key[x] search in the left subtree of x
ü if k > key[x] search in the right subtree of x

3 7
2 4 9
Example: Tree–Search

15
• Search for key 13:
6 18 • 15 ® 6 ® 7 ® 13

3 7 17 20
2 4 13
9
Searching for a Key
Alg: TREE-SEARCH(x, k)
5
1. if x = NIL or k == key [x]
3 7
2. then return x
2 4 9
3. if k < key [x]
4. then return TREE-SEARCH(left [x], k )
5. else return TREE-SEARCH(right [x], k )

Note: Running Time: O (h), Where h is the height of the tree


Finding the Minimum in a BST
• Goal: find the minimum value in a BST
• Following left child pointers from the root, until a NIL is
encountered
15

Alg: TREE-MINIMUM(x) 6 18

1. while left [x] ¹ NIL 3 7 17 20


2 4 13
2. do x ← left [x] 9
3. return x Minimum = 2

Note: Running Time: O (h), Where h is the height of the tree


Finding the Maximum in a BST
• Goal: find the maximum value in a BST
• Following right child pointers from the root, until a NIL is
encountered
15
Alg: TREE-MAXIMUM(x)
1. while right [x] ¹ NIL 6 18
3 7 17 20
2. do x ← right [x]
2 4 13
3. return x 9

Note: Running Time: O (h), Where h is the height of the tree


Insertion
• Goal:
• Insert value v into a binary search tree
• Idea: Insert value 13
• If key [x] < v move to the right child of x,
12
else move to the left child of x
• When x is NIL, we found the correct position 5 18
• If v < key [y] insert the new node as y’s left child 2 9 15 19

else insert it as y’s right child 1 3 17


13

• Begining at the root, go down the tree and maintain:


• Pointer x : traces the downward path (current node)
• Pointer y : parent of x (“trailing pointer” )
Deletion
• Goal:
• Delete a given node z from a binary search tree
• Idea:
• Case 1: z has no children
• Delete z by making the parent of z point to NIL

15 15

5 16 5 16

3 12 20 3 12 20

10 13 18 23 10 18 23

6 delete 6

7 7
Cont….
• Case 2: z has one child
• Delete z by making the parent of z point to z’s child, instead of to z

15 delete 15

5 16 5 20

3 12 20 3 12 18 23

10 13 18 23 10

6 6

7 7
Cont….
• Case 3: z has two children
• z’s successor (y) is the minimum node in z’s right subtree
• y has either no children or one right child (but no left child)
• Delete y from the tree (via Case 1 or 2)
• Replace z’s key and satellite data with y.

15 15
delete z
5 16 6 16

3 12 20 3 12 20

10 13 18 23 10 13 18 23

6 7

7
Cont….
Ø Tree is a non linear data structure to present data in
hierarchical form.
Ø It is also called acyclic data structure.
Ø The main component of a tree are node , branches, level,
family (subtree) and path.
Ø A node may be used as root, parent, left, brother,
predecessor , successor and leaf node.
Ø If every node of a tree have 0, 1 or 2 child it is called
binary tree.
Ø There ar different traversing method of tree such as
inorder, pre order and post order.
Cont….
ØBinary search tree is a special binary tree which is designed to
make the search of elements or keys in linear times.
ØFor each current parent node key, the values of all nodes of left
sub tree are less and of right sub tree are greater than from
current parent.
ØOperations on binary search trees:
• SEARCH O(h)
• MINIMUM O(h)
• MAXIMUM O(h)
• INSERT O(h)
• DELETE O(h)
• PREDECESSOR O(h)
• SUCCESOR O(h)
Graphs
Instructor: Sameer Malik
Introduction to Graphs
Graphs are important discrete structures because they are
a flexible mathematical model for many application
problems.

Any time there is

• a set of objects and

• there is some sort of "connection" or "relationship“ or


"interaction" between pairs of objects,

a graph is a good way to model this.


Applications
Applications that involve not only a set of items, but also the
connections between them.

Maps Computer networks


Schedules

Hypertext Circuits
Introduction to Graphs
Ø A graph G = ( V , E ) consists of
o a finite set of vertices V (or nodes) and
o E, a binary relation on V called edges.

Ø E is a set of pairs from V.

Ø If a pair is ordered, we have a directed graph.

Ø For unordered pair, we have an undirected graph.


Introduction to Graphs
Ø Directed and Undirected graphs
Ø Weighted and Un-weighted Graphs
Ø Nodes
• In-degree of a Node
• Out-degree of a Node
• Adjacent Nodes
Ø Path from one Node to other
Ø Cycle in a Graph
Ø Directed Acyclic Graph (DAG)
Introduction to Graphs
Adjacent Vertices
Ø A vertex w is adjacent to vertex v if there is an edge
from v to w.

Adjacent Vertices
1&2

1&3

1&4

2&4

Analysis of Algorithms by Saghir Ahmed 7


Incident Edge
Ø In an undirected graph, we say that an edge is
incident on a vertex if the vertex is an endpoint
of the edge.

e1 is incident on vertices 1 & 2

e2 is incident on vertices 1 & 3

e3 is incident on vertices 1 & 4

e4 is incident on vertices 2 & 4

Analysis of Algorithms by Saghir Ahmed 8


Degree of Vertex
■ In a digraph, the number of edges coming out of a
vertex is called the out-degree of that vertex

■ Number of edges coming in is the in-degree

■ In an undirected graph, we just talk of degree


of a vertex. It is the number of edges incident
on the vertex

Analysis of Algorithms by Saghir Ahmed 9


Degree of Vertex

in: 1 in: 1
out: 3 out: 1

out: 2 out: 0

Analysis of Algorithms by Saghir Ahmed 10


Observations about Graphs
For a diagraph G = (V , E),

Σin-degree(v) + Σout-degree(v) = | E |
Where | E | means the cardinality of the set E, i.e.
the number of edges.

Analysis of Algorithms by Saghir Ahmed 11


Observations about Graphs
For an undirected graph G = (V , E),

Σ degree(v) = 2| E |
Where | E | means the cardinality of the set E, i.e.
the number of edges.

Analysis of Algorithms by Saghir Ahmed 12


Path & Cycles
• A path in a directed graphs is a sequence of
vertices (v0,v1, … , vk) such that (vi-1, vi) is an edge
for i = 1 , 2 , . . . . , k.

• The length of the paths is the number of edges, k.

• A vertex w is reachable from vertex u if there is a


path from u to w.

• A path is simple if all vertices (except possibly the


first and last) are distinct.
Path & Cycles
• A graph is said to be acyclic if it contains no cycles.

• A graph is connected if every vertex can reach


every other vertex.

• A directed graph that is acyclic is called a directed


acyclic graph (DAG).

Analysis of Algorithms by Saghir Ahmed 14


Graph Representations
There are two ways of representing graphs
• Adjacency matrix
• Adjacency list

Let G = ( V , E ) be a digraph with n = | V | and


let e = | E |.

We will assume that the vertices of G are


indexed {1, 2, ….. , n}.

Analysis of Algorithms by Saghir Ahmed 15


Graph Representations
Adjacency Matrix
An adjacency matrix is a n x n matrix defined
for 1 ≤ v , w ≤ n.

ì
A( v , w ) = í
1 if (v, w) Î E
î0 otherwise

Adjacency matrix requires Θ (n2) storage.

Analysis of Algorithms by Saghir Ahmed 16


Adjacency Matrix

Analysis of Algorithms by Saghir Ahmed 17


Graph Representations
Adjacency List
An array Adj[ 1 . . . n ] of pointers where
for 1 ≤ v ≤ n, Adj[v] points to a linked list
containing the vertices which are adjacent to v.

Adjacency list requires Θ( n + e ) storage.

Analysis of Algorithms by Saghir Ahmed 18


Adjacency List

Analysis of Algorithms by Saghir Ahmed 19

You might also like