class Node {
constructor(data, left=null, right=null) {
this.data = data;
this.left = left;
this.right = right;
}
}
class BST {
constructor() {
this.root = null;
}
add (data) {
const node = this.root;
if (node === null) {
this.root = new Node(data);
return;
} else {
const serarchTree = function(node) {
// data < node.data then left
if (data < node.data) {
if (node.left === null) {
node.left = new Node(data);
return;
} else if (node.left !== null) {
return serarchTree(node.left);
}
}
// data > node.data then right
else if (data > node.data) {
if (node.right === null) {
node.right = new Node(data);
return;
} else if (node.right !== null) {
return serarchTree(node.right);
}
}
// if equal not insert in this tree
else {
return null;
}
};
return serarchTree(node);
}
}
// find min
findMin() {
let currentNode = this.root;
while (currentNode.left !== null) {
currentNode = currentNode.left;
}
return currentNode.data;
}
// find max
findMax() {
let currentNode = this.root;
while (currentNode.right !== null) {
currentNode = currentNode.right;
}
return currentNode.data;
}
// find data
find(data) {
let currentNode = this.root;
while (currentNode) {
if (data == currentNode) {
return true;
} else if (data > currentNode) {
currentNode = currentNode.right;
} else if (data < currentNode) {
currentNode = currentNode.left;
}
}
return false;
}
remove(data) {
const removeNode = function(node, data) {
if (node == null) {
return null;
}
if (data == node.data) {
// no child
if (node.left == null && node.right == null) {
return null;
}
// only one child
}
}
}
}
remove 만 마저 하고
후..............휴식휴식
후....근성33
후...
입술위에 후~~
후...