How to count non-leaf nodes in a binary search tree? - java

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);
}

Related

Counting the no. of nodes touched during deletion in a bst

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.

How to write code for Binary Search Tree Deletion?

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);
};

finding a node location within a BST and adding it to the tree recursively

So i have 3 methods 1 that adds a node to the binary tree using the traverseAdd method, and another method which finds the location of where a value would be placed within the tree based on its parent node. I would like to eliminate the traverseAdd method and use the findLocation method within the add method to add the new value to the BST.
public void add(int val) {
/*Adds a new node to the binary tree after traversing the tree and figuring out where it belongs*/
Node nodeObjToAdd = new Node(val);
if(root == null){
//if node root is not null root = new node value
root = nodeObjToAdd;
}
Node nodeTraversed = root;
traverseAdd(nodeTraversed, nodeObjToAdd);
}
private void traverseAdd(Node node, Node nodeObjToAdd){
/*Traverses tree and finds a place to add the node to be added by comparing values of the left child and right child of the
* focus node*/
if(nodeObjToAdd.value < node.value){
if(node.leftChild == null){
node.leftChild = nodeObjToAdd;
}
else {
//if the val < the root.value set int he constructor
traverseAdd(node.leftChild, nodeObjToAdd);
}
}
else if(nodeObjToAdd.value > node.value) {
if (node.rightChild == null) {
node.rightChild = nodeObjToAdd;
} else {
traverseAdd(node.rightChild, nodeObjToAdd);
}
}
}
public Node findNodeLocation(Node rootNode, int val) {
/*returns where a the Node after which the value would be added.*/
if(val < rootNode.value && rootNode.leftChild != null){
return rootNode.leftChild;
}
if(val >= rootNode.value && rootNode.rightChild != null){
return rootNode.rightChild;
}
else
return this.root;
}
public void add(int val) {
if (root == null) {
root = new Node(val);
}
Node cur = root;
Node next = null;
while (true) {
next = findNodeLocation(cur, val);
if (next != cur) {
cur = next;
} else {
break;
}
}
if (val < cur.value) {
cur.leftChild = new Node(val);
} else {
cur.rightChild = new Node(val);
}
}
I think this should work

Implementing nodeCount() and leafCount() in a binary search tree - java

I am trying to implement leafCount() and nodeCount() to this recursive binary tree - program.
When testing it, these two methods (or the tests of them) throw AssertionError, so obviously they're not working as expected. I cannot figure out where I'm doing or thinking wrong. If someone could explain what I'm doing wrong or pinpoint the problem, I would be very grateful.
public class BSTrec {
BSTNode tree, parent, curr;
public BSTrec () {
tree = null; // the root of the tree
parent = null; // keeps track of the parent of the current node
curr = null; // help pointer to find a node or its place in the tree
}
public boolean isEmpty() {
return tree == null;
}
private boolean findNodeRec(String searchKey, BSTNode subtree, BSTNode subparent) {
if (subtree == null) { // base case 1: node not found
curr = null;
parent = subparent; // the logical parent for the value
return false;
}
else {
if (subtree.info.key.equals(searchKey)) {
curr = subtree; // update current to point to the node
parent = subparent; // update parent to point to its parent
return true;
}
else {
if (searchKey.compareTo(subtree.info.key) < 0) {
return findNodeRec(searchKey, subtree.left, subtree);
}
else {
return findNodeRec(searchKey, subtree.right, subtree);
}
}
}
}
public NodeInfo retrieveNode(String searchKey) {
if (findNodeRec(searchKey, tree, null)) return curr.info;
else return null;
}
public void addRec(String keyIn, BSTNode subtree, BSTNode subparent, boolean goLeft) {
if (tree == null) { // a first node will be the new root: base case 1
tree = new BSTNode(new NodeInfo(keyIn));
curr = tree;
parent = null;
}
else { // insertion in an existing tree
if (subtree == null) {
if (goLeft) {
subparent.left = new BSTNode(new NodeInfo(keyIn));
curr = subparent.left;
parent = subparent;
}
else { // the new node is to be a left child
subparent.right = new BSTNode(new NodeInfo(keyIn));
curr = subparent.right;
parent = subparent;
}
}
else {
if (keyIn.compareTo(subtree.info.key) < 0) {
addRec(keyIn, subtree.left, subtree, true);
}
else {
addRec(keyIn, subtree.right, subtree, false);
}
}
}
}
public void deleteNode(String searchKey) {
boolean found = findNodeRec(searchKey, tree, null);
if (!found) // the key is not in the tree
System.out.println("The key is not in the tree!");
else {
if ((curr.left == null) && (curr.right == null))
if (parent == null)
tree = null;
else
if (curr == parent.left) // delete a left child
parent.left = null;
else // delete a right child
parent.right = null;
else // delete a node with children, one or two
if ((curr.left != null) && (curr.right != null)) { // two children
BSTNode surrogateParent = curr;
BSTNode replacement = curr.left;
while (replacement.right != null) {
surrogateParent = replacement;
replacement = replacement.right;
}
curr.info = replacement.info; // the information is copied over
if (curr == surrogateParent) {
curr.left = replacement.left; // curr "adopts" the left
replacement = null;
}
else {
surrogateParent.right = replacement.left;
replacement = null;
}
} // End: if two children
else { // delete a node with one child
if (parent == null)
if (curr.left != null)
tree = curr.left;
else
tree = curr.right;
else
if (curr == parent.left)
if (curr.right == null)
parent.left = curr.left;
else
parent.left = curr.right;
else
if (curr.right == null)
parent.right = curr.left;
else
parent.right = curr.right;
}
curr = null;
}
}
public void inOrder(BSTNode root) {
if (root != null) {
inOrder(root.left); // process the left subtree
System.out.println(root.info.key); // process the node itself
inOrder(root.right); // process the right subtree
}
}
public void preOrder(BSTNode root) {
if (root != null) { // implicit base case: empty tree: do nothing
System.out.println(root.info.key); // process the node itself
preOrder(root.left); // process the left subtree
preOrder(root.right); // process the right subtree
}
}
public void postOrder(BSTNode root) {
if (root != null) { // implicit base case: empty tree: do nothing
postOrder(root.left); // process the left subtree
postOrder(root.right); // process the right subtree
System.out.println(root.info.key); // process the node itself
}
}
public int nodeCount() {
int count = 0;
if (tree == null) {
count = 0;
//throw new NullPointerException();
}
else {
if (tree.left != null) {
count = 1;
count += tree.left.nodeCount();
}
if (tree.right != null) {
count = 1;
count += tree.right.nodeCount();
}
}
return count;
}
public int leafCount() {
int count = 0;
if (tree == null) {
return 0;
}
if (tree != null && tree.left == null && tree.right==null) {
return 1;
}
else {
count += tree.left.leafCount();
count += tree.right.leafCount();
}
return count;
}
private class BSTNode {
NodeInfo info;
BSTNode left, right;
BSTNode() {
info = null;
left = null;
right = null;
}
public int leafCount() {
// TODO Auto-generated method stub
return 0;
}
public int nodeCount() {
// TODO Auto-generated method stub
return 0;
}
BSTNode(NodeInfo dataIn) {
info = dataIn;
left = null;
right = null;
}
BSTNode(NodeInfo dataIn, BSTNode l, BSTNode r) {
info = dataIn;
left = l;
right = r;
}
}
}
public class NodeInfo {
String key; // add other fields as needed!
NodeInfo() {
key = null;
}
NodeInfo(String keyIn) {
key = keyIn;
}
}
Your nodeCount logic has an error :
if (tree.left != null) {
count = 1;
count += tree.left.nodeCount();
}
if (tree.right != null) {
count = 1; // here you initialize the count, losing the count of the left sub-tree
count += tree.right.nodeCount();
}
Change to
if (tree.left != null || tree.right != null) {
count = 1;
if (tree.left != null) {
count += tree.left.nodeCount();
}
if (tree.right != null) {
count += tree.right.nodeCount();
}
}
In your leafCount you are missing some null checks, since it's possible one of the children is null :
if (tree != null && tree.left == null && tree.right==null) {
return 1;
} else {
if (tree.left != null) // added check
count += tree.left.leafCount();
if (tree.right != null) // added check
count += tree.right.leafCount();
}
In here:
public int leafCount() {
int count = 0;
if (tree == null) {
return 0;
}
if (tree != null && tree.left == null && tree.right==null) {
return 1;
}
else {
count += tree.left.leafCount();
count += tree.right.leafCount();
}
return count;
}
you're not allowing for the possibility that tree.left is non-null but tree.right is null. In that case, you'll try to execute
count += tree.right.leafCount();
which will throw a NullPointerException.
I think you should also rethink what you're doing with your instance fields curr and parent. These really should be local variables in whatever method you need to use them in. A tree doesn't have a "current" node in any meaningful sense.

Recursive Binary Search Tree Deletion

Note: I've included the code for the insert in case that is where my error lies.
I'm having trouble removing nodes in my binary search tree. I ran this through eclipse and the node's "pointers" seem to be getting reassigned, but as soon as I exit my recursive method it goes back to the way the node was.
I may be misunderstanding how java is passing the tree nodes between methods.
public abstract class BinaryTree<E> implements Iterable<E> {
protected class Node<T> {
protected Node(T data) {
this.data = data;
}
protected T data;
protected Node<T> left;
protected Node<T> right;
}
public abstract void insert(E data);
public abstract void remove(E data);
public abstract boolean search(E data);
protected Node<E> root;
}
import java.util.Iterator;
public class BinarySearchTree<E extends Comparable<? super E>> extends BinaryTree<E> {
private Node<E> findIOP(Node<E> curr) {
curr = curr.left;
while (curr.right != null) {
curr = curr.right;
}
return curr;
}
public Iterator<E> iterator() {
return null;
}
public static void remove(E data) {
if (root != null){
if (data.compareTo(root.data) == 0) {
if (root.left == null || root.right == null) {
root = root.left != null ? root.left : root.right;
} else {
Node<E> iop = findIOP(root);
E temp = root.data;
root.data = iop.data;
iop.data = temp;
if (root.left == iop) {
root.left = root.left.left;
} else {
Node<E> curr = root.left;
while (curr.right != iop) {
curr = curr.right;
}
curr.right = curr.right.left;
}
}
} else {
if (data.compareTo(root.data) < 0) {
remove(root.left ,data);
} else {
remove(root.right ,data);
}
}
}
}
private void remove (Node<E> node, E data){
if (data.compareTo(node.data) == 0) {
if (node.left == null || node.right == null) {
if (node.left != null) {
// Problem is either here
node = node.left;
} else {
// or here
node = node.right;
}
} else {
Node<E> iop = findIOP(node);
E temp = node.data;
node.data = iop.data;
iop.data = temp;
if (node.left == iop) {
node.left = node.left.left;
} else {
Node<E> curr = node.left;
while (curr.right != iop) {
curr = curr.right;
}
curr.right = curr.right.left;
}
}
} else {
if (data.compareTo(node.data) < 0) {
remove(node.left ,data);
} else {
remove(node.right ,data);
}
}
}
}
When I insert:
tree.insert(10);
tree.insert(15);
tree.insert(6);
tree.insert(8);
tree.insert(9);
and then
tree.remove(8);
System.out.println(tree.root.left.right.data);
is still 8 instead of 9.
Removal works at the root and pointers are properly reassigned if removing from
root.left and root.right.
Any suggestion would be appreciated.
EDIT: I seem to have narrowed down the question. I implemented an iterative version where I make root = curr and change curr.left.right = curr.left.right.right. I notice that this change reflects my root node while when I pass node = root.left.right to my recursive function changing node to node.right does not reflect the changes in the root. Why is this?
Narrowed down some more. Why does node.left = node.left.left make changes to my tree and node = node.left do nothing.
I fixed it by recursively reassigning nodes of the parent as opposed to recursively reassigning the nodes in the child. This is the resulting private and public function.
public void remove(E data) {
Node<E> curr;
if (root != null) {
if (data.compareTo(root.data) == 0) {
if (root.left == null || root.right == null) {
root = root.left != null ? root.left : root.right;
}
else {
Node<E> iop = findIOP(root);
E temp = root.data;
root.data = iop.data;
iop.data = temp;
if (root.left == iop) {
root.left = root.left.left;
}
else {
curr = root.left;
while (curr.right != iop) {
curr = curr.right;
}
curr.right = curr.right.left;
}
}
} else if (data.compareTo(root.data) < 0) {
root.left = remove(data, root.left);
} else {
root.right = remove(data, root.right);
}
}
}
private Node<E> remove(E data, Node<E> node){
Node<E> curr;
if (node != null){
if (data.compareTo(node.data) == 0) {
if (node.left == null || node.right == null) {
node = node.left != null ? node.left : node.right;
return node;
} else {
Node<E> iop = findIOP(node);
E temp = node.data;
node.data = iop.data;
iop.data = temp;
if (node.left == iop) {
node.left = node.left.left;
return node;
} else {
curr = node.left;
while (curr.right != iop) {
curr = curr.right;
}
curr.right = curr.right.left;
return node;
}
}
} else {
if (data.compareTo(node.data) < 0) {
node.left = remove(data, node.left);
if (node.left != null){
return node.left;
}
} else {
node.right = remove(data, node.right);
if (node.right != null){
return node.right;
}
}
// if node.left/right not null
return node;
}
}
// if node = null;
return node;
}
You are indeed right when you say "I may be misunderstanding how java is passing the tree nodes between methods". Consider:
public class Ref {
public static void main(String args[]) {
Integer i = new Integer(4);
passByRef(i);
System.out.println(i); // Prints 4.
}
static void passByRef(Integer j) {
j = new Integer(5);
}
}
Although i is "passed by reference", the reference i itself isn't changed by the method, only the thing that j refers to. Put another way, j is initialized with a copy of the reference i, that is, j initially refers to the same object as i (but crucially is not i). Assigning j to refer to something else has no effect on what i refers to.
To achieve what you want in your search, I suggest you instead return the new reference to the caller. For example, something analogous to the following change:
public class Ref {
public static void main(String args[]) {
Integer i = new Integer(4);
i = passByRef(i);
System.out.println(i); // Prints 5.
}
static Integer passByRef(Integer j) {
j = new Integer(5);
return j;
}
}

Categories