MTH4300 Home

Videos About Trees, Sets, and Maps

Summary

The solution to the problem from the video 2:

Video 1: Introduction to binary search trees and AVL trees

Video 2: Sets and Maps in C++

1. Problems

Problem 1. The node of the binary tree is defined in the following way
class TN{
public:
    long content;
    TN* aLeft;
    TN* aRight;
};
The implementation of the function f is given below
long f(TN* a, long h){
    if(a==nullptr){
        return 1;
    }
    long c=a->content;
    return (c%10)+(h%2)*f(a->aLeft,h+1)+(c%2)*f(a->aRight,h+1);
}
Assume that aRoot is the pointer to the root of the tree

A binary search tree with 8 nodes. The root is 75, with left child 59 and right child 193. Node 59 has left child 201 (a leaf) and right child 519. Node 519 has left child 510 (a leaf) and right child 323. Node 323 has no left child and right child 24 (a leaf). Node 193 has left child 727 and no right child. Node 727 has no left child and right child 28 (a leaf).

What is the result of the evaluation f(aRoot,3)? Provide a rigorous justification for your answer.

Problem 2. What happens when the number \(55\) is inserted in the following AVL tree? What is the resulting tree after all the rotations?

A binary search tree with 9 nodes. The root is 40, with left child 20 and right child 70. Node 20 has left child 10 (a leaf) and right child 30 (a leaf). Node 70 has left child 50 and right child 95. Node 50 has left child 45 (a leaf) and right child 60 (a leaf). Node 95 has no left child and right child 99 (a leaf).