cs225_final_20-12-2012

Upload: chu-manh-tuan

Post on 02-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 CS225_Final_20-12-2012

    1/6

    !"#$ &'(

    Ho Chi Minh City University of Technology

    Department of Electrical and Electronics Engineering

    FINAL EXAMINATION (K2010)

    Grading: 50%

    CS225 Data Structures and Programming Principles

    (Equivalent course: Computer System Engineering)

    Course ID: 407406

    Date: 20 Dec, 2012 Duration: 120 minutes

    Student name: Student ID:

    Students are allowed to use one A4 page with two sides for reference.

    Books and other documents are not allowed to use.

    This examination consists of 6 pages

    Problem 1: Answer the following questions for the binary tree :1. The height of the tree is2. The depth of the node B is3. Descendants of the node B are4. External nodes of the tree are5. Pre-order traversal of the tree is6. Post-order traversal of the tree is

    Problem 2: Give the following code for Node class#include using namespace std;class Node {

    private:char element;

    Node* parent; Node* left; Node* right;

    public: Node() {

    element = 0; parent = NULL;left = NULL; right = NULL; }

    Node(char value) {element = value; parent = NULL;left = NULL; right = NULL; }

    void insert_left(Node* L) {left = L; }

    void insert_right(Node* R) {left = R; }

    void node_print() {cout

  • 8/10/2019 CS225_Final_20-12-2012

    2/6

    !"#$ )'(

    int main(){

    Node* A = new Node('A'); Node* B = new Node('B');

    2. Write a function that print out preorder traversal of the binary treevoid preorder_traversal(BinaryTree BT){

    Problem 3: Balance the binary search tree below15

    10

    2

    5

    7

    8

  • 8/10/2019 CS225_Final_20-12-2012

    3/6

    !"#$ *'(

    Describe step by step the balance process; note the steps that use single rotation or double rotation Step 1 Step 2 Step 3

    Step 4 Step 5 Step 6

    Problem 4: Insert into an initially empty binary search tree, items with the following key (in this order):50, 45, 18, 55, 20, 60, 46, and 58. Draw the tree after each insertion.Insert 50 Insert 45 Insert 18 Insert 55

    50

    Insert 20 Insert 60 Insert 46 Insert 58

  • 8/10/2019 CS225_Final_20-12-2012

    4/6

    !"#$ +'(

    Problem 5: Give the C++ code for Quick-sort using List STLvoid quick_sort(list *L){ list *L1 = new list ;

    list *L2 = new list ;int pivot_num;int N = L->size();if (N>1){ pivot_num = pivot(L);

    partition(L, L1, L2, pivot_num);quick_sort(L1);

    quick_sort(L2);merge(L,L1,L2,pivot_num);

    }}

    int pivot(list *L){

    list ::iterator L_Iter;int N = L->size();int t = rand() % N;L_Iter = L->begin();for (int i=0; i< t; i++) L_Iter++;return *L_Iter;

    }

    Write C++ code for the functions partition and merge

    void partition(list *L, list *L1, list

    *L2, int pivot_num){

    merge(list *L, list *L1, list *L2, int

    pivot_num){

  • 8/10/2019 CS225_Final_20-12-2012

    5/6

    !"#$ ,'(

    Problem 6: Fill in the following table for the operation of Priority Queue ADT

    No. Operation Output Priority Queue1 insertItem(10, C) - {(10,C)}2 insertItem(8, A)

    3 insertItem(9, B)4 minElement()5 minKey()6 insertItem(7, D)7 size()8 removeMin()9 removeMin()10 removeMin()

    Problem 7: Describe the sorting process using Radix-sort algorithm for the following sequence of 4-bit

    unsigned number.

    1100

    0101

    1101

    1000

    0110

    1110

    0111

    Problem 8: Answer the following questions about Graph ADT

    1. If the number of vertices is 10 , the maximum number of edges in the graph is

    2. If the number of edges is 12 , the sum of degree of all vertices in the graph is3. If the number of vertices is n , and the number of edges is m , the running time of DFS is

    4. If the number of vertices is n , and the number of edges is m , the running time of BFS is

    5. If the number of vertices is n , and the number of edges is m , the running time of Dijkstrasalgorithm is

  • 8/10/2019 CS225_Final_20-12-2012

    6/6

    !"#$ ('(

    Problem 9: Give the graph G below. Assume that the graph is implemented by a linked list with alphabetorder and numeric order. Find traversal of G using Depth first search (DFS) and Breadth first search (BFS)starting at S0.

    Graph G Draw DFS traversal Draw BFS traversal S1

    S3

    S4 S5 S6

    S0

    S2

    Problem 10: Apply Dijkstras algorithm to find Minimum Spanning Tree for the following graph.Describe step by step the shortest path finding process.Step 1:

    A

    B

    C

    D F

    E

    G

    H23

    24

    25 13

    1513

    10

    29

    57

    23

    0

    27

    Step 2: Step 3:

    Step 4: Step 5: Step 6:

    Step 7: Step 8:

    --------------------------------------------------- The end ------------------------------------------------------ Electronics Department Lecturer

    Dr. Hoang Trang Dr. Truong Quang Vinh