본문 바로가기
✘✘✘ Algorithm + Data structure/자료구조

[BST] Construct binary search tree methods :: insert, contains, remove(delete)

by PrettyLog 2022. 7. 4.

BST definition

BST

class BST {
    constructor(value) {
        this.value = value;
          this.left = null;
          this.right = null;
    }


    insert(value) {
        let currentNode = this;
        while (currentNode !== null) {
            if (currentNode.value > value) {
                if (currentNode.left === null) {
                    currentNode.left = new BST(value);
                } else {
                    currentNode.left.insert(value);
                }
            } else {
                if (currentNode.right === null) {
                    currentNode.right = new BST(value);
                } else {
                    currentNode.right.insert(value);
                }
            }
        }
    }

    // recursively
    insert(value) {
        if (value < this.value) {
            if (this.left === null) {
                this.left = new BST(value);
            } else {
                this.left.insert(value);
            }
        } else {
            if (this.right === null) {
                this.right = new BST(value);
            } else  {
                this.right.insert(value);
            }
        }
    }

    // Average: O(logN) time, O(logN) space
      // Worst: O(N) time, O(N) space
    contins(value) {
        let currentNode = this;
        while (currentNode !== this) {
            if (currentNode.value === value) {
                return true;
            } else if (currentNode.value > value) {
                if (currentNode.left !== null) {
                    currentNode = currentNode.left
                } else {
                    return false;
                }
            } else if (currentNode.value < value) {
                if (currentNode.right !== null) {
                    currentNode = currentNode.right;
                } else {
                    return false;
                }
            }
        }
    }

    // Average: O(logN) time, O(logN) space
      // Worst: O(N) time, O(N) space
    // recursively
    contins(value) {
        if (value < this.value) {
            if (this.left === null) {
                return false;
            } else {
                return this.left.contains(value);
            }
        } else if (value > this.value) {
            if (this.right === null) {
                return false;
            } else {
                return this.right.cotains(value);
            }
        } else {
            return true;
        }

    }

    // Average: O(logN) time, O(logN) space
    // Worst: O(N) time, O(N) space
    remove(value, parentNode = null) {
        let currentNode = this;

        while(currentNode !== null) {
            if (currentNode.value > value) {
                parentNode = currentNode;
                currentNode = currentNode.left;
            } else if (currentNode.value < value) {
                parentNode = currentNode;
                currentNode = currentNode.right;
            } else if (currentNode.value === value) {
              if (currentNode.left !== null && currentNode.right !== null) {
                  currentNode.value = currentNode.right.getMinValue();
                  currentNode.right.remove(currentNode.value, currentNode);
              } else if (parentNode === null) {
                  if (currentNode.left !== null) {
                    currentNode.value = currentNode.left.value;
                    // be cautious about sequence to assin right -> left
                    currentNode.right = currentNode.left.right; 
                    currentNode.left = currentNode.left.left;
                } else if (currentNode.right !== null) {
                    currentNode.value = currentNode.right.value;
                    currentNode.left = currentNode.right.left;
                    currentNode.right = currentNode.right.right;
                } else {
                    // Both left and right are null
                    // hmmm? Nothing to do
                }

              } else if (parentNode.left === currentNode) {
                parentNode.left = currentNode.left !== null ? currentNode.left : currentNode.right;
              } else if (currentNode.right !== null) {
                parentNode.right = currentNode.left !== null ? currentNode.left : currentNode.right;
              }
              break;
            }

        }
    }

    getMinValue() {
        let currentNode = this;
        while (currentNode !== null) {
          currentNode = currentNode.left;
        }
        return currentNode.value;
    }

    getMinValue() {
        if (this.left === null) {
          return this.value;
        } else {
          return this.left.getMinValue();
        }
    }

}

댓글