You are on page 1of 265

ALGORITHMS AND

DATA STRUCTURE
‫محور الخوارزميات وبنى المعطيات‬

2021 By Eng. Joud Khattab 1


Content

■ OOP. (‫)البرمجة غرضية التوجه‬


■ Complexity. (‫)التعقيد‬
■ Recursive Functions. (‫)التوابع العودية‬
■ Data Structure: (‫)بنى المعطيات‬
– Sequential: Linked List, Stacks, Queue.
– Non Sequential: Tree, Graph.
■ Sort & Search Algorithms. (‫)خوارزميات البحث والترتيب‬
■ Advance Algorithms Methods (‫)خوارزميات متقدمة‬
– Greedy, Backtracking, Dynamic, Divide & Conquer.
■ Hashing Function. (‫)توابع التقطيع‬
■ MCQ. (‫)اختبارات‬

2021 By Eng. Joud Khattab 2


‫)تعاريف( ‪Definitions‬‬

‫■ خوارزمية حل مسألة‬
‫– هي توصيف صوري لطريقة الحل على شكل متتالية منتهية من العمليات البسيطة‪ ،‬تنفذ‬
‫حسب تسلسل محدد‪.‬‬
‫■ البرنامج‬
‫هو توصيف لخوارزمية حل مسألة معينة بإحدى لغات البرمجة التي يقبلها الحاسوب‪.‬‬
‫■ لغة البرمجة‬
‫– هي مجموعة من المفردات والقواعد والدالالت المعرفة التي تسمح بكتابة برنامج يمكن‬
‫تنفيذه على الحاسوب‪.‬‬
‫■ المترجم‬
‫– هو برنامج يفهم البرنامج المكتوب بلغة برمجة معينة‪ ،‬ويحوله إلى برنامج مكافئ مكتوب‬
‫بلغة المجمع الخاصة بالمعالج الصغري للحاسوب‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪3‬‬


OBJECT ORIENTED
PROGRAMMING (OOP)
‫البرمجة غرضية التوجه‬

2021 By Eng. Joud Khattab 4


‫)تعاريف( ‪Definitions‬‬

‫■ الصف أو الفئة (‪)class‬‬


‫– الصف عبارة عن حاوية كبيرة تستطيع أن تحتوي كل الكود من متغيرات ودوال‬
‫وكائنات إلخ‪...‬‬
‫■ الكائن أو الغرض (‪)object‬‬
‫– الكائن عبارة عن نسخة مطابقة لصف معين‪.‬‬
‫– يمكننا القول إنه ال يمكن إنشاء كائن إذا لم يكن هناك صف‪.‬‬
‫– في مفهوم برمجة الكائنات نقوم بإنشاء صف معين يسمونه (‪ )blue print‬أي‬
‫(النسخة الخام أو النسخة األصلية)‪ ،‬وبعدها ننشئ نسخة أو أكثر من هذا الصف‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪5‬‬


Definitions (‫)تعاريف‬

2021 By Eng. Joud Khattab 6


‫)تعاريف( ‪Definitions‬‬

‫■ الخصائص (‪)instance variables‬‬


‫كل كائن ننشئه من صف معين يملك نسخة خاصة به من المتغيرات الموجودة في هذا‬ ‫–‬
‫الصف‪.‬‬
‫المتغيرات التي يتم إعطاء نسخة منها لكل كائن من الصف هي ما تسمى بالخصائص‪.‬‬ ‫–‬
‫■ الدوال (‪)methods‬‬
‫الدالة عبارة عن كود جاهز يتنفذ فقط عندما تقوم باستدعائه‪.‬‬ ‫–‬
‫■ البيانات نوعين‪:‬‬
‫– البيانات البدائية (‪ :)primitive data types‬كال ‪byte – short – int – long – float – bool‬‬
‫– البيانات المرجعية (‪ :)reference/object data types‬كالكائنات وأي نوع نضع له الكلمة‬
‫‪ new‬عند تعريفه‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪7‬‬


‫)تعاريف( ‪Definitions‬‬

‫التابع المشيد أو البناء (‪)constructor‬‬ ‫■‬


‫– عبارة عن دالة لها نوع خاص‪ ،‬يتم استدعائها أثناء إنشاء كائن لتوليد قيم أولية للخصائص الموجودة‬
‫فيه‪.‬‬
‫مهمة حول ال ‪constructor‬‬ ‫نقاط‬ ‫■‬
‫كل صف يتم إنشاءه‪ ،‬يحتوي على ‪ constructor‬واحد على األقل‪ .‬وحتى إن لم تقم بتعريف أي‬ ‫–‬
‫‪ constructor‬سيقوم المترجم بإنشاء واحد افتراضي عنك‪.‬‬
‫في كل مرة يتم إنشاء كائن جديد‪ ،‬يجب استدعاء ‪ constructor‬حتى يتم إنشاء هذا الكائن‪.‬‬ ‫–‬
‫القاعدة األساسية عند تعريف ‪ constructor‬هي أنه يجب أن يحمل نفس اسم الصف ويكون نوعه‬ ‫–‬
‫‪public‬‬
‫في حال قمت بتعريف ‪ constructor‬لن يقوم المترجم بإنشاء واحد افتراضي‪ ،‬أي لن يعود هناك‬ ‫–‬
‫‪default constructor‬‬
‫يمكنك تعريف أكثر من ‪ .constructor‬ويمكنك دائما إنشاء ‪ constructor‬فارغ‪ ،‬حتى تستخدمه إن‬ ‫–‬
‫كنت ال تريد إعطاء قيم أولية محددة للخصائص عند إنشاء كائن‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪8‬‬


‫)تعاريف( ‪Definitions‬‬

‫■ المتغيرات التي يتم وضعها في الصف تقسم إلى ثالث فئات أساسية‪:‬‬
‫– ‪ :Local variables‬هي المتغيرات التي يتم تعريفها بداخل أي دالة‪.‬‬
‫– ‪ :Instance variables‬هي المتغيرات التي يتم تعريفها بداخل الكالس وخارج حدود‬
‫أي دالة‪.‬‬
‫– ‪ :Class variables‬هي المتغيرات التي يتم تعريفها ك ‪ static‬بداخل الكالس‪ .‬وخارج‬
‫حدود أي دالة‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪9‬‬


‫)تعاريف( ‪Definitions‬‬

‫■ مفهوم ‪ superclass‬و ‪subclass‬‬


‫– الصف الذي يرث من صف أخر يسمى ‪ subclass‬ويسمى أيضا (‪ derived class‬او‬
‫‪ extended class‬او ‪)child class‬‬
‫– الصف الذي يورث محتوياته لصف أخر يسمى ‪ superclass‬ويسمى أيضا (‪base class‬‬
‫او ‪)parent class‬‬
‫■ مفهوم ‪super‬‬
‫– تستخدم للتمييز بين المتغيرات والدوال الموجودة في ال ‪ superclass‬و ‪subclass‬‬
‫في حال كانت األسماء مستخدمة في كال الصفين‪.‬‬
‫– تستخدم الستدعاء المشيد الموجود في ال ‪superclass‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪10‬‬


‫‪Question 1‬‬

‫■ يمكن تطبيق مفهوم البرمجة غرضية التوجه في البرامج بدون استخدام الصفوف‬
‫‪ .A‬صح‬
‫‪ .B‬خطأ‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪11‬‬


‫‪Question 1‬‬

‫■ يمكن تطبيق مفهوم البرمجة غرضية التوجه في البرامج بدون استخدام الصفوف‬
‫‪ .A‬صح‬
‫‪ .B‬خطأ‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪12‬‬


Question 2

‫) وليست موجودة في البنية‬classes( ‫■ ما هي الخاصية اإلضافية الموجودة في الصفوف‬


)struct(
A. Data members
B. Member functions
C. Static data allowed
D. Public access specifier

2021 By Eng. Joud Khattab 13


Question 2

‫) وليست موجودة في البنية‬classes( ‫■ ما هي الخاصية اإلضافية الموجودة في الصفوف‬


)struct(
A. Data members
B. Member functions
C. Static data allowed
D. Public access specifier

2021 By Eng. Joud Khattab 14


Question 3

)class( ‫■ ما هو التعريف األفضل للصفوف‬


A. Parent of an object
B. Instance of an object
C. Blueprint of an object
D. Scope of an object

2021 By Eng. Joud Khattab 15


Question 3

)class( ‫■ ما هو التعريف األفضل للصفوف‬


A. Parent of an object
B. Instance of an object
C. Blueprint of an object
D. Scope of an object

2021 By Eng. Joud Khattab 16


Note
What Is a Class? (‫)ما هو الصف‬
■ In the real world, you'll often find many individual objects all of the same kind.
■ There may be thousands of other bicycles in existence, all of the same make and
model.
■ Each bicycle was built from the same set of blueprints and therefore contains the
same components.
■ In object-oriented terms, we say that your bicycle is an instance of the class of
objects known as bicycles.
■ A class is the blueprint from which individual objects are created.

2021 By Eng. Joud Khattab 17


Question 4

‫■ ما هي الخاصية الموجودة في البرمجة غرضيه التوجه التي توضح وتركز على مفهوم‬
)reusability( ‫إعادة االستخدام في البرمجة‬
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance

2021 By Eng. Joud Khattab 18


Question 4

‫■ ما هي الخاصية الموجودة في البرمجة غرضيه التوجه التي توضح وتركز على مفهوم‬
)reusability( ‫إعادة االستخدام في البرمجة‬
A. Polymorphism
B. Abstraction
C. Encapsulation
D. Inheritance

2021 By Eng. Joud Khattab 19


Question 6

)Encapsulation( ‫■ ما هو التعريف المناسب لشرح مفهوم التغليف‬


A. It is a way of combining various data members into a single unit
B. It is a way of combining various member functions into a single unit
C. It is a way of combining various data members and member functions into a
single unit which can operate on any data
D. It is a way of combining various data members and member functions that
operate on those data members into a single unit

2021 By Eng. Joud Khattab 20


Question 6

)Encapsulation( ‫■ ما هو التعريف المناسب لشرح مفهوم التغليف‬


A. It is a way of combining various data members into a single unit
B. It is a way of combining various member functions into a single unit
C. It is a way of combining various data members and member functions into a
single unit which can operate on any data
D. It is a way of combining various data members and member functions that
operate on those data members into a single unit

2021 By Eng. Joud Khattab 21


Question 7

‫■ أي من الميزات التالية تتناسب مع بعضها البعض؟‬


A. Inheritance and Encapsulation
B. Encapsulation and Polymorphism
C. Encapsulation and Abstraction
D. Abstraction and Polymorphism

2021 By Eng. Joud Khattab 22


Question 7

‫■ أي من الميزات التالية تتناسب مع بعضها البعض؟‬


A. Inheritance and Encapsulation
B. Encapsulation and Polymorphism
C. Encapsulation and Abstraction
D. Abstraction and Polymorphism

2021 By Eng. Joud Khattab 23


Note
OOP Features (‫)خصائص البرمجة غرضية التوجه‬

■ Abstraction (‫)التجريد‬
– Abstraction is a process where you show only “relevant” data and “hide”
unnecessary details of an object from the user.
■ Encapsulation (‫)التغليف‬
– Encapsulation is binding the data with the code that manipulates it. It keeps the
data and the code safe from external interference.
■ Inheritance (‫)الوراثة‬
– Inheritance is the mechanism by which an object acquires the some/all properties
of another object. It supports the concept of hierarchical classification.
■ Polymorphism (‫)تعدد األوجه‬
– Polymorphism means to process objects differently based on their data type.

2021 By Eng. Joud Khattab 24


‫‪Note‬‬
‫)خصائص البرمجة غرضية التوجه( ‪OOP Features‬‬
‫مفهوم التجريد (‪)abstraction‬‬ ‫■‬
‫– هو أسلوب يستخدم لتسهيل كتابة األوامر على المبرمجين‪ ،‬فهو يجعلك قادرا على تنفيذ‬
‫ما تريد دون الحاجة إلى معرفة كافة التفاصيل التي تم فيها تنفيذ ذلك‪.‬‬
‫– اذا ال ‪ abstraction‬يجعلك تتعامل مع األشياء بسطحية بدل أن تغوص في معرفة تفاصيل‬
‫الكودات المعقدة‪.‬‬
‫مفهوم التغليف (‪)encapsulation‬‬ ‫■‬
‫– هو أسلوب يمكن اتباعه إلخفاء البيانات األساسية في الصف‪ ،‬أي إلخفاء الخصائص‬
‫الموجودة فيه (‪ ،)global variables‬وجعل الصفوف األخرى قادرة على التعامل مع هذه‬
‫الخصائص فقط من خالل دوال يقوم بإنشائها المبرمج األساسي للصف‪.‬‬
‫– لتحقيق مبدأ التغليف‪ ،‬عليك تعريف الخصائص ك ‪ private‬وتعريف الدوال التي تستخدم‬
‫للوصول اليهم ك ‪public‬‬
‫– مثال على التغليف‪setter and getter :‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪25‬‬


‫‪Note‬‬
‫)خصائص البرمجة غرضية التوجه( ‪OOP Features‬‬

‫■ مفهوم ال ‪polymorphism‬‬
‫– المقصود منه بناء دالة تنفذ أوامر مختلفة على حسب الكائن الذي يمرر لها ك‬
‫‪argument‬‬
‫– تعني قدرة الكائن على أخذ عدة اشكال‪ ،‬تحديدا قدرة الكائن على التحول إلى نوع‬
‫مشتق منه‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪26‬‬


Note
Polymorphism
■ Overloading (Static Polymorphism)
– Overloading in simple words means more than one method having the same
method name that behaves differently based on the arguments passed while
calling the method.
■ Overriding (Dynamic Polymorphism)
– Overriding means a derived class is implementing a method of its super class.
The call to overridden method is resolved at runtime, thus called runtime
polymorphism.

2021 By Eng. Joud Khattab 27


‫‪Note‬‬
‫‪Overriding‬‬
‫‪■ Overriding:‬‬
‫– تعني تعريف الدالة التي ورثها ال ‪ subclass‬من ال ‪ superclass‬من جديد‪.‬‬
‫– هذه الدالة الجديدة تكون مشابهة للدالة الموروثة من حيث الشكل فقط‪ .‬أي لها‬
‫نفس االسم والنوع وعدد الباراميترات لكن محتواها مختلف‪.‬‬
‫– الهدف هو اتاحة الفرصة لل ‪ subclass‬ليعرف الدوال حسب حاجته‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪28‬‬


‫‪Note‬‬
‫‪Overriding‬‬
‫■ شروط ال ‪:overriding‬‬
‫– يجب أن يكون ال ‪ modifier‬المستخدم للدالة الجديدة هو نفسه المستخدم للدالة‬
‫القديمة‪ ،‬ويجب أن يكون نوعه ‪ public‬او ‪.protected‬‬
‫– الدالة المعرفة ك ‪ final‬ال يمكن أن نفعل لها ‪ ،override‬ألن كلمة ‪ final‬تمنع تغير‬
‫محتوى الدالة بعد تعريفها‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪29‬‬


Question 8

‫) وال تدعم تعدد االشكال‬classes( ‫■ ماذا تسمي اللغة التي تدعم الصفوف‬
‫)؟‬polymorphism(
A. Class based language
B. Procedure oriented language
C. Object based language
D. If classes are supported, polymorphism will always be supported

2021 By Eng. Joud Khattab 30


Question 8

‫) وال تدعم تعدد االشكال‬classes( ‫■ ماذا تسمي اللغة التي تدعم الصفوف‬
‫)؟‬polymorphism(
A. Class based language
B. Procedure oriented language
C. Object based language
D. If classes are supported, polymorphism will always be supported

2021 By Eng. Joud Khattab 31


Note
Programming Paradigm
■ The main programming paradigms:
1. Imperative
■ Cobol, Fortran, C, Ada, Perl
2. Object-oriented
■ Smalltalk, Java, C++, C#, Python
3. Functional
■ Lisp, Scheme, ML, Haskell
4. Logic
■ Prolog
5. Declarative
■ SQL, HTML

2021 By Eng. Joud Khattab 32


Question 9

■ If data members are private, what can we do to access them from the class object?
A. Create public member functions to access those data members
B. Create private member functions to access those data members
C. Create protected member functions to access those data members
D. Private data members can never be accessed from outside the class

2021 By Eng. Joud Khattab 33


Question 9

■ If data members are private, what can we do to access them from the class object?
A. Create public member functions to access those data members
B. Create private member functions to access those data members
C. Create protected member functions to access those data members
D. Private data members can never be accessed from outside the class

2021 By Eng. Joud Khattab 34


Question 10

‫) دائما؟‬encapsulation( ‫■ أي من الخيارات التالية ينتهك مفهوم التغليف‬


A. Local variables
B. Global variables
C. Public variables
D. Array variables

2021 By Eng. Joud Khattab 35


Question 10

‫) دائما؟‬encapsulation( ‫■ أي من الخيارات التالية ينتهك مفهوم التغليف‬


A. Local variables
B. Global variables
C. Public variables
D. Array variables

2021 By Eng. Joud Khattab 36


Question 11

‫)؟‬constructor( ‫■ أي مما يلي ليس شرطا ضروريا للبناة‬


A. Its name must be same as that of class
B. It must not have any return type
C. It must contain a definition body
D. It can contains arguments

2021 By Eng. Joud Khattab 37


Question 11

‫)؟‬constructor( ‫■ أي مما يلي ليس شرطا ضروريا للبناة‬


A. Its name must be same as that of class
B. It must not have any return type
C. It must contain a definition body
D. It can contains arguments

2021 By Eng. Joud Khattab 38


Question 12

■ How many types of constructors are available for use in general (with respect to
parameters)?
A. 2
B. 3
C. 4
D. 5

2021 By Eng. Joud Khattab 39


Question 12

■ How many types of constructors are available for use in general (with respect to
parameters)?
A. 2
B. 3
C. 4
D. 5

2021 By Eng. Joud Khattab 40


Question 13

■ Default constructor must be defined, if parameterized constructor is defined and the


object is to be created without arguments.
A. True
B. False

2021 By Eng. Joud Khattab 41


Question 13

■ Default constructor must be defined, if parameterized constructor is defined and the


object is to be created without arguments.
A. True
B. False

2021 By Eng. Joud Khattab 42


Question 14

■ If class C inherits class B. And B has inherited class A. Then while creating the object
of class C, what will be the sequence of constructors getting called?
A. Constructor of C then B, finally of A
B. Constructor of A then C, finally of B
C. Constructor of C then A, finally B
D. Constructor of A then B, finally C

2021 By Eng. Joud Khattab 43


Question 14

■ If class C inherits class B. And B has inherited class A. Then while creating the object
of class C, what will be the sequence of constructors getting called?
A. Constructor of C then B, finally of A
B. Constructor of A then C, finally of B
C. Constructor of C then A, finally B
D. Constructor of A then B, finally C

2021 By Eng. Joud Khattab 44


COMPLEXITY
‫تعقيد الخوارزميات‬

2021 By Eng. Joud Khattab 45


‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫■ يرتبط بعاملين اساسيين‪:‬‬


‫– حجم الذاكرة‪:‬‬
‫الالزمة لتخزين البرنامج والمعطيات التي يعالجها‪.‬‬ ‫■‬
‫– زمن التنفيذ‪:‬‬
‫الزمن الالزم لتنتهي الخوارزمية من إنجاز تعليماتها كافة‪.‬‬ ‫■‬
‫يقاس زمن التنفيذ بحساب عدد التعليمات والزمن الالزم لتنفيذ كل تعليمة‪.‬‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪46‬‬


‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫■ يعتمد زمن تنفيذ البرنامج على حاسب على عوامل عديدة منها‪:‬‬
‫– معطيات المسألة الخاصة بتجربة ما‪.‬‬
‫– جودة الرماز الذي يولده المترجم من أجل بناء الملف التنفيذي‪.‬‬
‫– طبيعة وسرعة التعليمات المتوفرة في الحاسوب (في المعالج الصغري)‪.‬‬
‫– فعالية الخوارزمية‪( .‬تعتمد على قدرة المبرمج)‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪47‬‬


‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫لحساب زمن تنفيذ خوارزمية يجب تحديد بعد (او طول او حجم) معطيات المسألة من أجل كتابة درجة‬ ‫■‬
‫التعقيد بداللة هذا البعد‪.‬‬
‫أمثلة‪:‬‬ ‫■‬
‫– مسائل كثيرات الحدود‪:‬‬
‫يكون البعد هو درجة كثيرات الحدود‪.‬‬ ‫■‬
‫مسائل المصفوفات‪:‬‬ ‫–‬
‫يكون البعد بداللة أبعاد المصفوفة‪.‬‬ ‫■‬
‫مسائل البيانات‪:‬‬ ‫–‬
‫يكون البعد بداللة عدد العقد أو األسهم أو مجموعها‪.‬‬ ‫■‬
‫مسائل الفرز‪:‬‬ ‫–‬
‫يكون البعد بداللة عدد العناصر التي نريد ترتيبها‪.‬‬ ‫■‬
‫مسائل التحليل القواعدي‪:‬‬ ‫–‬
‫يكون البعد بداللة طول الكلمة‪.‬‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪48‬‬


‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫■ تذكرة ببعض القوانين الرياضية‪:‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪49‬‬


‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫■ تذكرة ببعض القوانين الرياضية‪:‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪50‬‬


Complexity (‫)تعقيد الخوارزميات‬

2021 By Eng. Joud Khattab 51


Complexity (‫)تعقيد الخوارزميات‬

T(n)
n n n log n n2 n3 n4 n10 2n
10 .01s .03s .1s 1s 10s 10s 1s
20 .02s .09s .4s 8s 160s 2.84h 1ms
30 .03s .15s .9s s 810s 6.83d 1s
40 .04s .21s 1.6s s 2.56ms 121d 18m
50 .05s .28s s s 6.25ms 3.1y 13d
100 .1s .66s 10s 1ms 100ms 3171y 41013y
103 1s 9.96s 1ms 1s 16.67m 3.171013y 3210283y
104 s 130s 100ms 16.67m 115.7d 3.171023y
105 s 1.66ms 10s 11.57d 3171y 3.171033y
106 ms 19.92ms 16.67m 31.71y 3.17107y 3.171043y

2021 By Eng. Joud Khattab 52


Complexity (‫)تعقيد الخوارزميات‬
Exercise

// a simple loop to calculate the sum of numbers in an array


int i , sum;
for (i = sum = 0; i<n ; i++)
sum += a[i];

2021 By Eng. Joud Khattab 53


Complexity (‫)تعقيد الخوارزميات‬
Rule

If a loop iterates for i = initial value to final value in step of 1


then:

Number of iterations of the loop = final value – initial value + 1

2021 By Eng. Joud Khattab 54


Complexity (‫)تعقيد الخوارزميات‬
Exercise
2
int i = 0; assignments
int sum = 0; The body of this loop is
executed for i = 0, 1, 2, … n-1.
while (i < n) Hence, it is executed
{ ((n-1) – 0) + 1 = n times
sum = sum + a[i];
i = i + 1;
} Therefore, these 2
assignments are
executed n times

Answer: Number of Assignment Operations =


2 + n + n = 2n + 2
2021 By Eng. Joud Khattab 55
Complexity (‫)تعقيد الخوارزميات‬
Question 1
‫ رتب التعقيدات التالية تصاعديا‬:‫■ السؤال‬
n –
n*n –
n*logn –
logn –
:‫■ اإلجابة‬
logn .1
n .2
n*logn .3
n*n .4

2021 By Eng. Joud Khattab 56


‫)تعقيد الخوارزميات( ‪Complexity‬‬
‫‪Question 2‬‬
‫■ السؤال‪ :‬أصغر تعقيد لخوارزمية الرفع لقوة‬
‫– ‪n‬‬
‫– ‪n*n‬‬
‫– ‪n*logn‬‬
‫– ‪logn‬‬
‫■ اإلجابة‪:‬‬
‫– ‪logn‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪57‬‬


Complexity (‫)تعقيد الخوارزميات‬
Question 2
int power (int x, int y) { int power2 (int x, int y) {
int p = 1; int res = 1;
for (int i = 1; i<=y; i++) { int factor = x;
p = p*x; } while (y>0) {
return p; } if (y%2==1)
res = res*factor;
factor = factor*factor;
y = y / 2; }
return res; }

2021 By Eng. Joud Khattab 58


‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫تصنيف المسائل حسب زمن التنفيذ‪:‬‬ ‫■‬


‫– )‪ :O(1‬خوارزميات البحث في جداول التقطيع‪)Hash Tables( .‬‬
‫– ‪ :Log n‬خوارزميات البحث الثنائي‪)Binary Search( .‬‬
‫– ‪ :n‬خوارزميات البحث التسلسلي‪)Search( .‬‬
‫– ‪ :n * Log n‬خوارزميات الفرز الجيدة‪)Sort( .‬‬
‫– ‪ :n*n‬ضرب المصفوفات (تعالج معطيات متوسطة الحجم)‪.‬‬
‫– 𝑛‪ :2‬أسي ال تستخدم إال في حالة معطيات ذات حجم محدود‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪59‬‬


‫)تعقيد الخوارزميات( ‪Complexity‬‬

‫■ يمكن ان نقول إن الخوارزميات الجيدة لحل مسألة معينة هي التي يكون زمن تنفيذها‪:‬‬
‫– ثابتا مهما كان حجم المعطيات‪ :‬مثل خوارزميات البحث في جداول التقطيع‪.‬‬
‫– لوغاريتميا‪ :‬مثل خوارزميات البحث الثنائي‪.‬‬
‫– خطيا‪ :‬مثل خوارزمية البحث التسلسلي‪.‬‬
‫– من مرتبة )‪ :n*log(n‬مثل خوارزميات الفرز الجيدة‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪60‬‬


Complexity (‫)تعقيد الخوارزميات‬
Question 3
void Main() :‫■ السؤال‬
{ ‫– احسب تعقيد الخوارزميات‬
:‫التالية‬
for (int i=1; i<=n; i++)
{
m=m+1; :‫■ اإلجابة‬
p=p*2;
l=l+3;
}
}

2021 By Eng. Joud Khattab 61


Complexity (‫)تعقيد الخوارزميات‬
Question 4
void Main() :‫■ السؤال‬
{ ‫– احسب تعقيد الخوارزميات‬
:‫التالية‬
for (int i=1; i<=n; i++) {
for (int j=1; j<=n; j++){
:‫■ اإلجابة‬
m=m+1;
p=p*2;
l=l+3; } }
}

2021 By Eng. Joud Khattab 62


Complexity (‫)تعقيد الخوارزميات‬
Question 5
void Main() :‫■ السؤال‬
{ ‫– احسب تعقيد الخوارزميات‬
:‫التالية‬
for (int i=1; i<=n; i++) {
for (int j=1; j<=i; j++){
:‫■ اإلجابة‬
m=m+1;
p=p*2;
l=l+3; } }
}

2021 By Eng. Joud Khattab 63


Complexity (‫)تعقيد الخوارزميات‬
Question 6
void Main() :‫■ السؤال‬
{ ‫– احسب تعقيد الخوارزميات‬
:‫التالية‬
for (int cnt=5,i=1; i<=n; i++)
for (int j=1; j<=n; j=j*2)
:‫■ اإلجابة‬
cnt++; n * log n –
}

2021 By Eng. Joud Khattab 64


RECURSIVE FUNCTIONS
‫الخوارزميات العودية‬

2021 By Eng. Joud Khattab 65


‫‪Recursive Functions‬‬
‫)الخوارزميات العودية(‬
‫■ هي خوارزمية تستدعي نفسها وينتهي االستدعاء بتحقق شرط للتوقف‪.‬‬
‫■ أمثلة‪:‬‬
‫– حساب اعداد فيبوناتشي‪.‬‬
‫– سلسلة أكريمان‪.‬‬
‫– أبراج هانوي‪.‬‬
‫– رسم المنحنيات‪.‬‬
‫– مسألة الوزراء الثمانية‪.‬‬
‫– منحنيات هلبرت‪.‬‬
‫■ يؤول حساب تعقيد الخوارزمية العودية إلى حل معادلة تراجعية‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪66‬‬


‫‪Recursive Functions‬‬
‫)الخوارزميات العودية(‬
‫■ يمكن التمييز بين نوعين من اإلجرائيات العودية‪:‬‬
‫– اإلجرائيات ذات العودية المباشرة‪:‬‬
‫نقول عن إجرائية ‪ P‬إنها عودية مباشرة إذا كانت تحوي استدعاء صريح لنفسها‪.‬‬ ‫■‬
‫– اإلجرائيات ذات العودية غير المباشرة‪:‬‬
‫نقول عن إجرائية ‪ P‬إنها عودية غير مباشرة إذا كانت تستدعي إجرائية أخرى ‪Q‬‬ ‫■‬
‫تستدعي ‪.P‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪67‬‬


‫‪Recursive Functions‬‬
‫)الخوارزميات العودية(‬
‫■ عملية االستدعاء العودي مكلفة من حيث الزمن التنفيذ وحجم الذاكرة المطلوبة‪.‬‬
‫■ يمكن تحويل خوارزمية عودية إلى تكرارية باستخدام مكدس‬
‫– حيث يتم تخزين معامالت الدخل والمتحوالت المحلية لكل استدعاء عودي فيه ثم‬
‫يتم حذف عناصر المكدس لتنفيذ بقية التعليمات بعد االستدعاء العودي على هذه‬
‫العناصر حيث يتم التنفيذ من االستدعاء األخير إلى األول‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪68‬‬


Recursive Functions (‫)الخوارزميات العودية‬
Question 1
f(33,33) ‫ ما النتيجة إذا كان االستدعاء‬:‫■ السؤال‬
int f(int x, int y) {
if (x==1) return y;
else if (y==1) return x;
else if (x%2==0) return y+f(x/2,y);
else return x+f(x,y/2); }
:‫■ اإلجابة‬
198

2021 By Eng. Joud Khattab 69


Recursive Functions (‫)الخوارزميات العودية‬
Question 1
f(22,22) ‫ ما النتيجة إذا كان االستدعاء‬:‫■ السؤال‬
int f(int x, int y) {
if (x==1) return y;
else if (y==1) return x;
else if (x%2==0) return y+f(x/2,y);
else return x+f(x,y/2); }
:‫■ اإلجابة‬
77

2021 By Eng. Joud Khattab 70


Recursive Functions (‫)الخوارزميات العودية‬
Question 2
f(1024*1024) ‫ ما النتيجة إذا كان االستدعاء‬:‫■ السؤال‬
Public int f(int n) {
int x=0;
for (int i=2; i<=n ; i*=2) {
x++; }
return x; }
:‫■ اإلجابة‬
f(210*210)=20

2021 By Eng. Joud Khattab 71


Recursive Functions (‫)الخوارزميات العودية‬
Question 3
f(33) ‫ ما النتيجة إذا كان االستدعاء‬:‫■ السؤال‬
Int f(int n) {
for (int i=1;i<=n;i++) {
i=i+1;
n=n-1; }
return n; }
:‫■ اإلجابة‬
22

2021 By Eng. Joud Khattab 72


Recursive Functions (‫)الخوارزميات العودية‬
Question 3
f(10) ‫ ما النتيجة إذا كان االستدعاء‬:‫■ السؤال‬
Int f(int n) {
for (int i=1;i<=n;i++) {
i=i+1;
n=n-1; }
return n; }
:‫■ اإلجابة‬
6

2021 By Eng. Joud Khattab 73


Recursive Functions (‫)الخوارزميات العودية‬
Question 4
int fun(int n) { 1. 4
if (n == 4) 2. 8
return n; 3. 16
else return 2*fun(n+1); } 4. Runtime error.

int main() {
printf("%d ", fun(2));
return 0; }

2021 By Eng. Joud Khattab 74


Recursive Functions (‫)الخوارزميات العودية‬
Question 4
int fun(int n) { 1. 4
if (n == 4) 2. 8
return n; 3. 16
else return 2*fun(n+1); } 4. Runtime error.

int main() {
printf("%d ", fun(2));
return 0; }

2021 By Eng. Joud Khattab 75


Recursive Functions (‫)الخوارزميات العودية‬
Question 4
int fun(int n) { 1. 4
if (n == 4) 2. 8
return n; 3. 16
else return 2*fun(n+1); } 4. Runtime error.

int main() {
printf("%d ", fun(6));
return 0; }

2021 By Eng. Joud Khattab 76


Recursive Functions (‫)الخوارزميات العودية‬
Question 4
int fun(int n) { 1. 4
if (n == 4) 2. 8
return n; 3. 16
else return 2*fun(n+1); } 4. Runtime error.

int main() {
printf("%d ", fun(6));
return 0; }

2021 By Eng. Joud Khattab 77


Recursive Functions (‫)الخوارزميات العودية‬
Question 5
int fun(int x, int y) { 1. 13
if (x == 0) 2. 12
return y; 3. 9
return fun(x - 1, x + y); } 4. 10

int main() {
printf("%d ", fun(4,3));
return 0; }

2021 By Eng. Joud Khattab 78


Recursive Functions (‫)الخوارزميات العودية‬
Question 5
int fun(int x, int y) { 1. 13
if (x == 0) 2. 12
return y; 3. 9
return fun(x - 1, x + y); } 4. 10

int main() { The rule: x(x+1)/2 + y


printf("%d ", fun(4,3));
return 0; }

2021 By Eng. Joud Khattab 79


Recursive Functions (‫)الخوارزميات العودية‬
Question 6
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
printf("%d", n%2);
fun(n/2); }

