Advertisement
stanevplamen

BST JS

Sep 23rd, 2022 (edited)
499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 0.90 KB | Software | 0 0
  1. // https://adrianmejia.com/data-structures-for-beginners-trees-binary-search-tree-tutorial/
  2. // https://www.youtube.com/c/BackToBackSWE
  3. // binary search tree
  4. const LEFT = 0;
  5. const RIGHT = 1;
  6.  
  7. class TreeNode {
  8.   constructor(value) {
  9.     this.value = value;
  10.     this.descendants = [];
  11.     this.parent = null;
  12.   }
  13.  
  14.   get left() {
  15.     return this.descendants[LEFT];
  16.   }
  17.  
  18.   set left(node) {
  19.     this.descendants[LEFT] = node;
  20.     if (node) {
  21.       node.parent = this;
  22.     }
  23.   }
  24.  
  25.   get right() {
  26.     return this.descendants[RIGHT];
  27.   }
  28.  
  29.   set right(node) {
  30.     this.descendants[RIGHT] = node;
  31.     if (node) {
  32.       node.parent = this;
  33.     }
  34.   }
  35. }
  36.  
  37. ///
  38.  
  39.  
  40. class BinarySearchTree {
  41.   constructor() {
  42.     this.root = null;
  43.     this.size = 0;
  44.   }
  45.  
  46.   add(value) { /* ... */ }
  47.   find(value) { /* ... */ }
  48.   remove(value) { /* ... */ }
  49.   getMax() { /* ... */ }
  50.   getMin() { /* ... */ }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement