Professional Documents
Culture Documents
Computing Part III
Computing Part III
Computing Part III
Part III
Data Organization and Utilization
Just To Remember !
Process
Input ( Computing ) Output
إذا التجريد يشمل نمدجة بيانات العالم الحقيقي في تركيبة بيانية واحدة و إخفاء
التفاصيل عن المستخدم .هذا يعني إنه إستحداث للنوع:
-إستحداث نوع لهذه التركيبة البيانية
-تعريف و تحديد عناصرها
-تحديد العمليات عليها
-راجع األمثلة اآلتية بلغة السي:
Abstraction أ لتجريدcont.
To build a type for complex numbers: X + iY
struct ComNum{ float x ; real imaginary
float y; data structure
};
typedef struct ComNum Complex; new type
Operations
(algorithms)
I/O System
Outside world
Data Utilization Cont.
• A program= set of algorithms on a set of data structures
written in a programming language.
• An algorithm = operation on data structure
• An algorithm is a set of steps define the problem solution.
• A problem is solvable algorithmically (computable) if a
computer program can be written and will produce correct
output for any input run for a sufficient time and allocated
enough memory storage.
• From algorithm to program to process.
• The program during execution (process) should be in the main
memory.
• A program at its static state is on a storage media.
Problem Solving and Algorithm Efficiency
• Time and storage space are important factors. They are
the measures of Computational Complexity.
• Algorithms criteria:
1. Correctness 2. Amount of work done 3. Amount of space
used 4. Simplicity and clarity 5. Optimality
• Well defined and selected Data structures :
- Arrays and Matrices - Records - Queues (simple queue,
circular queue, buffers) - Stacks
- Lists (simple linked lists, bidirectional linked lists, circular linked
lists, multi-ways linked lists) - Trees (binary trees, general Trees,
Binary Search trees, Binary sort trees ) - Graphs ( undirected
graphs, directed graphs, un weighted graphs, weighted graphs. -
hash tables
• Algorithms to investigate: searching , sorting for the selected DS.
C Features
• It is a modular, imperative, general purpose programming
language.
• Developed for systems programming by Ritchie and Karinghan
1970 UNIX OS.
• Known as ANSI C / R&K C.
• Basic data types int char float double
• Compound data types arrays, struct, union
• Data type abstraction using typedef and struct.
• Program modules functions with clear interface
• Program Entry point and also exit point main function
• I/O operations are performed using standard library functions
such as scanf, printf, getchar, ….
• Any name followed by ( ) is considered as a function.
C functions
• To work with functions in C you must consider:
(1) function prototype (declaration or header file)
(2) function definition ( code module)
(4) function call ( in the calling module)
• In these three considerations:
- the function name should be the same
- the returned type should be the same
- the parameters should be the same for type, number and order.
• The function code module structure is as shown:
head / interface
Returned type name ( parameters)
Body {
variables type
code statements
returned value
return value
}
C function example
• The following function computes the sum of two complex numbers.
It implements one of the operations on complex numbers. Assume
that we have already built an abstract data type Complex :
First last
<x xx >x
10 15 10 15 15 17 18 22 22 25 25 44
25
Trees cont.
Binary Search trees, Binary sort trees
• Nodes implementation:
NODPTR maketree(int x) {
NODPTR p;
P=(NODPTR) malloc(sizeof(struct nodetype));
p->data = x;
p->left= NULL;
p->right=NULL;
p-> father= NULL;
return p;
}
void setleft( NODPTR p, int x) {
if(p==NULL) printf(“No insertion”);
else if(p->left !=NULL) printf(“Invalid insertion”);
else {
p->left= maketree(x)
(p->left)->father= p; }
}
void setright( ) { // complete it }
Trees cont.
Binary Search trees, Binary sort trees
• Traversal Algorithms Implementations:
Void pretrav(NODPTR tree) {
If(tree!=NULL) {
Printf(“%d\n”,tree->data); // visit the root
Pretrav(tree->left); // traverse left subtree
Pretrav(tree->right); // traverse right subtree
} /* end if */
} /* end pretrav */
Void intrav(NODPTR tree) {
If(tree!=NULL) {
intrav(tree->left); // traverse left subtree
Printf(“%d\n”,tree->data); // visit the root
intrav(tree->right); // traverse right subtree
} /* end if */
} /* end intrav */
Void posttrav(NODPTR tree) {
If(tree!=NULL) {
Posttrav(tree->left); // traverse left subtree
Posttrav(tree->right); // traverse right subtree
Printf(“%d\n”,tree->data); // visit the root
} /* end if */
Trees
Binary Search trees, Binary sort trees
• Binary Tree Searching : Binary trees are used to organize data in a
sorted form if it is retrieved using inorder traversal algorithm. Binary
trees are also used for binary search as shown by the following
algorithm:
A B C D E A B C D E
A 0 0 1 1 0 A 0 0 0 1 1
B 0 0 1 0 0 B 0 0 0 1 1
C 0 0 0 1 1 C 0 0 0 1 1
D 0 0 0 0 1 D 0 0 0 1 0
E 0 0 0 1 0 E 0 0 0 0 1
Adjacent matrix with length=1 adjacent matrix with length 2
( 1 indicates an edge between two nodes)
END OF P A R T III