2021 By Eng. Joud Khattab 80


Recursive Functions (‫)الخوارزميات العودية‬
Question 6
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
printf("%d", n%2);
fun(n/2); }

2021 By Eng. Joud Khattab 81


Recursive Functions (‫)الخوارزميات العودية‬
Question 7
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
fun(n/2);
printf("%d", n%2); }

2021 By Eng. Joud Khattab 82


Recursive Functions (‫)الخوارزميات العودية‬
Question 7
n = 25 1. 11001
2. 10011
void fun(int n) { 3. 11111
if (n == 0) 4. 00000
return;
fun(n/2);
printf("%d", n%2); }

2021 By Eng. Joud Khattab 83


Recursive Functions (‫)الخوارزميات العودية‬
Question 8
What does the following function do? 1. x+y
int fun(int x, int y) 2. x+x*y
{ 3. x*y
if (y == 0) return 0; 4. x^y
return (x + fun(x, y-1));
}

2021 By Eng. Joud Khattab 84


Recursive Functions (‫)الخوارزميات العودية‬
Question 8
What does the following function do? 1. x+y
int fun(int x, int y) 2. x+x*y
{ 3. x*y
if (y == 0) return 0; 4. x^y
return (x + fun(x, y-1));
}

2021 By Eng. Joud Khattab 85


