Deletion of the leaf node from B-tree - algorithm

What are the rules of deleting nodes from a leaf nodes in a B tree. I have given an example below. I need to delete keys, J,K,U from leaf nodes. the 't' of the B tree is 3. so the minimum number of keys in a node should be 2.
J can be deleted without any issue.
But when J is deleted, the remaining would be K,L. Next when deleting K, since the node contains 2 nodes, K cannot be deleted directly.
Since its sibling node, which is N,O also contains its minimum nodes what should I perform here? Is it a merge?
How can I delete K and also U.
Please help.

I referred this book Introduction-to-algorithms-3rd-edition by Thomas H Cormen and he explained it very well.
Here are the 3 steps that include all the cases.Hope it helps.
If the key k is in node x and x is a leaf, delete the key k from x.
If the key k is in node x and x is an internal node, do the following:
a. If the child y that precedes k in node x has at least t keys, then find the predecessor k' of k in the subtree rooted at y. Recursively delete k0,and
replace k by k' in x. (We can find k0 and delete it in a single downward
pass.)
b. If y has fewer than t keys, then, symmetrically, examine the child z that
follows k in node x. If z has at least t keys, then find the successor k' of k in the subtree rooted at z. Recursively delete k' ,and replace k by k' in x. (We
can find k' and delete it in a single downward pass.)
c. Otherwise, if both y and z have only t-1 keys, merge k and all of z into y,
so that x loses both k and the pointer to z, and y now contains 2t-1 keys.
Then free z and recursively delete k from y.
If the key k is not present in internal node x, determine the root x.ci of the appropriate subtree that must contain k, if k is in the tree at all. If x.ci has only t-1 keys, execute step 3a or 3b as necessary to guarantee that we descend to a node containing at least t keys. Then finish by recursing on the appropriate child of x.
a. If x.ci has only t-1 keys but has an immediate sibling with at least t keys, give x.ci an extra key by moving a key from x down into x.ci, moving a
key from x.ci’s immediate left or right sibling up into x, and moving the
appropriate child pointer from the sibling into x.ci.
b. If x.ci and both of x.ci’s immediate siblings have t-1 keys, merge x.ci with one sibling, which involves moving a key from x down into the new
merged node to become the median key for that node.

Related

AVL TREE traversal in order

Can someone help me, how I could traversal a balanced binary tree in order without recursion, stack, or morris traversal. I want to traverse it iteratively without modifying the tree.
Thank you so much.
In the case where there are no duplicate keys, this corresponds to the tree representing a set (or map.) In that case, a backtracking approach will be O(log n) (AVL tree property) per key. One could get a faster run time by storing the nodes, (such as in recursion,) but often this is unfeasible.
If current is not null, descend next with the current key as the target. Whenever the left is taken, first assign an ancestor (before descending.)
There are three cases: current was null -> next = root; current == next has a right child, next <- next.right; or next does not have a right child, next <- ancestor (if it does not exist, finished.)
In the first two cases, descend on the left until you hit a leaf.
I'll use Wikipedia AVL Tree article example, without the balance factors, (this will work on any binary tree, but one is not guaranteed performance.)
current
path
result
null
root J
C
C
ancestor D
D
D
ancestor F
F
F
right G
G
G
ancestor J
J
J
right P
N
N
ancestor L
L
L
ancestor P
P
P
right V
Q
Q
ancestor S
S
S
right U
U
U
ancestor V
V
V
right X
X
X
ancestor null
null
If the tree can have duplicate entries, this might be said to be a multiset. In this case, this will not work because this relies on the keys being unique.

Does a subsequence in a tree certificate guarantees it contains given tree?

I am using an algorithm for tree certificates described for example here (p. 24-29).
Let's say I have two trees: A and B, and each tree has it's certificate produced by the algorithm above (C1 and C2).
Is it true, that if C1 contains C2 (exact sequence anywhere), it means A contains B as a subtree (B can be basically concentrated and considered as a leaf node of A)? If not, could you state a counter-example?
--edit--
Algorithm: (please take a look at the linked document for examples):
Label all vertices with string 01
While there are more than 2 vertices in G:
for each non-leaf x do:
let Y be the set of labels of the leaves adjacent to X and the label of x with initial 0 and trailing 1 deleted from x.
Replace the label of x with the concentration of the labels in Y, sorted in increasing lexicographic orher, with a 0 prepended and a 1 appended.
Remove all leaves adjacent to x.
If there is only one vertex x left, report x's label as the certificate.
If there are 2 vertices x and y left, concentrate x and y in increasing lexicographic order, and report it as the cerfificate.
Yes, it is true.
Assuming the certificate is correct, there is no possibility a certificate would contain another certificate and it wouldn't be it's subtree.

