트리에서 노드를 제거하는 것은 멀리서 보면 상당히 복잡합니다. 노드를 제거할 때 고려해야 할 3가지 경우가 있습니다. 이들은 다음 기능의 주석에 언급되어 있습니다. 이전에 했던 것처럼 클래스에 메서드를 만들고 재귀적으로 호출하는 도우미를 만듭니다.
클래스 메소드
deleteNode(key) { // If a node is successfully removed, a reference will be received. return !(deleteNodeHelper(this.root, key) === false); }
도우미 방법
/** * Takes root and key and recursively searches for the key. * If it finds the key, there could be 3 cases: * * 1. This node is a leaf node. * * Example: Removing F * A * / \ * B C * / / \ * D E F * * To remove it, we can simply remove its parent's connection to it. * * A * / \ * B C * / / * D E * * 2. This node is in between the tree somewhere with one child. * * Example: Removing B * A * / \ * B C * / / \ * D E F * * To remove it, we can simply make the child node replace the parent node in the above connection * A * / \ * D C * / \ * E F * * 3. This node has both children. This is a tricky case. * * Example: Removing C * * A * / \ * B C * / / \ * D E F * / / \ * G H I * * In this case, we need to find either a successor or a predecessor of the node and replace this node with * that. For example, If we go with the successor, its successor will be the element just greater than it, * ie, the min element in the right subtree. So after deletion, the tree would look like: * * A * / \ * B H * / / \ * D E F * / \ * G I * * To remove this element, we need to find the parent of the successor, break their link, make successor's left * and right point to current node's left and right. The easier way is to just replace the data from node to be * deleted with successor and delete the sucessor. */ function deleteNodeHelper(root, key) { if (root === null) { // Empty tree return false; } if (key < root.data) { root.left = deleteNodeHelper(root.left, key); return root; } else if (key > root.data) { root.right = deleteNodeHelper(root.right, key); return root; } else { // No children //case 1 - a leaf node if (root.left === null && root.right === null) { root = null; return root; } // Single Child cases if (root.left === null) return root.right; if (root.right === null) return root.left; // Both children, so need to find successor let currNode = root.right; while (currNode.left !== null) { currNode = currNode.left; } root.data = currNode.data; // Delete the value from right subtree. root.right = deleteNodeHelper(root.right, currNode.data); return root; } }
다음을 사용하여 테스트할 수 있습니다.
예시
let BST = new BinarySearchTree(); BST.insertRec(10); BST.insertRec(15); BST.insertRec(5); BST.insertRec(50); BST.insertRec(3); BST.insertRec(7); BST.insertRec(12); BST.inOrder(); BST.deleteNode(15); BST.deleteNode(10); BST.deleteNode(3); BST.inOrder();
출력
이것은 출력을 줄 것입니다 -
5 7 12 50