insert Btree with parents pointers - java

I tried writing an insert code in Java for Btree but cant split the nodes correctly, can anybody direct me to a good algorithm for insert,split and nonfullInset in Btrees?
thanks

Assuming per node:
maximum number of children for is D,
maximum number of keys is K= D-1,
minimum number of children is d = D/2,
minimum number of keys is k= D/2-1
here are the algorithms:
Insert: Search the tree to find the leaf node for containing the key. Insert the key. See if the key is over flown (the number of keys is higher than D-1), if not operation is complete. If over flown, Split the node into 2 nodes (itself and a new one), move the last (k) keys to the new node, leaving the node (leaf node) with k +1 keys. Move the last key in leaf node to parent. If parent is over flown repeat these steps.
Delete: Search the tree to find the node containing the key you want to remove. 2 cases are possible. The container node is a leaf node or it is an internal non-leaf node. If it is an internal node, first find the immediate predecessor of the key in the node , which always comes from a lead node. Then replace the key with this predecessor key. Now you need to delete the key from a leaf node. (notice that the deletion from an internal node is always reduced to deletion from a leaf node). If a leaf node, remove the key from the leaf node. Check if the leaf node is under flown (has less than k keys), if not operation is complete. If under flown, however 3 cases are possible. If the node has a left sibling that has at least k+1 keys, then perform a right rotation via parent. Else if the node has a right sibling that has at least k+1 keys, then perform a left rotation via parent. Else if the node does not have any sibling with at least k+1 keys, then join the node with one of its sibling, while bringing down a key from parent. Then check if parent is under flown, if under flown repeat these steps to fix the parent.
Search: is similar to binary search tree, the main difference is that now you need to also perform a search with in each node, since each node has sorted keys, you can perform a binary search over each node.

Related

Remove the node in Binary Search Tree

How can I remove all isRemoved=true; data in Binary Search Tree in one method?
Or two it doesn't matter.
There is a remove method already and it marks the node that is coming from method isRemoved=true.
So,constructor has four specilization they are Type data,left,right,isRemoved.
The method can work recursively or not doesn't matter.
My idea is traversing all of the nodes when I found the removed go and delete method and delete it then turn back and keep going look at the nodes. I couldn't implement the idea because BST is complicated. Can you guys give me some clue?
First you will need to decide how you are going to traverse your tree (inorder, preorder, postorder).
Then you will handle three separate cases for a node to be deleted; when the node is a leaf, when the node has one child, and when there are both a left and a right child.
If the node is a leaf, you can remove it.
If the node has a child, you can set the node's child to be the parent node's child, then remove the node.
If the node has two children, you can go to the subtree of the left child and replace the node with the rightmost child of the left subtree. Since the rightmost node in the subtree is a leaf, you can replace without rebalancing the tree.
Traverse the tree while removing the isRemoved=true nodes using one of the methods above until you reach the last node in the traversal.

Removing a node from a binary search tree in Java

I've implemented a binary search tree with nodes (represented as users) but I'm having trouble getting my deFriend() method to work. This method should delete a node from the tree whilst keeping the "rules" of a binary search tree - e.g. root bigger than left sub-tree, but smaller than right sub-tree.
I understand that when deleting a node from a binary search tree, I should consider 3 cases:
1 - when the node to delete has no children,
2 - when the node to delete has one child,
3 - when the node to delete has two children. can't figure this one out
My deFriend() method works for the first two cases but not for the last one. I even know what node I need to replace it with if it's the third case but I can't get it down with code. I can conceptualize it or draw it on paper, but I can't translate that to code. I've included links to the two classes that I think are necessary to fixing my problem. I'm convinced my problem lies between lines 111 and 114 of my BinaryTree class. Thanks in advance.
User class: https://gist.github.com/anonymous/732f02628f6edf622d88363b68cf22ee
BinaryTree class: https://gist.github.com/anonymous/1be7b5577c959dc1bcf6c77474b11bce
When removing a node from a binary search tree it is mandatory to maintain the in-order sequence of the nodes. There are three possible cases to consider:
if node has no children simply remove the node from the tree.
if node has one child remove the node and replace it with its child.
if node D has two children do not delete it. Instead, choose either its in-order predecessor node or its in-order successor node as replacement node E. Copy the user values of E to D. If E does not have a child simply remove E from its previous parent. If E has a child, say F, it is a right child. Replace E with F at E's parent.

Time complexity of deletion in binary search tree

Assume the height of the BST is h.
If we want to delete a node with two children, then what would be the time complexity of the process.
I know that in a normal binary tree, the time complexity for deletion is O(h); O(n) worst case and O(logn) best case. But since we are replacing the key of the deleting node by the minimum node of right sub tree of it, it will take more time to find the minimum key.
So does anybody know how to explain the time complexity in this situation?
Source Wikipedia :
Deletion
There are three possible cases to consider:
Deleting a leaf (node with no children): Deleting a leaf is easy, as we can simply remove it from the tree.
Deleting a node with one child: Remove the node and replace it with its child.
Deleting a node with two children: Call the node to be deleted N. Do not delete N. Instead, choose either its in-order successor node or its in-order predecessor node, R. Copy the value of R to N, then recursively call delete on R until reaching one of the first two cases. If you choose in-order successor of a node, as right sub tree is not NIL( Our present case is node has 2 children), then its in-order successor is node with least value in its right sub tree, which will have at a maximum of 1 sub tree, so deleting it would fall in one of first 2 cases.
Deleting a node with two children from a binary search tree. First the rightmost node in the left subtree, the inorder predecessor 6, is identified. Its value is copied into the node being deleted. The inorder predecessor can then be easily deleted because it has at most one child. The same method works symmetrically using the inorder successor labelled 9.
Consistently using the in-order successor or the in-order predecessor for every instance of the two-child case can lead to an unbalanced tree, so some implementations select one or the other at different times.
Runtime analysis:
Although this operation does not always traverse the tree down to a leaf, this is always a possibility; thus in the worst case it requires time proportional to the height of the tree. It does not require more even when the node has two children, since it still follows a single path and does not visit any node twice. Hence the pointer adjustments in all three cases need constant time.
Useful Links :
http://en.wikipedia.org/wiki/Binary_search_tree#Deletion
http://cse.iitkgp.ac.in/~pb/algo-1-pb-10.pdf

Dealing with "Keys" in Binary Search Trees

Should I always, use some data as a key value while dealing with the Binary Search trees? I'm asking this becasue I would need key at some point of time
if I want to search an element in the tree. Is thre any other alternative?
For example, please consider the following code:
class Node {
int iData; // data used as key value
double fData; // other data
Node leftChild; // this node's left child
Node rightChild; // this node's right child
}
My Second Question:
Is there any way I can find elements in a Binary Tree as Binary Tree doesn't have any property just like Binary Search Tree where left node of parent must be less than the parent and right node must be greater.
I don't see why you always need a key. You can just use double fData as the value you use to determine which node is bigger / smaller (if your requirement suits).
I believe Binary Seach Tree is a Binary Tree where the elements are ordered. Hence nodes on a Binary Tree is not necessarily ordered. So yes you can still find elements on a Binary Tree -- but you have to scan the entire node. You lose the performance benefit of a BST

Is this list a Binary Search Tree?

Is the following list a BST or not?
list:{2,5,3,8,6}
Is there a way I can determine this?
Consider that my list will have 100000 elements.
Quick answer is: no. BST is a tree and you have a list.
Long answer is: it may be possible to encode a tree as a list, in which case, whether your list can be decoded into a tree or not will depend on the encoding algorithm. See BST wiki page for more details http://en.wikipedia.org/wiki/Binary_search_tree
In fact your list may be an encoded version of BST. If you read elements from left to right pushing them onto a stack, and whenever you stack has 3 elements do:
parent = pop()
right = pop()
left = pop()
push new Node(parent, left, right)
then you should get a valid BST. But I am only speculating here.
you are having a list , you need to construct BST from this list
A BST has following properties
1- Each node has two children or it is a leaf node
2- For each node its left subtree is smaller than node's value
3- For each node its right subtree is greater than node's value
a bst MUST BE BALANCED i.e. while inserting nodes in a BST , code must respect above 3 conditions.
Searching in a BST is O(log n) operation that is because , each search step divide the search space into two halfs and choose one of the half.
There is a case where search will take O(N) time
Consider following
node = {1,2,3,4,5}
if we make the BST from this node set it will be right alifned that means every next node will be on the right subtree , here if we want to search for a item , we need to traverse whole right sub tree like a link list.

Categories

Resources