Binary Tree
Time limit: 500ms
Memory limit: 256mb
Description:
Please complete the missing code of the binary tree ADT, such that the program could output a correct answer.
1. isEmpty
2. create
3. makeBT
4. Lchild
5. Rchild
6. Data
7. isBalanced
-------------------------Copy the following code, complete it and submit-------------------------
/*
I, <Your Full Name>, am submitting the assignment for
an individual project.
I declare that the assignment here submitted is original except for
source material explicitly acknowledged, the piece of work, or a part
of the piece of work has not been submitted for more than one purpose
(i.e. to satisfy the requirements in two different courses) without
declaration. I also acknowledge that I am aware of University policy
and regulations on honesty in academic work, and of the disciplinary
guidelines and procedures applicable to breaches of such policy and
regulations, as contained in the University website
http://www.cuhk.edu.hk/policy/academichonesty/.
It is also understood that assignments without a properly signed
declaration by the student concerned will not be graded by the
teacher(s).
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>
#define max(a, b) (((a) > (b)) ? (a) : (b))
typedef struct node BinTree;
struct node {
int element;
struct node* parent;
struct node* leftchild;
struct node* rightchild;
};
//create an empty binary tree.
BinTree* Create() {
// write down your code here
}
//If the binary tree is empty, return TRUE, otherwise, return FALSE.
bool isEmpty(BinTree* bintree) {
// write down your code here
}
//return a binary tree with root as rootNode. Its left subtree is bintree1, and right subtree is bintree2
BinTree* MakeBT(BinTree* bintree1, BinTree* rootNode, BinTree* bintree2) {
// write down your code here
}
//If the bintree is empty, return ERROR, otherwise, return the left subtree of bintree.
BinTree* Lchild(BinTree* bintree) {
// write down your code here
}
//If the bintree is empty, return ERROR, otherwise, return the right subtree of bintree.
BinTree* Rchild(BinTree* bintree) {
// write down your code here
}
//If the bintree is empty, return ERROR, otherwise, return the element data stored in the root node of bintree.
int Data(BinTree* bintree) {
// write down your code here
}
int isBalanced(BinTree* bintree, bool * flag)
{
// write down your code here
}
// DO NOT MODIFY THE CODE BELOW
int main() {
int n;
scanf("%d", &n);
BinTree** BinTreeNodeArray = (BinTree**)malloc( (n+1) * sizeof(BinTree*));
// Allocate memory for each struct in the array and initialize the struct members
const int BUFFER_SIZE = 100;
for(int i = 1; i <= n; i++){
char buffer[BUFFER_SIZE];
scanf("%s", buffer);
if(strcmp(buffer, "None") != 0){
int num = atoi(buffer);
BinTreeNodeArray[i] = (BinTree*)malloc(sizeof(BinTree));
BinTreeNodeArray[i]->element = num;
BinTreeNodeArray[i]->leftchild = NULL;
BinTreeNodeArray[i]->rightchild = NULL;
}
else{
BinTreeNodeArray[i] = NULL;
}
}
char end_str[BUFFER_SIZE];
//discard the end
scanf("%s", end_str);
for(int i = n / 2; i >= 1; i--){
MakeBT(BinTreeNodeArray[2 * i], BinTreeNodeArray[i], BinTreeNodeArray[2 * i + 1]);
}
bool BalancedFlag = true;
isBalanced(BinTreeNodeArray[1], &BalancedFlag);
if(BalancedFlag){
printf("YES\n");
}
else{
printf("NO\n");
}
return 0;
}
-------------------------------------------End of Code------------------------------------------
Input:
The first line contains a non-negative integer n;
Then the next n lines are binary tree elements from id "1" to "n" in the array representation, if an empty node, input None.
Note that you need to implement a pointer version of binary tree and check if it's height-balanced.
The last line is an ending indicator, “end”.
Output:
If the input binary tree is height-balanced, please output ”YES”. Otherwise, please output “NO”.
Sample Input 1:
7
3
2
1
5
None
None
4
end
Sample Output 1:
YES
Submit