B-tree: delete a non-leaf node?

Im studying B tree and in the book they said that:
If the key k is in node x and x is a leaf, delete the key k from x.
If the key k is in node x and x is an internal node, do the following.
a. If the child y that precedes k in node x has at least t keys, then find the predecessor k' of k in the subtree rooted at y. Recursively delete k', and replace k by k' in x. (Finding k' and deleting it can be performed in a single downward pass.)
b. Symmetrically, if the child z that follows k in node x has at least t keys, then find the successor k' of k in the subtree rooted at z. Recursively delete k', and replace k by k' in x. (Finding k' and deleting it can be performed in a single downward pass.)
c. Otherwise, if both y and z have only t- 1 keys, merge k and all of z into y, so that x loses both k and the pointer to z, and y now contains 2t - 1 keys. Then, free z and recursively delete k from y.
My question is this: In case 2.a.
Can someone explain me with example: Recursively delete k', and replace k by k' in x.
Regards.
Suppose you have b tree with the degree t = 4:
26, 49, 60
27,31,34,36 51,55,56,58
Suppose y = [27,31,34,36], x = [51,55,56,58] are not leaf nodes and you want to delete key k = 51. Let K = 49 be the key in the parent node which splits x and y.
Find the predecessor of k which is the rightmost key k' in the subtree y (which could contain integers between 37 and 48 in this example, say k' = 40). Set k = K = 49 and K = k' = 40 and delete k' recursively (which is actually the first case of the deletion procedure when the node is leaf). The resulting b tree looks like
26, 40, 60
27,31,34,36 49,55,56,58

How to delete a node in Ternary Tree?

I am working on implementing a Java program on inserting and deleting a node in a ternary tree.
I am able to implement insertion without any issues, but I'm facing a few hiccups in implementing the deletion operation.
So, my question is:
How to delete a node from ternary tree if it has one or more child nodes?
It will be great if you can share any logic or pseudo-code to implement the "delete" functionality.
I found a solution.
Suppose n is the node we want to delete, l is its left child, r is its right child and m is its middle child.
If n is a root node, then make n null.
If n is not a root node, then check if m is not null. If so, simply invoke recursively the current procedure on m, since m matches n in value: we will delete the last matching node!
If m is null, then we have the following possible cases:
If both l and r are null, then make l, r and m values in the parent node n to be null.
If only one node, say x, (either l or r) is not null, then replace x non-null value with n's value, and delete x.
If both l and r are not null, then find the node z with maximum value in the left sub-tree of n, and replace z's value to with n's node, and delete z.

How to find largest common sub-tree in the given two binary search trees?

