Midterm 2
Midterm 2
#include <stdio.h>
#include <stdlib.h>
LabelType label;
} Node;
Please write a function numberOfNodes(n) to compute the number of nodes of the tree with root n.
Prototype
Name: numberOfNodes
Parameter(s):
Body: computes and returns the number of nodes of the tree with root n.
For example:
Test Result
n0->parent = NULL_NODE;
n1->parent = n0;
n2->parent = n1;
n3->parent = n2;
n4->parent = n3;
n5->parent = n4;
DCBAEF
Such a student list can be represented by a binary search tree in which each node contains two fields
one for the name and one for the mark.
The name is the key used to compare and organize nodes in the tree.
Nodes are inserted based on the lexicographical order (thứ tự ABC) of the names. Specifically:
o If the new student's name is lexicographically smaller than the current node’s name, the
new node is inserted into the left subtree.
o If the new student's name is larger, it is inserted into the right subtree.
This structure ensures that an inorder traversal of the binary search tree will yield the students in
alphabetical order based on their names.
Given a Data Structure StudentBST (a BST tree) to store students and some basic functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
} LabelType;
} Node;
typedef Node *StudentBST; //a StudentBST is a pointer to the root of the tree
Please write a function insert(n, m, pT) to insert a student with name 'n' and mark 'm' into pT.
Prototype
Name: insert
Parameter(s):
o If a student with the name n already exists in the tree, print an error message: "The
name has existed.\n".
o After the insertion, the inorder traversal of pT should result in a list of students sorted in
increasing order by name.
Hint
For example:
Test Result
Luna Lovegood: 8,
==END==
"Fluffy", Dobby: 8,
int n = 5; Fluffy: 5
printStudents(&studentBST); Dobby: 8,
} Fluffy: 5,
"Fluffy", ==END==
"Nagini", Fluffy: 5,
Cedric Diggory: 9,
Cho Chang: 9,
Dobby: 8,
Draco Malfoy: 7,
Fluffy: 5,
Lily Potter: 8,
Nagini: 6,
Newt Scamander: 6
==END==
The name is the key used to compare and organize nodes in the tree.
Nodes are inserted based on the lexicographical order (thứ tự ABC) of the names. Specifically:
o If the new student's name is lexicographically smaller than the current node’s name, the
new node is inserted into the left subtree.
o If the new student's name is larger, it is inserted into the right subtree.
This structure ensures that an inorder traversal of the binary search tree will yield the students in
alphabetical order based on their names.
Given a Data Structure StudentBST (a BST tree) to store students and some basic functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
} LabelType;
LabelType label;
struct NodeTag *left, *right;
} Node;
typedef Node *StudentBST; //a StudentBST is a pointer to the root of the tree
Please write a function retrieveMark(n, T) to retrieve the mark of the student with the name n from the
binary search tree T.
Prototype
Name: retrieveMark
Parameter(s):
Body:
o return the mark of the student with the name n if found in the tree T.
Hint
For example:
Test Result
"Lord Voldemort",
"Rubeus Hagrid",
"Dudley Dursley",
"Dolores Umbridge",
Test Result
"Peeves",
"Hermione Granger",
"Nymphadora Tonks"};
Such a student list can be represented by a binary search tree in which each node contains two fields
one for the name and one for the mark.
The name is the key used to compare and organize nodes in the tree.
Nodes are inserted based on the lexicographical order (thứ tự ABC) of the names. Specifically:
o If the new student's name is lexicographically smaller than the current node’s name, the
new node is inserted into the left subtree.
o If the new student's name is larger, it is inserted into the right subtree.
This structure ensures that an inorder traversal of the binary search tree will yield the students in
alphabetical order based on their names.
Given a Data Structure StudentBST (a BST tree) to store students and some basic functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
} LabelType;
LabelType label;
} Node;
typedef Node *StudentBST; //a StudentBST is a pointer to the root of the tree
Please write a function countByMark(v, T) to count the number of students having a mark greater than
or equal to v in the tree T.
Prototype
Name: countByMark
Parameter(s):
Body:
o return the count of students who have a mark greater than or equal to v in the tree T.
Hint
Traverse the binary search tree T, comparing the marks of each student with the value v.
For example:
Test Result
The name is the key used to compare and organize nodes in the tree.
Nodes are inserted based on the lexicographical order (thứ tự ABC) of the names. Specifically:
o If the new student's name is lexicographically smaller than the current node’s name, the
new node is inserted into the left subtree.
o If the new student's name is larger, it is inserted into the right subtree.
This structure ensures that an inorder traversal of the binary search tree will yield the students in
alphabetical order based on their names.
Given a Data Structure StudentBST (a BST tree) to store students and some basic functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
} LabelType;
LabelType label;
struct NodeTag *left, *right;
} Node;
typedef Node *StudentBST; //a StudentBST is a pointer to the root of the tree
Please write a function filterByMark(v, T) to create a new tree containing students who have a mark
greater than v in the tree T.
Prototype
Name: filterByMark
Parameter(s):
Body:
o return a new StudentBST that contains only the students from the original tree T who
have a mark greater than v.
Hint
Traverse the binary search tree T (using a modified version of preorder/inorder/postorder), and
for each student whose mark is greater than v, insert them into the new tree (using insert
function).
For example:
Test Result
==END==
printf("**Original tree:**\n");
==END==
printf("\n**Original tree:**\n");
Draco Malfoy: 7,
Ron Weasley: 9
==END==
==END==
Test Result
int n = 5; ==END==
printStudents(&studentBST);
printStudents(&filteredTree);
6. Given a Data Structure Tree for binary trees (using pointers) to store characters as labels.
#include <stdio.h>
#include <stdlib.h>
LabelType label;
} Node;
Please write a function copyTree(T1, T2) to make a copy of T2 and store the result in T1.
Prototype
Name: copyTree
Parameter(s):
Hint
For example:
Test Result
n0->parent = NULL_NODE;
n1->parent = n0;
n2->parent = n1;
n3->parent = n2;
n4->parent = n3;
n5->parent = n4;
Tree T2 = n0;
Tree T1;
copyTree(&T1, T2);
n0->label = 'A';
n1->label = 'B';
n2->label = 'C';
n3->label = 'D';
n4->label = 'E';
n5->label = 'F';
BCEFDA
7. In mathematics, there are four basic arithmetic operators: addition ('+'), subtraction('-'), multiplication
('*'), and division (':').
The associative property does not apply to subtraction. This means the order in which we group
numbers matters. For example, (a−b)−c≠a−(b−c)(a−b)−c≠a−(b−c). Instead, (a−b)−c=a−(b+c)(a−b)−c=a−
(b+c), for example:
(12−3)−2=12−(3+2)(12−3)−2=12−(3+2)
LabelType label;
} Node;
Prototype
Name: transformWithPseudoAssociativity
Parameter(s):
o r: Node pointer - root of the tree
Body: transform a tree with root r, representing (a−b)−c(a−b)−c, into one representing a−
(b+c)a−(b+c). Note that a, b, c may be subtrees, not just leaves.
Hint
Relink nodes
Tree rotation is an operation on a binary tree that changes the structure without interfering with the
order of the elements. A tree rotation moves one node up in the tree and one node down. It is used to
change the shape of the tree, and in particular to decrease its height by moving smaller subtrees down
and larger subtrees up, resulting in improved performance of many tree operations..
The tree rotation renders the inorder traversal of the binary tree invariant. This implies the order of the
elements is not affected when a rotation is performed in any part of the tree. This property is very useful
for binary search trees.
LabelType label;
} Node;
Please write a function rotate(r) to perform a right rotation on a tree with root r.
Prototype
Name: rotate
Parameter(s):
Body: right rotate the tree with root r, and return the new root node.
Hint
Relink nodes.
Such a student list can be represented by a binary search tree in which each node contains two fields
one for the name and one for the mark.
The name is the key used to compare and organize nodes in the tree.
Nodes are inserted based on the lexicographical order (thứ tự ABC) of the names. Specifically:
o If the new student's name is lexicographically smaller than the current node’s name, the
new node is inserted into the left subtree.
o If the new student's name is larger, it is inserted into the right subtree.
This structure ensures that an inorder traversal of the binary search tree will yield the students in
alphabetical order based on their names.
Given a Data Structure StudentBST (a BST tree) to store students and some basic functions.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
} LabelType;
typedef struct NodeTag {
LabelType label;
} Node;
typedef Node *StudentBST; //a StudentBST is a pointer to the root of the tree
Please write a function printStudents(T) that prints the names and marks of all students stored in a tree
T, sorted by their names in alphabetical order. Each student's name and mark should be printed on a new
line in the following format:
<student_name>: <mark>,
<student_name>: <mark>,
...
<student_name>: <mark>
==END==
After all the students' names and marks have been printed, output ==END== on a new line, for example:
Hermione Granger: 8,
Luna Lovegood: 9
==END==
Prototype
Name: printStudents
Parameter(s):
Hint
You can use a global variable "first" to determine when the first element is printed.
For example:
Test Result
Draco Malfoy: 7,
Luna Lovegood: 8,
Ron Weasley: 9
==END==
} Rubeus Hagrid: 6,
Sirius Black: 6
==END==
Lord Voldemort: 9,
Moaning Myrtle: 7,
Pansy Parkinson: 9,
Rubeus Hagrid: 6,
Sirius Black: 6
==END==