# 50110. Tree Operations

## I'm a slow walker, but I never walk backwards.

Write a program to implement tree operations, including flipping, identical testing, and computing the average leaf depth.

Three operations for binary tree are as the follows:

• Flip: For a binary tree where the left subtree is L and the right subtree is R. To flip this tree means to swap L and R, i.e., now L will be the new right subtree, and the R will be the new left subtree, and then we recursively flip both L and R.

Before flipping (original tree):

After flipping (new tree):

For more example, if there is a binary tree T with node A as the root:

After flipping tree T, a new tree T' is generated:

Note that you need to construct a new binary tree by flipping the original tree. That is, you do not need to flip the tree in-place.

• Identical: Given two binary trees, check if they are identical. This can be recursively defined as follows. Two trees are identical if the data in the root are the same, and both the left and right subtrees are also identical. Note that the number of nodes of the two trees may be different in the input.

• Average depth computation: Given a binary tree, compute its average depth. The depth of a node is the length of the path to the root. In other words, the depth of a root node is 0, the depths of its children are 1, and the depths of its grandchildren are 2. The average depth of a tree is the total sum of the depths for all leaves (those nodes without any children) divided by the number of leaves. For example, consider the following tree.

The depths of node B and C are 1 since there is only one edges for them to the root. The depths of node D, E, F and G are 2 since they need to go through two edges to the root. Similarly, the depths of node H and I are 3 since they need to pass three edges to the root.

There are totally four leaves in the tree, which are the nodes D, H, F and I. Thus, the average depth is (the sum of leaves’ depth) / (the number of the leaves) = (2+3+2+3)/4 = 10/4.

Please implement each operation by three functions in operations.c:

1. For flipping operation, please construct a binary tree and do the operation in the function, Node *FlipTree(...), then return the pointer of the root of the new tree.
2. For identical operation, please compare tree1 and tree2 in the function, int isIdentical(...), and return an integer either 0 or 1. If tree1 and tree2 are identical, return 1; otherwise, return 0.
3. For average depth computation, please compute the average depth of the tree in the function, void depthAvg(...), and print the result with the format (the sum of leaves’ depth) / (the number of the leaves).

The prototype of three functions are as followed.

operations.h

#ifndef OPERATION#define OPERATIONtypedef struct Node{    int n;    struct Node *left, *right;} Node; Node *FlipTree(Node *root);int isIdentical(Node *tree1, Node *tree2);void depthAvg(Node *root); #endif


You may use the following main function to test your function.

Main Function 1

12345678910111213141516171819202122232425262728293031323334353637#include <stdio.h>#include "operations.h" void printTree(Node *root) {  if (root == NULL) {    printf("NULL\n");    return;  }  printf("%d\n", root->n);  if (root->left == NULL && root->right == NULL) return;  printTree(root->left);  printTree(root->right);} int main() {  Node node;   for(int i = 0; i < 9; i++){    node[i].n = i;    node[i].left = node[i].right = NULL;  }   node.left = &node;  node.right = &node;  node.left = &node;  node.right = &node;  node.left = &node;  node.right = &node;  node.left = &node;  node.right = &node;   Node *tree = FlipTree(&node);  printTree(&node);  printTree(tree);   return 0;}


Main Function 2

1234567891011121314151617181920212223242526272829303132333435#include <stdio.h>#include "operations.h" int main() {  Node tree1;  Node tree2;   for(int i = 0; i < 9; i++){    tree1[i].n = tree2[i].n = i;    tree1[i].left = tree1[i].right = NULL;    tree2[i].left = tree2[i].right = NULL;  }   tree1.left = &tree1;  tree1.right = &tree1;  tree1.left = &tree1;  tree1.right = &tree1;  tree1.left = &tree1;  tree1.right = &tree1;  tree1.left = &tree1;  tree1.right = &tree1;   tree2.left = &tree2;  tree2.right = &tree2;  tree2.left = &tree2;  tree2.right = &tree2;  tree2.left = &tree2;  tree2.right = &tree2;  tree2.left = &tree2;  tree2.right = &tree2;   printf("%d\n", isIdentical(&tree1, &tree2));  printf("%d\n", isIdentical(&tree1, &tree1));  return 0;}


Main Function 3

123456789101112131415161718192021222324#include <stdio.h>#include "operations.h" int main() {  Node tree1;   for(int i = 0; i < 9; i++){    tree1[i].n = i;    tree1[i].left = tree1[i].right = NULL;  }   tree1.left = &tree1;  tree1.right = &tree1;  tree1.left = &tree1;  tree1.right = &tree1;  tree1.left = &tree1;  tree1.right = &tree1;  tree1.left = &tree1;  tree1.right = &tree1;   depthAvg(&tree1);   return 0;}


## Compile

gcc -std=c99 -O2 main.c operations.c


• 30 points: To implement the flip operation.
• 25 points: To implement the identical testing operation.
• 45 points: To compute the average depth.

## Note

It is guaranteed that only the specific function will be executed for each subtask. For example, you can implement only the flip operation and isIdentical function is incomplete in operations.c. However, you need to check if your operations.c could compiled. Otherwise, you will get a CE (compile error).

## Input Format

• 0 < the number of nodes for each tree < 100001.

## Output Format

Print the result of each operation.

## Sample Output 1

01347NULL256NULL80268NULL514NULL73


## Sample Output 2

01


## Sample Output 3

10/4