Recursive Functions (‫)الخوارزميات العودية‬
Question 9
int f(int n) { 1. Stack overflow
if(n <= 1) 2. 3
return 1; 3. 4
if(n%2 == 0) 4. 5
return f(n/2);
return f(n/2) + f(n/2+1); }

int main() {
printf("%d", f(11));
return 0; }

2021 By Eng. Joud Khattab 86


Recursive Functions (‫)الخوارزميات العودية‬
Question 9
int f(int n) { 1. Stack overflow
if(n <= 1) 2. 3
return 1; 3. 4
if(n%2 == 0) 4. 5
return f(n/2);
return f(n/2) + f(n/2+1); }

int main() {
printf("%d", f(11));
return 0; }

2021 By Eng. Joud Khattab 87


Recursive Functions (‫)الخوارزميات العودية‬
Question 10
Consider the following: f(5) 1. 0
int fun (int n) 2. 26
{ 3. 51
int x=1, k; 4. 71
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}

2021 By Eng. Joud Khattab 88


Recursive Functions (‫)الخوارزميات العودية‬
Question 10
Consider the following: f(5) 1. 0
int fun (int n) 2. 26
{ 3. 51
int x=1, k; 4. 71
if (n==1) return x;
for (k=1; k<n; ++k)
x = x + fun(k) * fun(n – k);
return x;
}

2021 By Eng. Joud Khattab 89


Recursive Functions (‫)الخوارزميات العودية‬
Question 11
If get(6) function is being called. how 1. 15
many times will the get() function be
2. 25
invoked?
3. 35
void get (int n) {
4. 45
if (n < 1) return;
get(n-1);
get(n-3);
printf("%d", n); }

2021 By Eng. Joud Khattab 90


Recursive Functions (‫)الخوارزميات العودية‬
Question 11
If get(6) function is being called. how 1. 15
many times will the get() function be
2. 25
invoked? get(6) [25 Calls]
3. 35 / \
void get (int n) { [17 Calls] get(5) get(3) [7 Calls]
4. 45 / \
if (n < 1) return; get(4) get(2) [5 Calls]
/ \
get(n-1); [7 Calls] get(3) get(1) [3 Calls]
/ \
get(n-3); get(2) get(0)
/ \
printf("%d", n); } [3 Calls] get(1) get(-1)
/ \
get(0) get(-2)

2021 By Eng. Joud Khattab 91


SEQUENTIAL
DATA STRUCTURE
‫بنى المعطيات التسلسلية‬

2021 By Eng. Joud Khattab 92


‫)السالسل الخطية( ‪Linked List‬‬

‫هي بنية معطيات تمكن من تخزين عناصر من نفس النمط‪.‬‬ ‫■‬


‫■ ميزات السالسل‪:‬‬
‫– ال ضرورة لتحديد طولها مسبقا‪.‬‬
‫– ال نحتاج إلى ازاحة عناصر السلسلة عند الحذف واالضافة‪.‬‬
‫■ سيئاتها‪:‬‬
‫– ال يمكن النفاذ المباشر إلى عنصر‪ ,‬وبالتالي تحتاج إلى ‪ n‬عملية مقارنة في أسوأ االحوال‬
‫للوصول إلى العنصر‪.‬‬
‫– نحتاج إلى حجم تخزين إضافي لتخزين المؤشرات‪.‬‬
‫■ يوجد أنواع أخرى للسالسل مثل‪:‬‬
‫– السالسل الدائرية ‪.circular linked list‬‬
‫– السالسل المضاعفة االرتباط ‪.doubly linked list‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪93‬‬


Linked List (‫)السالسل الخطية‬

A B C

Head

node

data pointer

2021 By Eng. Joud Khattab 94


‫)السالسل الخطية( ‪Linked List‬‬
‫‪V.S. Ordinary and Dynamic Arrays‬‬

‫‪Ordinary Arrays‬‬ ‫‪Dynamic Arrays‬‬


‫■ الميزات‪:‬‬ ‫الميزات‪:‬‬ ‫■‬
‫– من السهل جدا إنشاء واستخدام‪.‬‬ ‫– من السهل جدا إنشائها‪.‬‬
‫– الوصول المباشر إلى أي عنصر‪.‬‬ ‫– الوصول المباشر إلى أي عنصر‪.‬‬
‫– يمكن تحديد الحجم في وقت التشغيل‪.‬‬
‫■ السيئات‪:‬‬
‫ً‬ ‫السيئات‪:‬‬ ‫■‬
‫معروفا في‬ ‫– يجب أن يكون حجمها‬ ‫– يتطلب إدراج عنصر أو حذفه نقل كل‬
‫وقت التحويل البرمجي‪ ،‬وال يمكن‬ ‫العناصر األخرى‪.‬‬
‫تغييره في وقت التشغيل‪.‬‬
‫– إذا كان على المصفوفة أن تتخطى‬
‫– يتطلب إدراج عنصر أو حذفه نقل‬ ‫الحجم األولي ‪ ،‬فيجب إعادة تشكيل‬
‫كل العناصر األخرى‪.‬‬ ‫المصفوفة بأكملها إلى موقع جديد‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪95‬‬


‫)المكدس( ‪Stack‬‬

‫■ هي بنية خطية تشبه السلسلة‪ ،‬غير إن عمليات اإلضافة والحذف تتم من جهة واحدة ندعوها‬
‫قمة المكدس‪.‬‬
‫هي بنية معطيات تمكن من تخزين عناصر من نفس النمط‪.‬‬ ‫■‬
‫■ تتبع مبدأ ‪:‬‬
‫– )‪First In last Out (FILO‬‬
‫– )‪Last In First Out (LIFO‬‬
‫■ العمليات االساسية عليه‪:‬‬
‫– يمكن الوصول إلى العناصر من خالل قمته )‪)head‬‬
‫– إضافة عنصر إلى القمة )‪)push‬‬
‫– حذف عنصر من القمة )‪)pop‬‬
‫يمكن تمثيل المكدس باستخدام المصفوفات أو السالسل الخطية‪.‬‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪96‬‬


Stack (‫)المكدس‬

2021 By Eng. Joud Khattab 97


‫)المكدس( ‪Stack‬‬

‫■ تطبيقات المكدسات‪:‬‬
‫– سجل الصفحات المزارة في متصفح االنترنت‪.‬‬
‫– عمليات التراجع في محررات النصوص‪.‬‬
‫– ربط أزواج األقواس في التعابير الرياضية‪.‬‬
‫– التحويل من التعابير الرياضية النظامية إلى التعابير الملحقة وحساب قيمة هذه‬
‫التعابير‪.‬‬
‫– استدعاء تابع في جسم تابع‪.‬‬
‫– التجول في األشجار والبيانات بطريقة العمق أوال‪.‬‬
‫– مكدس األخطاء في المترجمات‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪98‬‬


Stack (‫)المكدس‬

Infix (‫)نظامي‬ Prefix (‫)مصدر‬ Postfix (‫)ملحق‬

A+B +AB AB+

A+B*C +A*BC ABC*+

A*(B+C) *A+BC ABC+*

A*B+C +*ABC AB*C+

A+B*C+D-E*F -++A*BCD*EF ABC*+D+EF*-

(A+B)*(C+D-E)*F **+AB-+CDEF AB+CD+E-*F*

2021 By Eng. Joud Khattab 99


Stack (‫)المكدس‬

■ Infix to Postfix:
1. Scan the infix expression from left to right.
2. If the scanned character is an operand, output it.
3. Else,
1. If the precedence of the scanned operator is greater or equal than the precedence of the operator
in the stack (or the stack is empty), push it.
2. Else, pop the operator from the stack until the precedence of the scanned operator is less-equal to
the precedence of the operator residing on the top of the stack. Then push the scanned operator to
the stack.
4. If the scanned character is an ‘(‘, push it to the stack.
5. If the scanned character is an ‘)’, pop and output from the stack until an ‘(‘ is encountered.
6. Repeat steps 2 to 6 until infix expression is scanned.
7. Pop and output from the stack until it is not empty.

2021 By Eng. Joud Khattab 100


Stack (‫)المكدس‬

■ Infix to Prefix:
1. Reverse the infix expression i.e A+B*C will become C*B+A.
■ Note while reversing each ‘(‘ will become ‘)’ and each ‘)’ becomes ‘(‘.
2. Obtain the postfix expression of the modified expression i.e CB*A+.
3. Reverse the postfix expression. Hence in our example prefix is +A*BC.

2021 By Eng. Joud Khattab 101


Stack (‫)المكدس‬

■ Postfix Calculation:
1. Create a stack to store operands (values).
2. Scan the given expression and do following for every scanned element:
1. If the element is a number, push it into the stack.
2. If the element is a operator, pop operands for the operator from stack. Evaluate
the operator and push the result back to the stack.
3. When the expression is ended, the number in the stack is the final answer.

2021 By Eng. Joud Khattab 102


Stack (‫)المكدس‬

■ Prefix Calculation:
1. Put a pointer P at the end of the end.
2. If character at P is a number push it to Stack.
3. If the character at P is an operator pop two elements from the Stack. Operate
on these elements according to the operator, and push the result back to the
Stack.
4. Decrement P by 1 and go to Step 2 as long as there are characters left to be
scanned in the expression.
5. The Result is stored at the top of the Stack.

2021 By Eng. Joud Khattab 103


Infix to Postfix Conversion
Example 1
■ 3+4*5/6

2021 By Eng. Joud Khattab 104


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack:
■ Output:

2021 By Eng. Joud Khattab 105


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack:
■ Output: 3

2021 By Eng. Joud Khattab 106


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack: +
■ Output: 3

2021 By Eng. Joud Khattab 107


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack: +
■ Output: 3 4

2021 By Eng. Joud Khattab 108


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack: + *
■ Output: 3 4

2021 By Eng. Joud Khattab 109


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack: + *
■ Output: 3 4 5

2021 By Eng. Joud Khattab 110


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack: +
■ Output: 3 4 5 *

2021 By Eng. Joud Khattab 111


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack: + /
■ Output: 3 4 5 *

2021 By Eng. Joud Khattab 112


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack: + /
■ Output: 3 4 5 * 6

2021 By Eng. Joud Khattab 113


Infix to Postfix Conversion
Example 1
■ 3+4*5/6
■ Stack:
■ Output: 3 4 5 * 6 / +

■ Done!

2021 By Eng. Joud Khattab 114


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))

2021 By Eng. Joud Khattab 115


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack:
■ Output:

2021 By Eng. Joud Khattab 116


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: (
■ Output:

2021 By Eng. Joud Khattab 117


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: (
■ Output: 4

2021 By Eng. Joud Khattab 118


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: ( +
■ Output: 4

2021 By Eng. Joud Khattab 119


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: ( +
■ Output: 4 8

2021 By Eng. Joud Khattab 120


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: (
■ Output: 4 8 +

2021 By Eng. Joud Khattab 121


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack:
■ Output: 4 8 +

2021 By Eng. Joud Khattab 122


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: *
■ Output: 4 8 +

2021 By Eng. Joud Khattab 123


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: * (
■ Output: 4 8 +

2021 By Eng. Joud Khattab 124


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: * (
■ Output: 4 8 + 6

2021 By Eng. Joud Khattab 125


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: * ( -
■ Output: 4 8 + 6

2021 By Eng. Joud Khattab 126


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: * ( -
■ Output: 4 8 + 6 5

2021 By Eng. Joud Khattab 127


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: *
■ Output: 4 8 + 6 5 -

2021 By Eng. Joud Khattab 128


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack:
■ Output: 4 8 + 6 5 - *

2021 By Eng. Joud Khattab 129


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: /
■ Output: 4 8 + 6 5 - *

2021 By Eng. Joud Khattab 130


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / (
■ Output: 4 8 + 6 5 - *

2021 By Eng. Joud Khattab 131


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( (
■ Output: 4 8 + 6 5 - *

2021 By Eng. Joud Khattab 132


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( (
■ Output: 4 8 + 6 5 - * 3

2021 By Eng. Joud Khattab 133


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( ( -
■ Output: 4 8 + 6 5 - * 3

2021 By Eng. Joud Khattab 134


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( ( -
■ Output: 4 8 + 6 5 - * 3 2

2021 By Eng. Joud Khattab 135


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / (
■ Output: 4 8 + 6 5 - * 3 2 -

2021 By Eng. Joud Khattab 136


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( *
■ Output: 4 8 + 6 5 - * 3 2 -

2021 By Eng. Joud Khattab 137


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( * (
■ Output: 4 8 + 6 5 - * 3 2 -

2021 By Eng. Joud Khattab 138


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( * (
■ Output: 4 8 + 6 5 - * 3 2 - 2

2021 By Eng. Joud Khattab 139


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( * ( +
■ Output: 4 8 + 6 5 - * 3 2 - 2

2021 By Eng. Joud Khattab 140


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( * ( +
■ Output: 4 8 + 6 5 - * 3 2 - 2 2

2021 By Eng. Joud Khattab 141


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: / ( *
■ Output: 4 8 + 6 5 - * 3 2 - 2 2 +

2021 By Eng. Joud Khattab 142


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack: /
■ Output: 4 8 + 6 5 - * 3 2 - 2 2 + *

2021 By Eng. Joud Khattab 143


Infix to Postfix Conversion
Example 2
■ (4+8)*(6-5)/((3-2)*(2+2))
■ Stack:
■ Output: 4 8 + 6 5 - * 3 2 - 2 2 + * /

■ Done!

2021 By Eng. Joud Khattab 144


‫)األرتال( ‪Queue‬‬

‫هي بنية خطية تشبه السلسلة غير أن عمليات اإلضافة تجري في جهة ندعوها ذيل الرتل‪.‬‬ ‫■‬
‫ويجري الحذف في الجهة المعاكسة التي نسميها بداية الرتل‪.‬‬
‫هي بنية معطيات تمكن من تخزين عناصر من نفس النمط‪.‬‬ ‫■‬
‫■ تتبع مبدأ ‪:‬‬
‫– )‪First In First Out (FIFO‬‬
‫– )‪Last In Last Out (LILO‬‬
‫■ العمليات االساسية عليه‪:‬‬
‫– يمكن الوصول إلى العناصر من خالل بدايته (‪ )head‬أو نهايته (‪.)tail‬‬
‫– إضافة عنصر إلى النهاية (‪.)enqueue‬‬
‫– حذف عنصر من البداية (‪.)dequeue‬‬
‫يمكن تمثيل االرتال باستخدام المصفوفات أو السالسل الخطية‪.‬‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪145‬‬


Queue (‫)األرتال‬

2021 By Eng. Joud Khattab 146


‫)األرتال( ‪Queue‬‬

‫■ تطبيقات األرتال‪:‬‬
‫– مخازن المدخالت والمخرجات في الكمبيوتر (لوحة المفاتيح والطابعة)‪.‬‬
‫– بنية معطيات مساعدة في الخوارزميات مثل خوارزميات التجول بالعرض أوال في‬
‫األشجار والبيانات‪.‬‬
‫– محاكاة أرتال الحياة اليومية‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪147‬‬


‫)األرتال( ‪Queue‬‬

‫االولويات‪:‬‬ ‫■ رتل‬
‫يضاف عامل األولوية إلى الرتل‪.‬‬ ‫–‬
‫يتم تخديم العنصر ذو األولوية األعلى اوال‪.‬‬ ‫–‬
‫مثال‪:‬‬ ‫–‬
‫أرتال االنتظار في عيادة الطبيب حيث تكون األولوية للحاالت اإلسعافية‪.‬‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪148‬‬


NON SEQUENTIAL
DATA STRUCTURES
‫بنى المعطيات غير التسلسلية‬

2021 By Eng. Joud Khattab 149


‫)االشجار( ‪Trees‬‬

‫■ نعرف الشجرة‪:‬‬
‫– بأنها مجموعة من العناصر نسميها عقد )‪،)nodes‬‬
‫– مرتبطة بين بعضها بروابط (‪،)Links‬‬
‫– ومنظمة تنظيما هرميا (‪،)Hierarchical‬‬
‫– ال يحتوي حلقات (‪ )Cycles‬مغلقة‪،‬‬
‫– أي إنه توجد عقدة مميزة ووحيدة نسميها جذر (‪.)Root‬‬
‫■ أمثلة‪:‬‬
‫– تنظيم الملفات في مجلدات‪.‬‬
‫– تمثيل عبارة حسابية‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪150‬‬


‫)األشجار الثنائية( ‪Binary Trees‬‬

‫■ حالة خاصة من األشجار يكون فيها لكل عقدة ابنان علي األكثر‪:‬‬
‫– نسمي العقد التي لها ولد على األقل عقد داخلية‪.‬‬
‫– نسمي العقد التي ليس لها أي ولد عقد خارجية أو أوراق (‪.)Leaf‬‬
‫– نسمي طريقا (‪ )Path‬كل متتالية من العقد‪.‬‬
‫– نسمي فرعا كل طريق يصل بين الجذر وإحدى األوراق‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪151‬‬


‫)األشجار الثنائية( ‪Binary Trees‬‬

‫■ تعاريف مهمة‪:‬‬
‫– درجة الشجرة‪ :‬العدد األعظمي ألوالد عقدة‪( .‬درجة شجرة ثنائية هي ‪)2‬‬
‫– حجم الشجرة‪ :‬عدد عقدها‪.‬‬
‫– ارتفاع عقدة‪ :‬عدد االتصاالت في الطريق الواصل من العقدة الى الجذر‪.‬‬
‫– ارتفاع الشجرة‪ :‬أطول ارتفاع عقدة فيها‪.‬‬
‫– عرض الشجرة‪ :‬العدد األعظم للعقد في مستوى ما‪.‬‬
‫– مسافة التجول‪ :‬مجموع ارتفاعات عقدها‪.‬‬
‫– مسافة التجول الخارجي‪ :‬مجموع ارتفاعات أوراقها‪.‬‬
‫– مسافة التجول الداخلي‪ :‬مجموع ارتفاعات عقدها الداخلية‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪152‬‬


‫)األشجار الثنائية( ‪Binary Trees‬‬
‫‪Exercise‬‬
‫جذر الشجرة‪n1 :‬‬ ‫■‬
‫االبن االيسر‪n2 :‬‬ ‫■‬
‫االبن األيمن‪n4 :‬‬ ‫■‬
‫األوراق‪n8 – n10 – n5 – n9 :‬‬ ‫■‬
‫العقد الداخلية‪n1 – n2 – n3 – n4 – n6 – n7 :‬‬ ‫■‬
‫ارتفاع عقدة‪:‬‬ ‫■‬
‫‪n1=0 , n2=n4=1 , n3=n5=n7=2‬‬
‫ارتفاع الشجرة‪4 :‬‬ ‫■‬
‫مسافة التجول‪22 :‬‬ ‫■‬
‫مسافة التجول الخارجي‪13 :‬‬ ‫■‬
‫مسافة التجول الداخلي‪9 :‬‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪153‬‬


‫)األشجار الثنائية( ‪Binary Trees‬‬

‫■ بعض األشجار الثنائية الخاصة‪:‬‬


‫– الشجرة الثنائية الخطية‪ :‬شجرة ثنائية يكون لكل عقدة منها ولد واحد على األكثر‪.‬‬
‫– الشجرة الثنائية التامة‪ :‬كل شجرة تحوي عقدة واحدة في المستوى ‪ 0‬وعقدتين في‬
‫المستوى ‪ 1‬وأربع عقد في المستوى ‪ 2‬و 𝑘‪ 2‬عقدة في المستوى ‪.k‬‬
‫(عدد العقد بشجرة تامة ارتفاعها ‪ h‬هو ‪)2ℎ+1 − 1‬‬ ‫■‬
‫– الشجرة الثنائية الكاملة‪ :‬كل شجرة ثنائية تكون كل مستوياتها مليئة بالعقد ماعدا‬
‫المستوى األخير يمكن أن يحوي على فراغات‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪154‬‬


‫)األشجار الثنائية( ‪Binary Trees‬‬
‫‪Question 1‬‬
‫■ السؤال‪ :‬ما هو ارتفاع شجرة خطية عدد عقدها ‪ n‬؟‬
‫■ اإلجابة‪n-1 :‬‬

‫■ السؤال‪ :‬كم عدد أوراق شجرة ثنائية تامة ؟‬


‫■ اإلجابة‪ 2h :‬حيث ‪ h‬هو ارتفاع الشجرة‬

‫■ السؤال‪ :‬كم عدد العقد لشجرة ثنائية تامة ؟‬


‫𝒉=𝒊‬ ‫■ اإلجابة‪:‬‬
‫𝟏 ‪෍ 𝟐𝒊 = 𝟐𝒉+𝟏 −‬‬
‫𝟎=𝒊‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪155‬‬


‫)األشجار الثنائية( ‪Binary Trees‬‬
‫‪Question 2‬‬
‫■ السؤال‪ :‬ماهي أسوأ حالة للبحث ضمن شجرة بحث ثنائية؟‬
‫■ اإلجابة‪ :‬عندما تكون الشجرة خطية حيث تكون مسافة التجول أعظمية‪.‬‬

‫■ السؤال‪ :‬ماهي أفضل حالة للبحث ضمن شجرة بحث ثنائية؟‬


‫■ اإلجابة‪ :‬عندما تكون الشجرة كاملة او متوازنة تكون مسافة التجول أصغرية‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪156‬‬


‫‪Binary Search Trees‬‬
‫)أشجار البحث الثنائية(‬
‫■ هي شجرة ثنائية تحقق الخواص التالية‪:‬‬
‫– من أجل عقدة محددة ‪ n‬كل القيم المخزنة في الشجرة الجزئية اليسارية هي أصغر‬
‫من القيمة المخزنة في العقدة ‪.n‬‬
‫– من أجل عقدة محددة ‪ n‬كل القيم المخزنة في الشجرة الجزئية اليمينية هي أكبر‬
‫من القيمة المخزنة في العقدة ‪.n‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪157‬‬


Binary Search Trees (‫)األشجار الثنائية‬
5

10 2 45 10

5 30 30 5 45

2 25 45 10 2 25 30

25

Binary search tree Binary search tree Not a binary search tree

2021 By Eng. Joud Khattab 158


Binary Search Trees (‫)أشجار البحث الثنائية‬
Inserting Method

2021 By Eng. Joud Khattab 159


‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪Representation Methods‬‬
‫■ طرق تمثيل األشجار الثنائية‪:‬‬
‫– عن طريق المؤشرات‪.‬‬
‫– عن طريق المصفوفات حيث يكون عدد أسطرها بعدد العقد وعدد أعمدتها ‪ 3‬حيث‬
‫يحوي العمود األول رقم العقدة والثاني رقم االبن االيمن وفي الثالث رقم االبن‬
‫االيسر‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪160‬‬


‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪Question 3‬‬
‫■ السؤال‪ :‬أي من الشجر التالية تعتبر "شجرة بحث ثنائية"؟‬
‫■ اإلجابة‪ b :‬و ‪c‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪161‬‬


‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪Question 4‬‬
‫■ السؤال‪ :‬ماهي سيئة طريقة المصفوفات؟؟‬
‫■ اإلجابة‪ :‬فيها هدر كبير للذاكرة في حالة األشجار غير التامة أو الكاملة‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪162‬‬


Binary Search Trees (‫)أشجار البحث الثنائية‬
AVL (Height-balanced Trees)
■ An AVL tree (or height-balanced tree) is a binary search tree such that:
– The height of the left and right subtrees of the root differ by at most 1
– The left and right subtrees of the root are AVL trees

2021 By Eng. Joud Khattab 163


‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪DFS‬‬
‫‪22‬‬ ‫■ طرق التجول في الشجرة‪:‬‬
‫‪start‬‬
‫– التجول بالعمق أوال (‪)depth first search‬‬

‫‪6‬‬ ‫‪33‬‬ ‫ترتيب زيارة العقد في التجول ‪:VLR‬‬


‫‪22,6,-1,8,7,16,33,45‬‬

‫‪-1‬‬ ‫‪8‬‬ ‫‪45‬‬

‫‪7‬‬ ‫‪16‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪164‬‬


‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪DFS‬‬
‫■ طرق التجول في الشجرة‪ :‬التجول بالعمق أوال (‪)depth first search‬‬
‫– ‪ VLR‬يزور العقدة ثم الشجرة الجزئية اليسارية ثم اليمينية‪ ،‬ويدعى الترتيب المصدر‬
‫(‪.)preorder‬‬
‫– ‪ VRL‬يزور العقدة ثم الشجرة الجزئية اليمينية ثم اليسارية‪.‬‬
‫– ‪ LVR‬يزور الشجرة الجزئية اليسارية ثم العقدة ثم اليمينية ويدعى الترتيب المتناظر‬
‫(‪.)inorder‬‬
‫– ‪RVL‬‬
‫– ‪ LRV‬ويدعى الترتيب الملحق (‪.)postorder‬‬
‫– ‪RLV‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪165‬‬


‫)أشجار البحث الثنائية( ‪Binary Search Trees‬‬
‫‪BFS‬‬
‫‪22‬‬
‫■ طرق التجول في الشجرة‪:‬‬
‫– التجول بالعرض أوال (‪.)breadth first search‬‬

‫‪6‬‬ ‫‪33‬‬
‫ترتيب زيارة العقد في التجول ‪:VLR‬‬
‫‪22,6,33,-1,8,45,7,16‬‬

‫‪-1‬‬ ‫‪8‬‬ ‫‪45‬‬

‫‪7‬‬ ‫‪16‬‬
‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪166‬‬
GRAPHS
‫البيانات‬

2021 By Eng. Joud Khattab 167


‫)البيانات( ‪Graphs‬‬

‫■ هي بنية معطيات تخزن العناصر كعقد ‪ vertex‬ويوجد بينها وصالت تمثل العالقات بين العقد‬
‫‪.edges‬‬
‫■ يمكن اعتبار البيانات تعميما للبنى الهرمية غير التسلسلية من الناحية النظرية‪ ،‬فاألشجار‬
‫هي حالة خاصة من البيانات (بيانات ال تحوي حلقات)‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪168‬‬


‫)البيانات( ‪Graphs‬‬

‫■ تتواجد البيانات في كل مكان‪:‬‬


‫– شبكات الطرق‪ ،‬الخطوط الجوية‪ ،‬شبكات الحواسيب‪.‬‬
‫■ من أهم منهجيات حل المسائل في الذكاء الصنعي‪:‬‬
‫– تحويل المسألة إلى مسألة بحث في بيان‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪169‬‬


‫)البيانات( ‪Graphs‬‬

‫■ درجة العقدة )‪ Deg(v‬هي عدد األسهم الصادرة من العقدة والواردة إليها‬


‫– حيث )‪ Deg-(v‬هي الدرجة الواردة‪.‬‬
‫– و )‪ Deg+(v‬هي الدرجة الصادرة‪.‬‬

‫■ يكون البيان متصل بشدة إذا كان في حالة كل عقدتين ‪ v,w‬يوجد طريق من ‪ v‬إلى ‪ w‬و‬
‫من ‪ w‬إلى ‪v‬‬

‫)‪𝑛(𝑛−1‬‬
‫وفي هذه‬ ‫■ عدد الروابط األعظمي في بيان غير موجه يحوي ‪ n‬عقدة يساوي‬
‫‪2‬‬
‫الحالة نقول عن البيان إنه تام (‪.)Complete‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪170‬‬


Graphs (‫)البيانات‬

.)adjacency matrix( ‫■ تمثيل البيان باستخدام مصفوفة التجاور‬

The Graph
The Adjacency Matrix

2021 By Eng. Joud Khattab 171


Graphs (‫)البيانات‬

.)adjacency list( ‫■ تمثيل البيان باستخدام قائمة التجاور‬

The Graph

The
2021 AdjacencyByList
Eng. Joud Khattab 172
‫)البيانات( ‪Graphs‬‬
‫‪Question 1‬‬
‫السؤال‪ :‬ماهي كلفة البحث عن عقد مجاورة لعقدة ممثلة بطريقة قائمة التجاور؟‬ ‫■‬
‫■ الجواب‪O(deg(v)) :‬‬

‫■ السؤال‪ :‬ماهي كلفة مسح الروابط الواردة لعقدة في بيان ممثل بطريقة قائمة التجاور؟‬
‫■ الجواب‪ O(V+E) :‬مكلف جدا‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪173‬‬


‫)البيانات( ‪Graphs‬‬

‫طرق التجول في البيان‪:‬‬ ‫■‬


‫– العمق أوال (‪.)DFS‬‬
‫– العرض أوال (‪ :)BFS‬هي طريقة إليجاد أقصر طريق في بيان غير موزون ألن الوصول‬
‫للعقد يتم بمسح أقل عدد للروابط‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪174‬‬


Graphs (‫)البيانات‬
DFS V.S BFS

2021 By Eng. Joud Khattab 175


‫)البيانات( ‪Graphs‬‬
‫‪Dijkstra‬‬
‫خوارزمية ‪ Dijkstra‬إليجاد أقصر طريق في بيان موزون‪.‬‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪177‬‬


Graphs (‫)البيانات‬ 2
Dijkstra 1 5
3
4 4 11
4

5 3 9 8
7
6 7
12 6 9
2
8

Processed 1 2 3 4 5 6 7 8 9
distance 0 ? ? ? ? ? ? ? ?
predecessor -1 -1 -1 -1 -1 -1 -1 -1 -1

2021 By Eng. Joud Khattab 178


‫)البيانات( ‪Graphs‬‬
‫‪Topological Sort‬‬
‫■ الفرز الطبولوجي يعتمد على ترتيب العقد حسب درجات ورودها حيث تكون كل األسهم او‬
‫الوصالت تؤشر إلى جهة واحدة هي اليمين‪.‬‬
‫■ ال يمكن تطبيقها على البيان الذي يحتوي على حلقات‪.‬‬
‫■ مثال‪:‬‬
‫– المنهاج السنوي لجامعة‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪179‬‬


Graphs (‫)البيانات‬
Topological Sort 6 1

5 2 8 3

7 4

next 0

Node 1 2 3 4 5 6 7 8
Indegree 2 2 3 2 1 1 0 2 7

LIST
2021 By Eng. Joud Khattab 180
‫)البيانات( ‪Graphs‬‬
‫‪Minimum Spanning Tree‬‬
‫■ شجرة االرتباط األصغرية‪ :‬هي شجرة تربط البيان بأقل كلفة ممكنة من الروابط‪.‬‬
‫■ تتواجد هذه الشجرة في حالة البيان المترابط فقط‪.‬‬
‫■ ‪Prim’s algorithm‬‬

‫‪9‬‬ ‫‪b‬‬ ‫‪9‬‬ ‫‪b‬‬


‫‪a‬‬ ‫‪2‬‬ ‫‪6‬‬ ‫‪a‬‬ ‫‪2‬‬ ‫‪6‬‬
‫‪d‬‬ ‫‪d‬‬
‫‪4‬‬ ‫‪5‬‬ ‫‪4‬‬ ‫‪5‬‬
‫‪5‬‬ ‫‪4‬‬ ‫‪5‬‬ ‫‪4‬‬

‫‪5‬‬ ‫‪e‬‬ ‫‪5‬‬ ‫‪e‬‬


‫‪c‬‬ ‫‪c‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪181‬‬


SORTING ALGORITHMS
‫خوارزميات الترتيب‬

2021 By Eng. Joud Khattab 182


‫‪Bubble Sort Algorithm‬‬
‫)خوارزمية الفرز الفقاعي(‬
‫■ تعقيد خوارزمية الفرز الفقاعي‪:‬‬
‫– أحسن األحوال‪ :‬عندما تكون المصفوفة مفروزة حيث اليوجد عمليات اسناد‪.‬‬
‫– أسوأ األحوال‪ :‬عندما تكون المصفوفة مرتية ترتيب عكسي يكون عدد عمليات‬
‫االسناد من مرتبة )‪.O(n*n‬‬
‫– الحالة الوسطية‪ :‬عندما تكون المصفوفة مرتبة عشوائياً )‪.O(n*n‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪183‬‬


‫‪Selection Sort Algorithm‬‬
‫)خوارزمية الفرز باالختيار(‬
‫■ تعقيد الخوارزمية‪:‬‬
‫– عدد عمليات المقارنة )‪O(n*n‬‬
‫– عدد عمليات التبديل )‪O(n‬‬
‫■ جيدة لحجم صغير وفي حالة كانت عملية التبديل مكلفة وعملية المقارنة غير مكلفة‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪184‬‬


‫‪Insertion Sort Algorithm‬‬
‫)خوارزمية الفرز باإلضافة(‬
‫■ تعقيد خوارزمية الفرز باإلضافة‪:‬‬
‫– أحسن األحوال‪ :‬عندما تكون المصفوفة مفروزة حيث ال يوجد عمليات اسناد )‪O(1‬‬
‫وعمليات المقارنة )‪.O(n‬‬
‫– أسوأ األحوال‪ :‬عندما تكون المصفوفة مرتية ترتيب عكسي يكون عدد عمليات‬
‫االسناد من مرتبة )‪.O(n*n‬‬
‫– الحالة الوسطية‪ :‬عندما تكون المصفوفة مرتبة عشوائياً )‪.O(n*n‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪185‬‬


‫‪Merge Sort Algorithm‬‬
‫)خوارزمية الفرز بالدمج(‬
‫■ تعتمد مبدأ فرق تسد‪.‬‬
‫■ هي خوارزمية عوديه تقوم بما يلي‪:‬‬
‫– تقسم المصفوفة إلى جزئين‪،‬‬
‫– تفرز كل جزء على حدا‪،‬‬
‫– تدمج الجزئيين المفروزين‬
‫في مصفوفة واحدة مفروزة‪.‬‬

‫■ تعقيد الخوارزمية ‪:‬‬


‫– ))‪ O(nlog(n‬دوما‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪186‬‬


‫‪Quick Sort Algorithm‬‬
‫)خوارزمية الفرز السريع(‬
‫■ تعقيد الخوارزمية‪:‬‬
‫– ))‪ O(nlog(n‬في أحسن األحوال والحالة الوسطية‪.‬‬
‫– )‪ O(n*n‬في أسوأ األحوال‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪187‬‬


Sorting Algorithms

2021 By Eng. Joud Khattab 188


SEARCH ALGORITHMS
‫خوارزميات البحث في مصفوفة‬

2021 By Eng. Joud Khattab 189


Search Algorithms
Binary V.S. Sequential
O(n) ‫ تعقيده‬: ‫■ البحث التسلسلي‬
O(log(n)) ‫ تعقيده‬: ‫■ البحث الثنائي‬

2021 By Eng. Joud Khattab 190


ADVANCE ALGORITHMS
‫خوارزميات متقدمة‬

2021 By Eng. Joud Khattab 191


‫‪Backtracking Algorithms‬‬
‫)الخوارزميات التراجعية(‬
‫■ هي خوارزميات عودية تعتمد طريقة التجريب والخطأ في حل المشاكل‪.‬‬
‫■ تتلخص هذه الطريقة ببناء الحل النهائي للمسألة عن طريق مجموعة من الخطوات‪:‬‬
‫– في الخطوة نحدد اإلمكانيات المتاحة للخطوة التالية‪،‬‬
‫– ثم ندرس هذه اإلمكانيات بأن ننتقي أحدها‪،‬‬
‫– و نسجلها على أنها الخطوة التالية في الحل النهائي‪،‬‬
‫– و نتابع الخوارزمية اعتماداً على هذه الخطوة‪.‬‬
‫– عندما يظهر لنا أن اختيارنا ال يقود إلى الحل النهائي‪ ،‬أو يؤدي إلى طريق مسدود‪،‬‬
‫– نعدل عن هذه الخطوة‪.‬‬
‫■ تشبه هذه العملية بناء سجل أخطاء يفيد في تجنب الوقوع في الخطأ مرتين‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪192‬‬


‫‪Backtracking Algorithms‬‬
‫)الخوارزميات التراجعية(‬
‫■ مثال ‪:‬‬
‫– مسألة جولة حصان الشطرنج‪:‬‬
‫إيجاد طريقة لتغطية رقعة الشطرنج من موقع معين يقف فيه الحصان وذلك باستخدام‬ ‫■‬
‫حركات الحصان المعروفة في الشطرنج وبشرط المرور بكل مربع مرة واحدة‪.‬‬

‫– مسألة الوزراء الثمانية‪:‬‬


‫توزيع ثمانية وزراء على رقعة الشطرنج بحيث ال يكون فيها اي واحد مهدد لألخر‪.‬‬ ‫■‬

‫– مسألة الخيار األمثل‪:‬‬


‫مقارنة الحلول التي يمكن ايجادها لمسألة معينة واالحتفاظ بالحل األمثل وفق معايير‬ ‫■‬
‫محددة للمسألة‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪193‬‬


‫‪Greedy Algorithms‬‬
‫)الخوارزميات الجشعة(‬
‫■ هي خوارزمية التي تستند على الحدس المهني الذي يتم عن طريقه اختيار اإلمكانية‬
‫األفضل المرئية في المرحلة الحالية ( الحل األمثل في المرحلة الحالية)‪ ,‬من دون األخذ‬
‫بالحسبان تأثير هذه الخطوة على تكملة الحل‪.‬‬
‫■ قد ال يقودنا الحل األمثل المرحلي إلى الحل األمثل للمسألة‪.‬‬
‫■ خوارزمية الجشع ال يكن ان تتراجع عن خياراتها‪ .‬هذا هو الفرق الرئيسي بينها‬
‫وبين البرمجة الديناميكية‪.‬‬
‫■ مثل خوارزمية برايم وأشجار هوفمان‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪194‬‬


‫‪Divide and Conquer‬‬
‫)خوارزميات فرق تسد(‬
‫■ عمل خوارزمية فرق تسد عن طريق تقسيم المسألة بشكل عودي إلى مسألتين‬
‫جزئيتين أو أكثر من نفس النوع‪ ،‬حتى تصبح المسائل الجزئية بسيطة بما فيه الكفاية‬
‫لتحل بشكل مباشر‪ .‬ومن ثم تدمج حلول المسائل الجزئية لتعطي حال ً للمسألة الجزئية‪.‬‬
‫■ امثلة‪:‬‬
‫– خوارزميات البحث الثنائي‪.‬‬
‫– خوارزميات الفرز بالدمج‪.‬‬
‫– خوارزميات الفرز السريع‪.‬‬
‫– خوارزمية ابراج هانوي‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪195‬‬


‫‪Dynamic Programming‬‬
‫)البرمجة الديناميكية(‬
‫■ لحل مسألة ما‪ ،‬نحن بحاجة إلى حل أجزاء مختلفة من المسألة (مسائل فرعية)‪ ،‬ومن‬
‫ثم جمع حلول المسائل الفرعية للحصول على حل شامل‪.‬‬
‫■ نهج البرمجة الديناميكية‪:‬‬
‫– البحث عن حل كل مسألة فرعية مرة واحدة فقط‪،‬‬
‫– وبالتالي تقليل عدد الحسابات‪ :‬حالما تم حساب حل مسألة فرعية ما‪ ،‬يتم حفظه‪،‬‬
‫وفي المرة القادمة عند الحاجة للحل نفسه‪ ،‬يتم ببساطة استرجاعه‪.‬‬
‫■ خوارزميات البرمجة الديناميكية ستدرس الحلول السابقة للمسائل الثانوية وتقوم بدمجها‬
‫للحصول علي افضل حل للمسائلة المراد حله‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪196‬‬


HASHING FUNCTION
(‫)جداول التقطيع‬

2021 By Eng. Joud Khattab 197


‫‪Hashing Tables‬‬
‫)جداول التقطيع(‬
‫هي طريقة لإليجاد عنصر مباشرة دون الحاجة إلى خوارزمية بحث من خالل النفاذ المباشر إلى موقعه‪.‬‬ ‫■‬
‫هي بنية فعالة في برمجة التطبيقات التالية‪:‬‬ ‫■‬
‫– التي تحتاج إلى عمليات معجم‪.‬‬
‫– االمثلة التي ال تهتم بالترتيب‪.‬‬
‫يخزن العنصر ذو المفتاح ‪ k‬في الخانة )‪ h(k‬حيث ‪ h‬هو تابع التقطيع الذي يستخدم لحساب دليل الخانة‬ ‫■‬
‫من المفتاح ‪.k‬‬
‫قد يحدث تصادم في حال وجود مفتاحين لهما نفس قيمة تابع التقطيع‪.‬‬ ‫■‬
‫تخزن القيم على شكل ازواج من (‪)key, value‬‬ ‫■‬
‫جداول التقطيع ليست بنية جيدة للتجول حول سلسلة من القيم‪)Iteration( .‬‬ ‫■‬

‫الزمن المتوقع إلضافة والبحث عن عنصر في جدول تقطيع ضمن فرضيات مناسبة )‪( .O(1‬الحالة الوسطية)‬ ‫■‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪198‬‬


Hashing Tables V.S.
Other Data Structures
■ We want to implement the dictionary operations Insert(), Delete() and Search()/Find() efficiently.
■ Arrays:
– can accomplish in O(n) time
– but are not space efficient (assumes we leave empty space for keys not currently in
dictionary)
■ Binary search trees
– can accomplish in O(log n) time
– are space efficient.
■ Hash Tables:
– A generalization of an array that under some reasonable assumptions is O(1) for
Insert/Delete/Search of a key

2021 By Eng. Joud Khattab 199


Hashing Tables
(‫)جداول التقطيع‬
:‫■ مثال عن تخزين المعلومات باستخدام العنونة المباشرة‬
‫■ تستخدم عندما نستطيع منح موقع من الجدول لكل مفتاح (عدد العناصر المتوقع تخزينها‬
.‫قريب من العدد الكلي للعناصر‬

For technical or 0000000


commercial reasons, 0000001
certain numbers may not
6131467 Record for phone
be assigned to any
6131468 number 6131496
subscriber
6131469
6131470 Anas - Mazzah
Each phone number
uniquely determines an
array element 9999997
9999998
2021 By Eng. Joud Khattab 9999999 200
Hashing Tables
(‫)جداول التقطيع‬
■ For example, if we hash keys 0…1000 into a hash table with 5 entries and use
h(key) = key mod 5 , we get the following sequence of events:

Insert 2 Insert 21 Insert 34 Insert 54


key data key data key data

0 0 0 There is a
1 1 21 … 1 21 … collision at
2 2 … 2 2 … 2 2 … array entry
#4
3 3 3
4 4 4 34 … ???
2021 By Eng. Joud Khattab 201
‫‪Hashing Tables‬‬
‫)جداول التقطيع(‬
‫■ تقنية الربط لحل التصادم‪:‬‬
‫– توضع جميع العناصر التي لها نفس قيمة تابع التقطيع في سلسلة خطية‪.‬‬
‫)‪(Hashing with Chaining‬‬
‫– تصبح خانة الجدول مؤشر إلى بداية سلسلة العناصر المتصادمة‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪202‬‬


Hashing Tables (‫)جداول التقطيع‬
Hashing with Chaining
Insert 54 other Insert 101
key key data
0 0
1 21 1 21 101
2 2 2 2
3 3
4 54 34 4 54 34

CHAIN

2021 By Eng. Joud Khattab 203


Hashing Tables (‫)جداول التقطيع‬
Hashing with Chaining
■ What is the running time to insert/search/delete?
– Insert: It takes O(1) time to compute the hash function and insert at head of
linked list
– Search: It is proportional to max linked list length
– Delete: Same as search

2021 By Eng. Joud Khattab 204


‫)جداول التقطيع( ‪Hashing Tables‬‬
‫‪Question 1‬‬
‫■ السؤال‪ :‬ماهي كلفة عملية إضافة عنصر؟‬
‫■ الجواب‪ O(1) :‬حيث تتم اإلضافة للعناصر المتصادمة في رأس السلسلة‪.‬‬

‫■ السؤال‪ :‬ماهي كلفة عملية البحث والحذف في أسوأ األحوال؟‬


‫■ الجواب‪ :‬تتعلق بطول السلسلة الخطية للعناصر المتصادمة وهي في أسوأ االحوال )‪O(n‬عند‬
‫اختيار تابع تقطيع سيء‪.‬‬

‫■ السؤال‪ :‬ماهي الكلفة الوسطية للحذف واالضافة؟‬


‫■ الجواب‪ :‬هو عدد العناصر الوسطي في السلسلة عند استخدام تابع تقطيع يحقق توزع منتظم‬
‫وهو ‪ n/m‬حيث ‪ n‬عدد العناصر الكلي و ‪ m‬عدد الخانات في الجدول‪.‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪205‬‬


‫)توابع التقطيع( ‪Hashing Function‬‬

‫■ طريقة التقسيم‪:‬‬
‫– بربط المفتاح ‪ k‬بالخانة التي تساوي باقي قسمة ‪ k‬على ‪.m‬‬
‫– ‪h(k)=k%m‬‬
‫– يجب تجنب قيم ‪ m‬من قوى ‪ 2‬واختيار أعداد أولية بعيدة عنها‪.‬‬
‫■ طريقة الضرب‪:‬‬
‫– نضرب المفتاح ‪ k‬بثابت ‪ A‬حيث ‪ 0<A<1‬ونأخذ الجزء الكسري ثم نضرب هذه القيمة‬
‫ب ‪ m‬ونأخذ الجزء الطبيعي من العدد الناتج‪.‬‬
‫– يمكن اختيار أي قيمة للعدد ‪.m‬‬

‫‪2021‬‬ ‫‪By Eng. Joud Khattab‬‬ ‫‪206‬‬


MCQ

2021 By Eng. Joud Khattab 210


Question 1

■ For a binary search algorithm to work, it is necessary that the array (list) must be?
A. sorted
B. unsorted
C. in a heap
D. popped out of stack

2021 By Eng. Joud Khattab 211


Question 1

■ For a binary search algorithm to work, it is necessary that the array (list) must be?
A. sorted
B. unsorted
C. in a heap
D. popped out of stack

2021 By Eng. Joud Khattab 212


Question 2

■ Which one of the below mentioned is linear data structure?


A. Queue
B. Stack
C. Arrays
D. All of the above

2021 By Eng. Joud Khattab 213


Question 2

■ Which one of the below mentioned is linear data structure?


A. Queue
B. Stack
C. Arrays
D. All of the above

2021 By Eng. Joud Khattab 214


Question 3

■ Binary search tree has best case run-time complexity of Ο(log n). What could the
worst case?
A. Ο(n)
B. Ο(n2)
C. Ο(n3)
D. None of the above

2021 By Eng. Joud Khattab 215


Question 3

■ Binary search tree has best case run-time complexity of Ο(log n). What could the
worst case?
A. Ο(n)
B. Ο(n2)
C. Ο(n3)
D. None of the above

2021 By Eng. Joud Khattab 216


Question 4

■ An algorithm is?
A. a piece of code to be executed.
B. a loosely written code to make final code.
C. a step by step procedure to solve problem.
D. all of the above.

2021 By Eng. Joud Khattab 217


Question 4

■ An algorithm is?
A. a piece of code to be executed.
B. a loosely written code to make final code.
C. a step by step procedure to solve problem.
D. all of the above.

2021 By Eng. Joud Khattab 218


Question 5

■ Quick sort running time depends on the selection of?


A. size of array
B. pivot element
C. sequence of values
D. none of the above!

2021 By Eng. Joud Khattab 219


Question 5

■ Quick sort running time depends on the selection of?


A. size of array
B. pivot element
C. sequence of values
D. none of the above!

2021 By Eng. Joud Khattab 220


Question 6

■ A queue data-structure can be used for?


A. expression parsing
B. recursion
C. resource allocation
D. all of the above

2021 By Eng. Joud Khattab 221


Question 6

■ A queue data-structure can be used for?


A. expression parsing
B. recursion
C. resource allocation
D. all of the above

2021 By Eng. Joud Khattab 222


Question 7

■ Program with highest run-time complexity is?


A. Tower of Hanoi
B. Fibonacci Series
C. Prime Number Series
D. None of the above

2021 By Eng. Joud Khattab 223


Question 7

■ Program with highest run-time complexity is?


A. Tower of Hanoi
B. Fibonacci Series
C. Prime Number Series
D. None of the above

2021 By Eng. Joud Khattab 224


Question 8

■ From a complete graph, by removing maximum _______________ edges, we can


construct a spanning tree?
A. e-n+1
B. n-e+1
C. n+e-1
D. e-n-1

2021 By Eng. Joud Khattab 225


Question 8

■ From a complete graph, by removing maximum _______________ edges, we can


construct a spanning tree?
A. e-n+1
B. n-e+1
C. n+e-1
D. e-n-1

2021 By Eng. Joud Khattab 226


Question 9

■ The worst case complexity of binary search matches with?


A. interpolation search
B. linear search
C. merge sort
D. none of the above

2021 By Eng. Joud Khattab 227


Question 9

■ The worst case complexity of binary search matches with?


A. interpolation search
B. linear search
C. merge sort
D. none of the above

2021 By Eng. Joud Khattab 228


Question 10

■ What is a hash table?


A. A structure that maps values to keys.
B. A structure that maps keys to values.
C. A structure used for storage.
D. A structure used to implement stack and queue.

2021 By Eng. Joud Khattab 229


Question 10

■ What is a hash table?


A. A structure that maps values to keys.
B. A structure that maps keys to values.
C. A structure used for storage.
D. A structure used to implement stack and queue.

2021 By Eng. Joud Khattab 230


Question 11

■ The recurrence relation capturing the optimal time of the Tower of Hanoi problem
with n discs is
A. T(n) = 2T(n – 2) + 2
B. T(n) = 2T(n – 1) + n
C. T(n) = 2T(n/2) + 1
D. T(n) = 2T(n – 1) + 1

2021 By Eng. Joud Khattab 231


Question 11

■ The recurrence relation capturing the optimal time of the Tower of Hanoi problem
with n discs is
A. T(n) = 2T(n – 2) + 2
B. T(n) = 2T(n – 1) + n
C. T(n) = 2T(n/2) + 1
D. T(n) = 2T(n – 1) + 1

2021 By Eng. Joud Khattab 232


Question 12

■ What does the following function do for a given Linked List with first node as head?
void fun1(struct node* head) {
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data); }
A. Prints all nodes of linked lists
B. Prints all nodes of linked list in reverse order
C. Prints alternate nodes of Linked List
D. Prints alternate nodes in reverse order

2021 By Eng. Joud Khattab 233


Question 12

■ What does the following function do for a given Linked List with first node as head?
void fun1(struct node* head) {
if(head == NULL)
return;
fun1(head->next);
printf("%d ", head->data); }
A. Prints all nodes of linked lists
B. Prints all nodes of linked list in reverse order
C. Prints alternate nodes of Linked List
D. Prints alternate nodes in reverse order

2021 By Eng. Joud Khattab 234


Question 13

■ Which of the following points is/are true about Linked List data structure when it is
compared with array
A. Arrays have better cache locality that can make them better in terms of
performance
B. It is easy to insert and delete elements in Linked List
C. Random access is not allowed in a typical implementation of Linked Lists
D. All of the above

2021 By Eng. Joud Khattab 235


Question 13

■ Which of the following points is/are true about Linked List data structure when it is
compared with array
A. Arrays have better cache locality that can make them better in terms of
performance
B. It is easy to insert and delete elements in Linked List
C. Random access is not allowed in a typical implementation of Linked Lists
D. All of the above

2021 By Eng. Joud Khattab 236


Question 14

■ Which of the following sorting algorithms can be used to sort a random linked list
with minimum time complexity?
A. Insertion Sort
B. Quick Sort
C. Heap Sort
D. Merge Sort

2021 By Eng. Joud Khattab 237


Question 14

■ Which of the following sorting algorithms can be used to sort a random linked list
with minimum time complexity?
A. Insertion Sort
B. Quick Sort
C. Heap Sort
D. Merge Sort

2021 By Eng. Joud Khattab 238


Question 15

■ In the worst case, the number of comparisons needed to search a singly linked list
of length n for a given element is
A. log 2 n
B. n/2
C. log 2 n – 1
D. n

2021 By Eng. Joud Khattab 239


Question 15

■ In the worst case, the number of comparisons needed to search a singly linked list
of length n for a given element is
A. log 2 n
B. n/2
C. log 2 n – 1
D. n

2021 By Eng. Joud Khattab 240


Question 16

■ Which one of the following is an application of Stack Data Structure?


A. Managing function calls
B. The stock span problem
C. Arithmetic expression evaluation
D. All of the above

2021 By Eng. Joud Khattab 241


Question 16

■ Which one of the following is an application of Stack Data Structure?


A. Managing function calls
B. The stock span problem
C. Arithmetic expression evaluation
D. All of the above

2021 By Eng. Joud Khattab 242


Question 17

■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is


A. 284
B. 213
C. 142
D. 71

2021 By Eng. Joud Khattab 243


Question 17

■ The result evaluating the postfix expression 10 5 + 60 6 / * 8 – is


A. 284
B. 213
C. 142
D. 71

2021 By Eng. Joud Khattab 244


Question 18

■ How many stacks are needed to implement a queue. Consider the situation where
no other data structure like arrays, linked list is available to you.
A. 1
B. 2
C. 3
D. 4

2021 By Eng. Joud Khattab 245


Question 18

■ How many stacks are needed to implement a queue. Consider the situation where
no other data structure like arrays, linked list is available to you.
A. 1
B. 2
C. 3
D. 4

2021 By Eng. Joud Khattab 246


Question 19

■ The most appropriate matching for the following pairs


X: depth first search 1: heap
Y: breadth-first search 2: queue
Z: sorting 3: stack
A. X—1, Y—2 ,Z –3
B. X—3, Y—1, Z –2
C. X—3, Y—2, Z—1
D. X—2 ,Y—3 , Z--1

2021 By Eng. Joud Khattab 247


Question 19

■ The most appropriate matching for the following pairs


X: depth first search 1: heap
Y: breadth-first search 2: queue
Z: sorting 3: stack
A. X—1, Y—2 ,Z –3
B. X—3, Y—1, Z –2
C. X—3, Y—2, Z—1
D. X—2 ,Y—3 , Z--1

2021 By Eng. Joud Khattab 248


Question 20

■ Aliasing in the context of programming languages refers to


A. multiple variables having the same memory location
B. multiple variables having the same value
C. multiple variables having the same identifier
D. multiple uses of the same variable

2021 By Eng. Joud Khattab 249


Question 20

■ Aliasing in the context of programming languages refers to


A. multiple variables having the same memory location
B. multiple variables having the same value
C. multiple variables having the same identifier
D. multiple uses of the same variable

2021 By Eng. Joud Khattab 250


Question 21

■ Randomized quicksort is an extension of quicksort where the pivot is chosen


randomly. What is the worst case complexity of sorting n numbers using randomized
quicksort?
A. O(n)
B. O(n Log n)
C. O(n^2)
D. O(n!)

2021 By Eng. Joud Khattab 251


Question 21

■ Randomized quicksort is an extension of quicksort where the pivot is chosen


randomly. What is the worst case complexity of sorting n numbers using randomized
quicksort?
A. O(n)
B. O(n Log n)
C. O(n^2)
D. O(n!)

2021 By Eng. Joud Khattab 252


Question 22

■ The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
A. n/2
B. (n - 1)/3
C. (n - 1)/2
D. (2n + 1)/3

2021 By Eng. Joud Khattab 253


Question 22

■ The number of leaf nodes in a rooted tree of n nodes, with each node having 0 or 3
children is:
A. n/2
B. (n - 1)/3
C. (n - 1)/2
D. (2n + 1)/3

2021 By Eng. Joud Khattab 254


Question 23

■ Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an


initially empty binary search tree. The binary search tree uses the usual ordering on
natural numbers. What is the in-order traversal sequence of the resultant tree ?
A. 7 5 1 0 3 2 4 6 8 9
B. 0 2 4 3 1 6 5 9 8 7
C. 0 1 2 3 4 5 6 7 8 9
D. 9 8 6 4 2 3 0 1 5 7

2021 By Eng. Joud Khattab 255


Question 23

■ Suppose the numbers 7, 5, 1, 8, 3, 6, 0, 9, 4, 2 are inserted in that order into an


initially empty binary search tree. The binary search tree uses the usual ordering on
natural numbers. What is the in-order traversal sequence of the resultant tree ?
A. 7 5 1 0 3 2 4 6 8 9
B. 0 2 4 3 1 6 5 9 8 7
C. 0 1 2 3 4 5 6 7 8 9
D. 9 8 6 4 2 3 0 1 5 7

2021 By Eng. Joud Khattab 256


Question 24

■ Consider the following graph. Among the following sequences, Which are depth first
traversals of the graph?
I) abeghf
II) abfehg
III) abfhge
IV) afghbe
A. I, II and IV only
B. I and IV only
C. II, III and IV only
D. I, III and IV only

2021 By Eng. Joud Khattab 257


Question 24

■ Consider the following graph. Among the following sequences, Which are depth first
traversals of the graph?
I) abeghf
II) abfehg
III) abfhge
IV) afghbe
A. I, II and IV only
B. I and IV only
C. II, III and IV only
D. I, III and IV only

2021 By Eng. Joud Khattab 258


Question 25

■ What is the weight of a minimum spanning tree of the following graph ?


A. 29
B. 31
C. 38
D. 41

2021 By Eng. Joud Khattab 259


Question 25

■ What is the weight of a minimum spanning tree of the following graph ?


A. 29
B. 31
C. 38
D. 41

2021 By Eng. Joud Khattab 260


Question 26

■ The best data structure to check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List

2021 By Eng. Joud Khattab 261


Question 26

■ The best data structure to check whether an arithmetic expression has balanced
parentheses is a
A. Queue
B. Stack
C. Tree
D. List

2021 By Eng. Joud Khattab 262


Question 27

■ The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order
into a Max Heap. The resultant Max Heap is.
A. A
B. B
C. C
D. D

2021 By Eng. Joud Khattab 263


Question 27

■ The elements 32, 15, 20, 30, 12, 25, 16 are inserted one by one in the given order
into a Max Heap. The resultant Max Heap is.
A. A
B. B
C. C
D. D

2021 By Eng. Joud Khattab 264


Question 28

■ The time complexity of the following C function is (assume n > 0)


int recursive (int n) {
if (n == 1)
return (1);
else
return (recursive (n - 1) + recursive (n - 1)); }
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(2^n)

2021 By Eng. Joud Khattab 265


Question 28

■ The time complexity of the following C function is (assume n > 0)


int recursive (int n) {
if (n == 1)
return (1);
else
return (recursive (n - 1) + recursive (n - 1)); }
A. O(n)
B. O(n log n)
C. O(n^2)
D. O(2^n)

2021 By Eng. Joud Khattab 266


Question 29

■ A program takes as input a balanced binary search tree with n leaf nodes and
computes the value of a function g(x) for each node x. If the cost of computing g(x) is
min{no. of leaf-nodes in left-subtree of x, no. of leaf-nodes in right-subtree of x} then
the worst-case time complexity of the program is
A. Θ(n)
B. Θ(n Log n)
C. Θ(n^2)
D. Θ(n^2 log n)

2021 By Eng. Joud Khattab 267


Question 29

■ A program takes as input a balanced binary search tree with n leaf nodes and
computes the value of a function g(x) for each node x. If the cost of computing g(x) is
min{no. of leaf-nodes in left-subtree of x, no. of leaf-nodes in right-subtree of x} then
the worst-case time complexity of the program is
A. Θ(n)
B. Θ(n Log n)
C. Θ(n^2)
D. Θ(n^2 log n)

2021 By Eng. Joud Khattab 268


CONTACT INFO

2021 By Eng. Joud Khattab 269

You might also like