Two BSTs (Binary Search Trees) are given. How to find largest common sub-tree in the given two binary trees?
EDIT 1:
Here is what I have thought:
Let, r1 = current node of 1st tree
r2 = current node of 2nd tree
There are some of the cases I think we need to consider:
Case 1 : r1.data < r2.data
2 subproblems to solve:
first, check r1 and r2.left
second, check r1.right and r2
Case 2 : r1.data > r2.data
2 subproblems to solve:
- first, check r1.left and r2
- second, check r1 and r2.right
Case 3 : r1.data == r2.data
Again, 2 cases to consider here:
(a) current node is part of largest common BST
compute common subtree size rooted at r1 and r2
(b)current node is NOT part of largest common BST
2 subproblems to solve:
first, solve r1.left and r2.left
second, solve r1.right and r2.right
I can think of the cases we need to check, but I am not able to code it, as of now. And it is NOT a homework problem. Does it look like?
Just hash the children and key of each node and look for duplicates. This would give a linear expected time algorithm. For example, see the following pseudocode, which assumes that there are no hash collisions (dealing with collisions would be straightforward):
ret = -1
// T is a tree node, H is a hash set, and first is a boolean flag
hashTree(T, H, first):
if (T is null):
return 0 // leaf case
h = hash(hashTree(T.left, H, first), hashTree(T.right, H, first), T.key)
if (first):
// store hashes of T1's nodes in the set H
H.insert(h)
else:
// check for hashes of T2's nodes in the set H containing T1's nodes
if H.contains(h):
ret = max(ret, size(T)) // size is recursive and memoized to get O(n) total time
return h
H = {}
hashTree(T1, H, true)
hashTree(T2, H, false)
return ret
Note that this is assuming the standard definition of a subtree of a BST, namely that a subtree consists of a node and all of its descendants.
Assuming there are no duplicate values in the trees:
LargestSubtree(Tree tree1, Tree tree2)
Int bestMatch := 0
Int bestMatchCount := 0
For each Node n in tree1 //should iterate breadth-first
//possible optimization: we can skip every node that is part of each subtree we find
Node n2 := BinarySearch(tree2(n.value))
Int matchCount := CountMatches(n, n2)
If (matchCount > bestMatchCount)
bestMatch := n.value
bestMatchCount := matchCount
End
End
Return ExtractSubtree(BinarySearch(tree1(bestMatch)), BinarySearch(tree2(bestMatch)))
End
CountMatches(Node n1, Node n2)
If (!n1 || !n2 || n1.value != n2.value)
Return 0
End
Return 1 + CountMatches(n1.left, n2.left) + CountMatches(n1.right, n2.right)
End
ExtractSubtree(Node n1, Node n2)
If (!n1 || !n2 || n1.value != n2.value)
Return nil
End
Node result := New Node(n1.value)
result.left := ExtractSubtree(n1.left, n2.left)
result.right := ExtractSubtree(n1.right, n2.right)
Return result
End
To briefly explain, this is a brute-force solution to the problem. It does a breadth-first walk of the first tree. For each node, it performs a BinarySearch of the second tree to locate the corresponding node in that tree. Then using those nodes it evaluates the total size of the common subtree rooted there. If the subtree is larger than any previously found subtree, it remembers it for later so that it can construct and return a copy of the largest subtree when the algorithm completes.
This algorithm does not handle duplicate values. It could be extended to do so by using a BinarySearch implementation that returns a list of all nodes with the given value, instead of just a single node. Then the algorithm could iterate this list and evaluate the subtree for each node and then proceed as normal.
The running time of this algorithm is O(n log m) (it traverses n nodes in the first tree, and performs a log m binary-search operation for each one), putting it on par with most common sorting algorithms. The space complexity is O(1) while running (nothing allocated beyond a few temporary variables), and O(n) when it returns its result (because it creates an explicit copy of the subtree, which may not be required depending upon exactly how the algorithm is supposed to express its result). So even this brute-force approach should perform reasonably well, although as noted by other answers an O(n) solution is possible.
There are also possible optimizations that could be applied to this algorithm, such as skipping over any nodes that were contained in a previously evaluated subtree. Because the tree-walk is breadth-first we know than any node that was part of some prior subtree cannot ever be the root of a larger subtree. This could significantly improve the performance of the algorithm in certain cases, but the worst-case running time (two trees with no common subtrees) would still be O(n log m).
I believe that I have an O(n + m)-time, O(n + m) space algorithm for solving this problem, assuming the trees are of size n and m, respectively. This algorithm assumes that the values in the trees are unique (that is, each element appears in each tree at most once), but they do not need to be binary search trees.
The algorithm is based on dynamic programming and works with the following intution: suppose that we have some tree T with root r and children T1 and T2. Suppose the other tree is S. Now, suppose that we know the maximum common subtree of T1 and S and of T2 and S. Then the maximum subtree of T and S
Is completely contained in T1 and r.
Is completely contained in T2 and r.
Uses both T1, T2, and r.
Therefore, we can compute the maximum common subtree (I'll abbreviate this as MCS) as follows. If MCS(T1, S) or MCS(T2, S) has the roots of T1 or T2 as roots, then the MCS we can get from T and S is given by the larger of MCS(T1, S) and MCS(T2, S). If exactly one of MCS(T1, S) and MCS(T2, S) has the root of T1 or T2 as a root (assume w.l.o.g. that it's T1), then look up r in S. If r has the root of T1 as a child, then we can extend that tree by a node and the MCS is given by the larger of this augmented tree and MCS(T2, S). Otherwise, if both MCS(T1, S) and MCS(T2, S) have the roots of T1 and T2 as roots, then look up r in S. If it has as a child the root of T1, we can extend the tree by adding in r. If it has as a child the root of T2, we can extend that tree by adding in r. Otherwise, we just take the larger of MCS(T1, S) and MCS(T2, S).
The formal version of the algorithm is as follows:
Create a new hash table mapping nodes in tree S from their value to the corresponding node in the tree. Then fill this table in with the nodes of S by doing a standard tree walk in O(m) time.
Create a new hash table mapping nodes in T from their value to the size of the maximum common subtree of the tree rooted at that node and S. Note that this means that the MCS-es stored in this table must be directly rooted at the given node. Leave this table empty.
Create a list of the nodes of T using a postorder traversal. This takes O(n) time. Note that this means that we will always process all of a node's children before the node itself; this is very important!
For each node v in the postorder traversal, in the order they were visited:
Look up the corresponding node in the hash table for the nodes of S.
If no node was found, set the size of the MCS rooted at v to 0.
If a node v' was found in S:
If neither of the children of v' match the children of v, set the size of the MCS rooted at v to 1.
If exactly one of the children of v' matches a child of v, set the size of the MCS rooted at v to 1 plus the size of the MCS of the subtree rooted at that child.
If both of the children of v' match the children of v, set the size of the MCS rooted at v to 1 plus the size of the MCS of the left subtree plus the size of the MCS of the right subtree.
(Note that step (4) runs in expected O(n) time, since it visits each node in S exactly once, makes O(n) hash table lookups, makes n hash table inserts, and does a constant amount of processing per node).
Iterate across the hash table and return the maximum value it contains. This step takes O(n) time as well. If the hash table is empty (S has size zero), return 0.
Overall, the runtime is O(n + m) time expected and O(n + m) space for the two hash tables.
To see a correctness proof, we proceed by induction on the height of the tree T. As a base case, if T has height zero, then we just return zero because the loop in (4) does not add anything to the hash table. If T has height one, then either it exists in T or it does not. If it exists in T, then it can't have any children at all, so we execute branch 4.3.1 and say that it has height one. Step (6) then reports that the MCS has size one, which is correct. If it does not exist, then we execute 4.2, putting zero into the hash table, so step (6) reports that the MCS has size zero as expected.
For the inductive step, assume that the algorithm works for all trees of height k' < k and consider a tree of height k. During our postorder walk of T, we will visit all of the nodes in the left subtree, then in the right subtree, and finally the root of T. By the inductive hypothesis, the table of MCS values will be filled in correctly for the left subtree and right subtree, since they have height ≤ k - 1 < k. Now consider what happens when we process the root. If the root doesn't appear in the tree S, then we put a zero into the table, and step (6) will pick the largest MCS value of some subtree of T, which must be fully contained in either its left subtree or right subtree. If the root appears in S, then we compute the size of the MCS rooted at the root of T by trying to link it with the MCS-es of its two children, which (inductively!) we've computed correctly.
Whew! That was an awesome problem. I hope this solution is correct!
EDIT: As was noted by #jonderry, this will find the largest common subgraph of the two trees, not the largest common complete subtree. However, you can restrict the algorithm to only work on subtrees quite easily. To do so, you would modify the inner code of the algorithm so that it records a subtree of size 0 if both subtrees aren't present with nonzero size. A similar inductive argument will show that this will find the largest complete subtree.
Though, admittedly, I like the "largest common subgraph" problem a lot more. :-)
The following algorithm computes all the largest common subtrees of two binary trees (with no assumption that it is a binary search tree). Let S and T be two binary trees. The algorithm works from the bottom of the trees up, starting at the leaves. We start by identifying leaves with the same value. Then consider their parents and identify nodes with the same children. More generally, at each iteration, we identify nodes provided they have the same value and their children are isomorphic (or isomorphic after swapping the left and right children). This algorithm terminates with the collection of all pairs of maximal subtrees in T and S.
Here is a more detailed description:
Let S and T be two binary trees. For simplicity, we may assume that for each node n, the left child has value <= the right child. If exactly one child of a node n is NULL, we assume the right node is NULL. (In general, we consider two subtrees isomorphic if they are up to permutation of the left/right children for each node.)
(1) Find all leaf nodes in each tree.
(2) Define a bipartite graph B with edges from nodes in S to nodes in T, initially with no edges. Let R(S) and T(S) be empty sets. Let R(S)_next and R(T)_next also be empty sets.
(3) For each leaf node in S and each leaf node in T, create an edge in B if the nodes have the same value. For each edge created from nodeS in S to nodeT in T, add all the parents of nodeS to the set R(S) and all the parents of nodeT to the set R(T).
(4) For each node nodeS in R(S) and each node nodeT in T(S), draw an edge between them in B if they have the same value AND
{
(i): nodeS->left is connected to nodeT->left and nodeS->right is connected to nodeT->right, OR
(ii): nodeS->left is connected to nodeT->right and nodeS->right is connected to nodeT->left, OR
(iii): nodeS->left is connected to nodeT-> right and nodeS->right == NULL and nodeT->right==NULL
(5) For each edge created in step (4), add their parents to R(S)_next and R(T)_next.
(6) If (R(S)_next) is nonempty {
(i) swap R(S) and R(S)_next and swap R(T) and R(T)_next.
(ii) Empty the contents of R(S)_next and R(T)_next.
(iii) Return to step (4).
}
When this algorithm terminates, R(S) and T(S) contain the roots of all maximal subtrees in S and T. Furthermore, the bipartite graph B identifies all pairs of nodes in S and nodes in T that give isomorphic subtrees.
I believe this algorithm has complexity is O(n log n), where n is the total number of nodes in S and T, since the sets R(S) and T(S) can be stored in BST’s ordered by value, however I would be interested to see a proof.

Resources