Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cmath>
- #include <cstdio>
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include <unordered_map>
- using namespace std;
- struct Node {
- Node *left;
- Node *right;
- int value;
- 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 printLeftProfile() {
- getLeftMostIndexes(root, 0);
- for(int i = min; i <= max; i++){
- cout << getValue[i] << " ";
- }
- }
- //до ТУК
- private:
- //моят код е от ТУК
- unordered_map<int, int> getValue; //height
- int min = 0;
- int max = 0;
- void getLeftMostIndexes(Node* node, int height){
- if(node == nullptr)
- return;
- if(getValue.count(height) == 0)
- getValue[height] = node->value;
- if(height < min)
- min = height;
- if(height > max)
- max = height;
- getLeftMostIndexes(node->left, height + 1);
- getLeftMostIndexes(node->right, height + 1);
- }
- //до ТУК
- Node* root;
- Node* insert(Node *curNode, int value) {
- if (curNode == NULL) {
- curNode = new Node(value);
- } else if (curNode->value < value) {
- curNode->right = insert(curNode->right, value);
- } else if (curNode->value > value) {
- curNode->left = insert(curNode->left, value);
- } else {
- //if we already have this value at the tree - do nothing
- }
- return curNode;
- }
- };
- int main() {
- int n;
- cin >> n;
- int value;
- BST tree;
- for(int i = 0 ; i < n; i++) {
- cin >> value;
- tree.insert(value);
- }
- tree.printLeftProfile();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment