Binary Search Tree

1
2
3
4
5
6
7
class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
class BinarySearchTree {
  constructor() {
    this.root = null;
  }

  insert(val) {
    const newNode = new Node(val);

    if (this.root === null) {
      this.root = newNode;
      return this;
    }

    let curr = this.root;

    while (true) {
      if (val === curr.val) return undefined;
      if (val < curr.val) {
        if (curr.left === null) {
          curr.left = newNode;
          return this;
        }
        curr = curr.left;
      } else {
        if (curr.right === null) {
          curr.right = newNode;
          return this;
        }
        curr = curr.right;
      }
    }
  }

  find(val) {
    if (this.root === null) return false;

    let curr = this.root;
    let found = false;

    while (curr && !found) {
      if (val < curr.val) {
        curr = curr.left;
      } else if (val > curr.val) {
        curr = curr.right;
      } else {
        found = true;
      }
    }

    if (!found) return undefined;
    return curr;
  }

  contains(val) {
    if (this.root === null) return false;

    let curr = this.root;
    let found = false;

    while (curr && !found) {
      if (val < curr.val) {
        curr = curr.left;
      } else if (val > curr.val) {
        curr = curr.right;
      } else {
        return true;
      }
    }

    return false;
  }

  bfs() {
    let node = this.root;
    const ret = [];
    const queue = [node];

    while (queue.length) {
      node = queue.shift();
      ret.push(node.val);
      if (node.left) queue.push(node.left);
      if (node.right) queue.push(node.right);
    }

    return ret;
  }

  dfs() {
    let node = this.root;
    const ret = [];
    const stack = [node];

    while (stack.length) {
      node = stack.pop();
      ret.push(node.val);
      if (node.right) stack.push(node.right);
      if (node.left) stack.push(node.left);
    }

    return ret;
  }

  preOrder() {
    const ret = [];

    const traverse = node => {
      ret.push(node.val);
      if (node.left) traverse(node.left);
      if (node.right) traverse(node.right);
    };

    traverse(this.root);

    return ret;
  }

  postOrder() {
    const ret = [];

    const traverse = node => {
      if (node.left) traverse(node.left);
      if (node.right) traverse(node.right);
      ret.push(node.val);
    };

    traverse(this.root);

    return ret;
  }

  inOrder() {
    const ret = [];

    const traverse = node => {
      if (node.left) traverse(node.left);
      ret.push(node.val);
      if (node.right) traverse(node.right);
    };

    traverse(this.root);

    return ret;
  }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
var tree = new BinarySearchTree();

tree.insert(10);
tree.insert(6);
tree.insert(3);
tree.insert(8);
tree.insert(15);
tree.insert(20);

//     10
//   6    15
// 3  8     20

tree.bfs(); // [10, 6, 15, 3, 8, 20]
tree.dfs(); // [10, 6, 3, 8, 15, 20]
tree.preOrder(); // [10, 6, 3, 8, 15, 20]
tree.postOrder(); // [3, 8, 6, 20, 15, 10]
tree.inOrder(); // [3, 6, 8, 10, 15, 20]