half-dreamer / AP1400-2-HW3

0 stars 0 forks source link

Advanced Programming - HW3

Homework 3 - Spring 2022 Semester
Deadline: Sunday Farvardin 14st - 11:59 pm

Outline

In this homework we are going to implement a Binary Search Tree (BST). A binary tree is a tree graph in which nodes can only have upto 2 children. A Binary Search Tree is a binary tree in which the right child of each node have a greater value than the left one.

We are going to implement 2 classes one of them is called Node which represents each node in the graph, and the other one is BST which is responsible to conncet nodes in a way to construct a binary search tree. Since the Node class is a direct property of the BST class; It should be defined inside the BST class.

note. You are only allowed to alter bst.cpp/h and only the debug section of main.cpp.


Node Class

Use the code fraction bellow to implement this class. note. you may need to add some keywords to these functions if necessary. other than these keywords you are not allowed to change the functions or add new member functions to this class unless otherwise specified in the following.

class Node
{
public:
    Node(int value, Node* left, Node* right);
    Node();
    Node(const Node& node);

    int value;
    Node* left;
    Node* right;
};

Each node has a integer value and can point to its children using left and right pointers. If one or more children does not exist assign nullptr to them.


BST Class

Use the code fraction bellow to implement this class. note. you may need to add some keywords to these functions if necessary. other than these keywords you are not allowed to change the functions or add new member functions to this class unless otherwise specified in the following.

class BST
{
public:
    Node*& get_root();
    void bfs(std::function<void(Node*& node)> func);
    size_t length();
    bool add_node(int value);
    Node** find_node(int value);
    Node** find_parrent(int value);
    Node** find_successor(int value);
    bool delete_node(int value);

private:
    Node* root;
};

This class is responsible to construct a BST graph by connceting the nodes in a proper manner. root is the only member variable of this class which points to the first node of the binary search tree.


Challenge

Finally

As mentioned before, do not alter other files already populated except otherwise indicated. In case you want to test your code you may use the debug section of the main.cpp.

if (true) // make false to run unit tests  
{ 
    // debug section 
}  
else  
{  
    ::testing::InitGoogleTest(&argc, argv);  
    std::cout << "RUNNING TESTS ..." << std::endl;  
    int ret{RUN_ALL_TESTS()};  
    if (!ret)  
        std::cout << "<<<SUCCESS>>>" << std::endl;  
    else  
      std::cout << "FAILED" << std::endl;  
}  
return 0;


GOOD LUCK