Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
4.1 kB
1
Indexable
Never
void inorderTraversal(BinaryTreeNode<int> *root, vector<int> &inorderArr) {
    BinaryTreeNode<int> *curr = root;

    while(curr != NULL) {
        if (curr -> left == NULL) { // LST doesn’t exist so curr comes in inorder and then move to RST.
            inorderArr.push_back(curr -> data);
            curr = curr -> right;
        } else {
            BinaryTreeNode<int> *rightMost = curr -> left;
            bool connectThread = true;

            // Find rightMost element of curr's LST. Two cases are possible.
            // case 1. RightMost element reached -> then thread it i.e. point its right child to curr.
            // case 2. RightMost element reached but its already threaded -> This implies we have already visited LST. So remove thread & move on to curr's RST.
            while (rightMost -> right != NULL) {
                if (rightMost -> right == curr) { // case 2
                    rightMost -> right = NULL;
                    inorderArr.push_back(curr -> data);
                    curr = curr -> right;
                    connectThread = false;
                    break;
                }
                rightMost = rightMost -> right;
            }

            if (connectThread) { // case 1
                rightMost -> right = curr;
                curr = curr -> left;
            }
        }
    }
}

void preorderTraversal(BinaryTreeNode<int> *root, vector<int> &preorderArr) {
    BinaryTreeNode<int> *curr = root;

    while (curr != NULL) {
        if (curr -> left == NULL) { // LST doesn’t exist so curr comes in preorder and then move to RST.
            preorderArr.push_back(curr -> data);
            curr = curr -> right;
        } else {
            BinaryTreeNode<int> *rightMost = curr -> left;
            bool createThread = true;

            // Find rightMost element of curr's LST. Two cases are possible.
            // case 1. RightMost element reached -> then thread it i.e. point its right child to curr.
            // case 2. RightMost element reached but its already threaded -> This implies we have already visited LST. So remove thread & move on to curr's RST.
            while (rightMost -> right != NULL) { // case 2
                if (rightMost -> right == curr) {
                    rightMost -> right = NULL;
                    curr = curr -> right;
                    createThread = false;
                    break;
                }
                rightMost = rightMost -> right;
            }

            if (createThread) { // case 1
                rightMost -> right = curr;
                preorderArr.push_back(curr -> data);
                curr = curr -> left;
            }
        }
    }
}

void postorderTraversal(BinaryTreeNode<int> *root, vector<int> &postorderArr) {
    BinaryTreeNode<int> *curr = root;

    while (curr != NULL) {
        if (curr -> right == NULL) {
            postorderArr.push_back(curr -> data);
            curr = curr -> left;
        } else {
            BinaryTreeNode<int> *leftMost = curr -> right;
            bool createThread = true;

            while (leftMost -> left != NULL) {
                if (leftMost -> left == curr) {
                    leftMost -> left = NULL;
                    curr = curr -> left;
                    createThread = false;
                    break;
                }
                leftMost = leftMost -> left;
            }

            if (createThread) {
                leftMost -> left = curr;
                postorderArr.push_back(curr -> data);
                curr = curr -> right;
            }
        }
    }
}

vector<vector<int>> getTreeTraversal(BinaryTreeNode<int> *root){
    vector<vector<int>> traversal(3);
    inorderTraversal(root, traversal[0]);

    preorderTraversal(root, traversal[1]);

    postorderTraversal(root, traversal[2]); // Post order is = root, right, left -> and then reverse the output.
    reverse(traversal[2].begin(), traversal[2].end());

    return traversal;
}