Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // task 1
- BST(){
- root = NULL;
- }
- void insert(int value){
- root = insert(root, value);
- }
- void realPrint(Node* root, int dividedBy)
- {
- if(root == nullptr)
- return;
- bool isLeaf = root->left || root->right;
- if(root->value % dividedBy == 0 && isLeaf)
- {
- cout << root->value << " ";
- }
- realPrint(root->left, dividedBy);
- realPrint(root->right, dividedBy);
- }
- void printSpecific(int dividedBy) {
- realPrint(root, dividedBy);
- }
- // task 2
- BST() {
- root = NULL;
- }
- void insert(int value) {
- root = insert(root, value);
- }
- void realPrint(Node* root, int k, int currDist)
- {
- if(root == nullptr)
- return;
- realPrint(root->right, k, currDist + 1);
- if(currDist == k)
- {
- cout << root->value << " ";
- }
- realPrint(root->left, k, currDist + 1);
- }
- void printKDistant(int k) {
- realPrint(root, k, 0);
- }
- // task 3
- struct Node {
- Node *left;
- Node *right;
- int value;
- bool isLeaf;
- Node(int value) {
- this->value = value;
- this->left = NULL;
- this->right = NULL;
- }
- };
- class BST {
- public:
- BST() {
- root = NULL;
- }
- void insert(int value) {
- root = insert(root, value);
- }
- void leafs(Node* root)
- {
- if(root == nullptr)
- {
- return;
- }
- leafs(root->left);
- if(root->left == nullptr && root->right == nullptr)
- {
- root->isLeaf = true;
- }
- else
- root->isLeaf = false;
- leafs(root->right);
- }
- void mydelete(Node* root)
- {
- if(root == nullptr)
- {
- return;
- }
- mydelete(root->left);
- bool toBeDeleted1 = false;
- bool toBeDeleted2 = false;
- bool toBeDeleted3 = false;
- if(root->left && root->right)
- toBeDeleted1 = root->left->isLeaf && root->right->isLeaf;
- if(root->left)
- toBeDeleted2 = root->left->isLeaf && root->right == nullptr;
- if(root->right)
- toBeDeleted3 = root->left == nullptr && root->right->isLeaf;
- bool del = toBeDeleted1 || toBeDeleted2 || toBeDeleted3;
- if(!del)
- cout << root->value << " ";
- mydelete(root->right);
- }
- void deletePenultimateAndPrint() {
- leafs(root);
- mydelete(root);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement