I want to calculate the no. of nodes touched while deleting a particular node in a binary search tree. I am unable to figure out where to put the statement count++; to fix the code.
public int delete(K key) {
count=0;
if(this.contains(key)){
int pos=(int)djb2(key.toString(),BHTable.length);
BTNode root = BHTable[pos];
BHTable[pos] = deleteRec(root, key);
if(count2!=0)System.out.println(count2);
else System.out.println(count);
}
else System.out.println("E");
return -1;
}
T odel;
public BTNode deleteRec(BTNode root, K key) {
if (root == null) return root;
if (((Comparable)key).compareTo(root.key)<0)
{count++; root.left = deleteRec(root.left, key);}
else if (((Comparable)key).compareTo(root.key) >0)
{count++; root.right = deleteRec(root.right, key);}
else if(((Comparable)key).compareTo(root.key)==0&&! root.key.toString().equals(key.toString())))
{count++; root.right = deleteRec(root.right, key);}
else if(root.key.toString().equals(key.toString())){
if (root.left == null)
{ count++; return root.right;}
else if (root.right == null)
{ count++; return root.left;}
root.key = minValue(root.right);
root.obj = odel;
root.right = deleteRec(root.right,(K)root.key);
}
return root;
}
K minValue(BTNode root) {
K minv =(K) root.key;
while (root.left != null) {
minv =(K) root.left.key;
root = root.left;
count++;
}
odel=(T)root.obj;
return minv;
}
The displayed output is many times differing with the expected output by +-1.
Related
I have written a Code for AVL Tree Insertion but when I try to print the value of Root Node it always returns Null. I am unable to understand the reason.Anyone who can solve this problem? I have tried many times but I could not resolve the problem. I am confused. I hope that someone from here will help in the case of resolving the problem I have as I am sure there are high level of experts here.
public class AVLTreeMethods {
public Node root = null;
public int height(Node node){
if (node == null)
return 0;
return node.height;
}
public int max(Node node1, Node node2){
if (node1.height > node2.height)
return node1.height;
return node2.height;
}
public Node rotateRight(Node node){
Node newNode = node.left;
node.left = newNode.right;
newNode.right = node;
node.height = max(node.left,node.right) + 1;
newNode.height = max(newNode.left, newNode.right) + 1;
return newNode;
}
public Node rotateleft(Node node){
Node newNode = node.right;
node.right = newNode.left;
newNode.left = node;
node.height = max(node.left,node.right) + 1;
newNode.height = max(newNode.left, newNode.right) + 1;
return newNode;
}
public Node AVLINSERT(int data, Node root){
if (root == null){
return new Node(data);
}
else if (data > root.data){
root.left = AVLINSERT(data, root.left);
}
else if (data < root.data){
root.right = AVLINSERT(data, root.right);
}
int balance = height(root.left) - height(root.right);
if (balance > 1){
if (height(root.left.left) > height(root.left.right)){
return rotateRight(root);
}
else {
root.left = rotateleft(root.left);
return rotateRight(root);
}
}
if (balance < -1){
if (height(root.right.right) > height(root.right.left)){
return rotateleft(root);
}
else
root.right = rotateRight(root);
return rotateleft(root);
}
root.height = 1 + max(root.left, root.right);
return root;
}
public void inorderPrinting(Node root){
inorderPrinting(root.left);
System.out.println(root.data);
inorderPrinting(root.right);
}
public void callingAVLInserting(int data){
AVLINSERT(data,root);
}
public void callinInorderPrinting(){
inorderPrinting(root);
}
}
Just by looking at your code, you have initialised the root to null however, you do not have any constructor that initializes it.
So try add something of the sort.
public class AVLTreeMethods {
public Node root = null;
//add the following
public AVLTreeMethods() {
//initialize your root appropriately e.g.
this.root = new Node(//pass in some data e.g 0)
}
...rest of your code
}
I have written the following pseudocode for a removeNode() method while working with BST's:
If left is null
Replace n with n.right
Else if n.right is null
Replace n with n.left
Else
Find Predecessor of n
Copy data from predecessor to n
Recursively delete predecessor*
Not only do I want this method to delete or remove Nodes, but I also want it to return true if the deletion is successful.
This is what I have written so far, and I was wondering if anyone would have feedback, suggested changes, or tips to help me complete the method. I will also attach my whole program below this method.
private void removeNode(Node<E> n) {
if (n.left == null) {
replace(n, n.right);
} else if (n.right == null) {
replace(n, n.left);
} else {
//How do I find pred of n
//Copy data from pred to n
//Recursively delete pred
}
}
Here is the rest of my code:
import java.util.Random;
public class BinarySearchTree<E extends Comparable<? super E>> extends BinaryTree<E> {
public boolean contains(E item) {
return findNode(item, root) != null;
}
private Node<E> findNode(E item, Node<E> n) {
if (n == null || item == null) return null;
int result = item.compareTo(n.data);
if (result == 0) {
return n;
} else if (result > 0) {
return findNode(item, n.right);
} else {
return findNode(item, n.left);
}
}
public E max() {
Node<E> m = maxNode(root);
return (m != null) ? m.data : null;
}
private Node<E> maxNode(Node<E> n) {
if (n == null) return null;
if (n.right == null) return n;
return maxNode(n.right);
}
public E min() {
Node<E> m = minNode(root);
return (m != null) ? m.data : null;
}
private Node<E> minNode(Node<E> n) {
if (n == null) return null;
if (n.left == null) return n;
return minNode(n.left);
}
public E pred(E item) {
Node<E> n = findNode(item, root);
if (n == null) return null;
Node<E> pred = predNode(n);
return (pred != null) ? pred.data : null;
}
private Node<E> predNode(Node<E> n) {
assert n != null;
if (n.left != null) return maxNode(n.left);
Node<E> p = n.parent;
while (p != null && p.left == n) {
n = p;
p = p.parent;
}
return p;
}
public E succ(E item) {
Node<E> n = findNode(item, root);
if (n == null) return null;
Node<E> succ = succNode(n);
return (succ != null) ? succ.data : null;
}
private Node<E> succNode(Node<E> n) {
assert n != null;
if (n.right != null) return minNode(n.right);
Node<E> p = n.parent;
while (p != null && p.right == n) {
n = p;
p = p.parent;
}
return p;
}
public void add(E item) {
if (item == null) return;
if (root == null) {
root = new Node<>(item, null);
} else {
addNode(item, root);
}
}
private void addNode(E item, Node<E> n) {
assert item != null && n != null;
int result = item.compareTo(n.data);
if (result < 0) {
if (n.left == null) {
n.left = new Node<>(item, n);
} else {
addNode(item, n.left);
}
} else if (result > 0) {
if (n.right == null) {
n.right = new Node<>(item, n);
} else {
addNode(item, n.right);
}
} else {
return; // do not add duplicates
}
}
public boolean remove(E item) {
Node<E> n = findNode(item, root);
if (n == null) return false;
removeNode(n);
return true;
}
private void removeNode(Node<E> n) {
if (n.left == null) {
replace(n, n.right);
} else if (n.right == null) {
replace(n, n.left);
} else {
//How do I find pred of n
//Copy data from pred to n
//Recursively delete pred
}
}
private void replace(Node<E> n, Node<E> child) {
assert n != null;
Node<E> parent = n.parent;
if (parent == null) {
root = child;
} else if (parent.left == n) {
parent.left = child;
} else {
parent.right = child;
}
if (child != null) child.parent = parent;
}
public String toString() {
return inorder();
}
The code to remove an element is very straightforward.
Search for the node you want to remove.
Check if the node has children.
Case 1 - Has Only left child -> Replace current node with left child.
Case 2 - Has Only right child -> Replace current node with right child.
Case 3 - Has both children -> Find smallest element in right child subtree, replace current node with that node and then delete that node.
The Code can be implemented recursively as follows ->
BinarySearchTree.prototype.remove = function(data) {
var that = this;
var remove = function(node,data){
if(node.data === data){
if(!node.left && !node.right){
return null;
}
if(!node.left){
return node.right;
}
if(!node.right){
return node.left;
}
//2 children
var temp = that.findMin(node.right);
node.data = temp;
node.right = remove(node.right,temp);
}else if(data < node.data){
node.left = remove(node.left,data);
return node;
}else{
node.right = remove(node.right,data);
return node;
}
};
this.root = remove(this.root,data);
};
I'm working on a binary search tree and try to write a method for creating minimum BST from an array values. However, it's not working successfully. Where I'm making mistake ? It should print values in ascending order using inOrderTraverseTree method. I keep some additional methods and can delete if may feel irrelevant.
I updated the code in question, but, I still need to get the root Node to call the inOrderTraversal (Node root) method.
BinaryTree.java
class Node {
int key;
Node leftChild;
Node rightChild;
Node(int key) {
this.key = key;
}
Node (){}
public String toString() {
return "\n"+key+" ";
}
}
public class BinaryTree {
Node root;
BinaryTree (){
root = null;
}
public void addNode(int key) {
Node newNode = new Node(key);
// If there is no root this becomes root
if (root == null) {
root = newNode;
}
else {
// Set root as the Node we will start
// with as we traverse the tree
Node focusNode = root;
Node parent;
while (true) {
parent = focusNode;
if (key < focusNode.key) {
focusNode = focusNode.leftChild;
if (focusNode == null) {
parent.leftChild = newNode;
return; // All Done
}
} // end of if
else {
focusNode = focusNode.rightChild;
if (focusNode == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
// get the height of binary tree
public int height(Node root) {
if (root == null)
return -1;
Node focusNode = root;
int leftHeight = focusNode.leftChild != null ? height( focusNode.leftChild) : 0;
int rightHeight = focusNode.rightChild != null ? height( focusNode.rightChild) : 0;
return 1 + Math.max(leftHeight, rightHeight);
}
// METHODS FOR THE TREE TRAVERSAL
// inOrderTraverseTree : i) X.left ii) X iii) X.right
public void inOrderTraverseTree(Node focusNode) {
if (focusNode != null) {
inOrderTraverseTree(focusNode.leftChild);
// System.out.println(focusNode);
System.out.print( focusNode );
inOrderTraverseTree(focusNode.rightChild);
}
// System.out.println();
}
// preOrderTraverseTree : i) X ii) X.left iii) X.right
public void preorderTraverseTree(Node focusNode) {
if (focusNode != null) {
System.out.println(focusNode);
preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);
}
}
// postOrderTraverseTree : i) X.left ii) X.right iii) X
public void postOrderTraverseTree(Node focusNode) {
if (focusNode != null) {
preorderTraverseTree(focusNode.leftChild);
preorderTraverseTree(focusNode.rightChild);
System.out.println(focusNode);
}
}
// END
public Node findNode(int key) {
Node focusNode = root;
while (focusNode.key != key) {
if (key < focusNode.key) {
focusNode = focusNode.leftChild;
} else {
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return null;
}
return focusNode;
}
public boolean remove(int key) {
Node focusNode = root;
Node parent = root;
boolean isItALeftChild = true;
// we will remove the focusNode
while (focusNode.key != key) {
parent = focusNode;
if (key < focusNode.key) {
isItALeftChild = true;
focusNode = focusNode.leftChild;
}
else {
isItALeftChild = false;
focusNode = focusNode.rightChild;
}
if (focusNode == null)
return false;
}
// no child
if (focusNode.leftChild == null && focusNode.rightChild == null) {
if (focusNode == root)
root = null;
else if (isItALeftChild)
parent.leftChild = null;
else
parent.rightChild = null;
}
// one child ( left child )
else if (focusNode.rightChild == null) {
if (focusNode == root)
root = focusNode.leftChild;
else if (isItALeftChild)
parent.leftChild = focusNode.leftChild;
else
parent.rightChild = focusNode.leftChild;
}
else if (focusNode.leftChild == null) {
if (focusNode == root)
root = focusNode.rightChild;
else if (isItALeftChild)
parent.leftChild = focusNode.rightChild;
else
parent.rightChild = focusNode.rightChild;
}
// two children exits
else {
// replacement is the smallest node in the right subtree
// we neeed to delete the focusNode
Node replacement = getReplacementNode(focusNode);
if (focusNode == root)
root = replacement;
else if (isItALeftChild)
parent.leftChild = replacement;
else
parent.rightChild = replacement;
replacement.leftChild = focusNode.leftChild;
}
return true;
}
public Node getReplacementNode(Node replacedNode) {
Node replacementParent = replacedNode;
Node replacement = replacedNode;
Node focusNode = replacedNode.rightChild;
// find the smallest node of the right subtree of the node to be deleted
while (focusNode != null) {
replacementParent = replacement;
replacement = focusNode;
focusNode = focusNode.leftChild;
}
// exit when the focusNode is null
// the replacement is the smallest of the right subtree
if (replacement != replacedNode.rightChild) {
replacementParent.leftChild = replacement.rightChild;
replacement.rightChild = replacedNode.rightChild;
}
return replacement;
}
private void createMinimalBST(int arr[], int start, int end, Node newNode){
if ( end <= start )
return;
int mid = (start + end) / 2;
newNode.key = arr[mid];
if ( root == null ){
root = newNode;
}
System.out.println("new node = "+ newNode );
if (start <= mid-1) {
newNode.leftChild = new Node();
createMinimalBST(arr, start, mid - 1, newNode.leftChild);
}
if (mid+1 <= end) {
newNode.rightChild = new Node();
createMinimalBST(arr, mid + 1, end, newNode.rightChild);
}
// System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}
public void createMinimalBST(int array[]) {
Node n = new Node();
createMinimalBST(array, 0, array.length - 1, n);
}
public static void main(String[] args) {
int[] myArr = { 1,2,3,4,5,6,7,8,9 }; // sortedArrayToBST
BinaryTree myTr = new BinaryTree();
// Node n = BinaryTree.createMinimalBST(myArr);
myTr.createMinimalBST(myArr);
// System.out.println("The root is = "+myTr.root);
// myTr.inOrderTraverseTree(myTr.root);
System.out.println();
myTr.inOrderTraverseTree(myTr.root);
}
}
You need to assign the result of createMinimalBST() to a variable.
This method returns a Node:
public Node createMinimalBST(int array[]) {...}
However, in the main, ...
myTr.createMinimalBST(myArr);
you calls to the method but no variable try to hold the result.
Also, You may want to make createMinialBST to be public static and call it like this:
myTr = BinaryTree.createMinimalBST(myArr);
Beside, there are 2 more ways to make this work:
1) move createMinimalBST() into Node, so that the recursion can occur while the key will be setting in-place.
private void createMinimalBST(int arr[], int start, int end){
int mid = (start + end) / 2;
this.key = arr[mid];
System.out.println("new node = "+n);
if (start <= mid-1) {
this.leftChild = new Node();
this.leftChild.createMinimalBST(arr, start, mid - 1);
}
if (mid+1 <= end) {
this.rightChild = new Node();
this.rightChild.createMinimalBST(arr, mid + 1, end);
}
System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}
2) If you want that method stays in BinaryTree, you can consider to pass in the Node as a parameter into createMinimalBST() ilke createMinimalBST(arr, node);
private void createMinimalBST(int arr[], int start, int end, Node newNode){
int mid = (start + end) / 2;
newNode.key = arr[mid];
System.out.println("new node = "+n);
if (start <= mid-1) {
newNode.leftChild = new Node();
createMinimalBST(arr, start, mid - 1, newNode.leftChild);
}
if (mid+1 <= end) {
newNode.rightChild = new Node();
createMinimalBST(arr, mid + 1, end, newNode.leftChild);
}
System.out.println("left child = "+ newNode.leftChild +" "+ " right child = "+ newNode.rightChild);
}
I am posting new question with my code. I am trying to count the non-leaf nodes of a binary search tree. I am creating the non-leaf method and then I am trying to call it in a test class. Can someone help me? Here is the code:
public class BST {
private Node<Integer> root;
public BST() {
root = null;
}
public boolean insert(Integer i) {
Node<Integer> parent = root, child = root;
boolean gLeft = false;
while (child != null && i.compareTo(child.data) != 0) {
parent = child;
if (i.compareTo(child.data) < 0) {
child = child.left;
gLeft = true;
} else {
child = child.right;
gLeft = false;
}
}
if (child != null)
return false;
else {
Node<Integer> leaf = new Node<Integer>(i);
if (parent == null)
root = leaf;
else if (gLeft)
parent.left = leaf;
else
parent.right = leaf;
return true;
}
}
public boolean find(Integer i) {
Node<Integer> n = root;
boolean found = false;
while (n != null && !found) {
int comp = i.compareTo(n.data);
if (comp == 0)
found = true;
else if (comp < 0)
n = n.left;
else
n = n.right;
}
return found;
}
public int nonleaf() {
int count = 0;
Node<Integer> parent = root;
if (parent == null)
return 0;
if (parent.left == null && parent.right == null)
return 1;
}
}
class Node<T> {
T data;
Node<T> left, right;
Node(T o) {
data = o;
left = right = null;
}
}
If you are interested in only count of non-leaf node then you can traverse tree once and maintain one count. whenever you encounter a node such that it has either left or right node increment count.
You can use the following function to count number of non-leaf nodes of binary tree.
int countNonLeafNodes(Node root)
{
if (root == null || (root.left == null &&
root.right == null))
return 0;
return 1 + countNonLeafNodes(root.left) +
countNonLeafNodes(root.right);
}
I'm trying to search for a node in a binary tree and return in case it's there, otherwise, return null. By the way, the node class has a method name() that return a string with it's name...What I have so far is:
private Node search(String name, Node node){
if(node != null){
if(node.name().equals(name)){
return node;
}
else{
search(name, node.left);
search(name, node.right);
}
}
return null;
}
Is this correct??
You need to make sure your recursive calls to search return if the result isn't null.
Something like this should work...
private Node search(String name, Node node){
if(node != null){
if(node.name().equals(name)){
return node;
} else {
Node foundNode = search(name, node.left);
if(foundNode == null) {
foundNode = search(name, node.right);
}
return foundNode;
}
} else {
return null;
}
}
public Node findNode(Node root, Node nodeToFind) {
Node foundNode = null;
Node traversingNode = root;
if (traversingNode.data == nodeToFind.data) {
foundNode = traversingNode;
return foundNode;
}
if (nodeToFind.data < traversingNode.data
&& null != traversingNode.leftChild) {
findNode(traversingNode.leftChild, nodeToFind);
} else if (nodeToFind.data > traversingNode.data
&& null != traversingNode.rightChild) {
findNode(traversingNode, nodeToFind);
}
return foundNode;
}
Since language doesn't matter much for this question, here's what it looks in C# with pre-order traversal:
public static Node FindNode(Node n, int nodeValue)
{
if (n == null) return null;
if (n.Value == nodeValue) return n;
return FindNode(n.Left, nodeValue) ?? FindNode(n.Right, nodeValue);
}
you should return something if it is found in node.left or node.right
so the else block should be something like that:
else{
Node temp = search(name, node.left);
if (temp != null) return temp;
temp = search(name, node.right);
if (temp != null) return temp;
}
you don't do anything with the result of the recursive calls
Node res = search(name, node.left);
if(res!=null)return res;
res = search(name, node.right);
if(res!=null)return res;
This might be better:
if(node != null){
if(node.name().equals(name)){
return node;
}
else {
Node tmp = search(name, node.left);
if (tmp != null) { // if we find it at left
return tmp; // we return it
}
// else we return the result of the search in the right node
return search(name, node.right);
}
}
return null;
Boolean FindInBinaryTreeWithRecursion(TreeNode root, int data)
{
Boolean temp;
// base case == emptytree
if (root == null) return false;
else {
if (data == root.getData()) return true;
else { // otherwise recur down tree
temp = FindInBinaryTreeWithRecursion(root.getLeft(), data);
if (temp != true)
return temp;
else
return (FindInBinaryTreeWithRecursion(root.getRight(), data));
}
}
}
public static TreeNode findNodeInTree(TreeNode root, TreeNode nodeToFind) {
if (root == null) {
return null;
}
if (root.data == nodeToFind.data) {
return root;
}
TreeNode found = null;
if (root.left != null) {
found = findNodeInTree(root.left, nodeToFind);
if (found != null) {
return found;
}
}
if (root.right != null) {
found = findNodeInTree(root.right, nodeToFind);
if (found != null) {
return found;
}
}
return null;
}
Actually, try to avoid recursivity.
In case you have big tree structure you will get stack overflow error.
Instead of this you can use a list:
private Node search(String name, Node node){
List<Node> l = new ArrayList<Node>();
l.add(node);
While(!l.isEmpty()){
if (l.get(0).name().equals(name))
return l.get(0);
else {
l.add(l.get(0).left);
l.add(l.get(0).right);
l.remove(0);
}
}
return null;
}
public static boolean findElement(Node root, int value) {
if (root != null) {
if (root.data == value) {
return true;
} else {
return findElement(root.left, value) || findElement(root.right, value);
}
}
return false;
}
public TreeNode searchBST(TreeNode root, int val) {
if(root==null || root.val==val) return root;
TreeNode found = (val < root.val) ? searchBST(root.left,val) : searchBST(root.right,val);
return found;
}
View Code on GitHub
private TreeNode findX(TreeNode root, int x) {
if(root == null) return null;
if(root.val == x) return root;
TreeNode left = findX(root.left,x);
TreeNode right = findX(root.right,x);
if(left == null) return right;
return left;
}
Node* search(Node* root,int key){
// If key is found or is NULL
if (root == NULL || root->key == key)
return root;
if (root->key < key)
return search(root->right, key);
return search(root->left, key);
}
For C++ guys:
//search in a binary tree | O(n)
TreeNode* searchBST(TreeNode* root, int val) {
if(!root) return root;
if(root->val == val) return root;
TreeNode* temp = searchBST(root->left, val);
if(!temp){
temp = searchBST(root->right, val);
}
return temp;
}
//search in a BST | O(logn)
TreeNode* searchBST(TreeNode* root, int val) {
if(!root) return root;
if(root->val == val) return root;
if(val < root->val) return searchBST(root->left, val);
return searchBST(root->right, val);
}