1321 - CS I2P (II) 2017-2 Lee Quiz3 (Mid 1補考) Scoreboard

Time

2017/11/03 13:20:00 2017/11/03 15:10:00

Clarification

# Problem Asker Description Reply Replier Reply Time For all team

# Problem Pass Rate (passed user / total user)
10990 cheat sheet
11372 Create A Binary Search Tree
11373 Polynomial addition using linked list

10990 - cheat sheet   

Description

Linked list

typedef struct _Node {
    int data;
    struct _Node *next;
} Node;

 

/* print the content of the linked list.*/

void printList(Node *head)
{
    Node *temp;
    for(temp=head; temp!=NULL; temp=temp->next) {
        printf("%d ", temp->data);
    }
}

/*Insert a node after the node pointed by nd.

Return 1 if insertion succeeds.
Otherwise, return 0.*/

int insertNode(Node* nd, int data)
{
    if (nd != NULL) {

        Node* temp = (Node*)malloc(sizeof(Node));
        temp->data = data;
        temp->next = nd->next;
        nd->next = temp;
        return 1;
    }
    return 0;
}

 

/*Delete a node after the node pointed by nd.

Return 1 if insertion succeeds. 

Otherwise, return 0.*/

int deleteNode(Node* nd)
{
    if (nd != NULL) {
        Node* temp = nd->next;
        if (temp != NULL) {
            nd -> next = temp->next;
            free(temp);
            return 1;
        }
    }
    return 0;
}

 

Binary tree

typedef struct _node {
     int data;
     struct _node *left, *right;
} BTNode;

 

Tree traversal

  • Pre-order: visit root, left subtree, and right subtree
  • In-order: visit left subtree, root, and right subtree
  • Post-order: visit left subtree, right subtree, and root

 

Expression order

  • Prefix:  operator, left operand,and right operand
  • Infix:  left operand, operator, and right operand
  • Postfix: left operand, right operand, and operator

 

Syntax tree

The internal nodes are operators, and the leaves are operands.

Recursive evaluation of prefix expression

/*It is a pseudo code */

int evalBoolExpr(){

char c = getchar();

If c is an operator

   op1 = evalBoolExpr();

   op2 = evalBoolExpr();

   return op1 c op2;

Else if c is a variable

   return the value of c;

}

Grammar of infix expression with parenthesis

  • EXPR = FACTOR| EXPR OP FACTOR
  • FACTROR = ID | (EXPR)

 

/*Suppose expr[] is an array of expression and pos is the current position, which initially is strlen(expr)-1. */

/*-------------------------------------*/

BTNode* makeNode(char c)
{
    int i;
    BTNode *node = (BTNode*)malloc(sizeof(BTNode));
    set node->data
    node->left = NULL;
    node->right = NULL;
    return node;
}

 

/*-------------------------------------*/

BTNode* FACTOR()
{
    char c;
    BTNode *node = NULL;
    if (pos>=0) {
        c = expr[pos--];
        if (c is an ID) {
            node = makeNode(c);
        } else if (c==‘)’) {
            node = EXPR();
            if(expr[pos--]!= '(') {
                printf("Error!\n");
                freeTree(node);
            }
        }
    }
    return node;
}

 

/*-------------------------------------*/

BTNode* EXPR()
{

    char c;

    BTNode *node = NULL, *right=NULL;

    if (pos>=0) {

        node = right = FACTOR();

        if (pos>0) {

            c = expr[pos];

            if (c is an operator) {

                node = makeNode(c);

                node->right = right;

                pos--;

                node->left = EXPR();

            }

        }

    }

    return node;

}

Input

Output

Sample Input  Download

Sample Output  Download

Tags




Discuss




11372 - Create A Binary Search Tree   

Description

Please create a binary search tree, and support insert operation. Please ignore the repeat number and print out the tree with preorder sequence. (The root node is greater than the node in the left subtree and smaller than the node in the right subtree.)

Pre-order:

  1. Check if the current node is empty / null.
  2. Display the data part of the root (or current node).
  3. Traverse the left subtree by recursively calling the pre-order function.
  4. Traverse the right subtree by recursively calling the pre-order function

Input

The input contains two lines. The first line is a integer n (n<=8000) , and the second line is a sequence S, where S contain n non-negative integer number. (The first number in the sequence S is the root.) 

Output

Please print out the tree with preorder. (You don't need to write the print_tree(). You only need to create a binary search tree.)

Sample Input  Download

Sample Output  Download

Partial Judge Code

11372.c

Partial Judge Header

11372.h

Tags




Discuss




11373 - Polynomial addition using linked list   

Description

You are required to use linked list to do the addition of two polynomials.

Input

The input contains two lines. Each lines presents a polynomial. The format of each line is looked like : "5 4 -3 2 1 0" which means polynomial "5x4-3x2+1x0".

Each polynomial must contain a constant term. (You can use this rule to determine whether the polynomial is end.)

For example, "-2 3 1 1 0 0" should be -2x3+1x1.

(The input polynomial should be arrangement in descending power.)

Output

Output the answer. Print a space character (' ') in the begining.

For example, if the input is

5 4 -3 2 -1 1 1 0 (means 5x4-3x2-1x1+1)

-2 3 1 1 0 0 (means -2x3+1x1)

the output should be 

" 5 4 -2 3 -3 2 1 0" (which means 5x4-2x3-3x2+1).

If the value of coefficient is 0, you don't have to print it.

(The output polynomial should be arrangement in descending power.)

Sample Input  Download

Sample Output  Download

Partial Judge Code

11373.c

Partial Judge Header

11373.h

Tags




Discuss