How to write code for Binary Search Tree Deletion? - java

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

Related

My insertion sort is not giving desired result and i am not able to figure out

i was trying to perform insertion sort on a doubly linked list and before anyone suggest that i should use sorted insertion i know it can be done but it is not allowed for the work.
here is the code:
import java.util.ArrayList;
public class DLinkedList {
private class Node {
private int value;
private Node nextNode;
private Node prevNode;
public Node(int v) {
value = v;
nextNode = null;
prevNode = null;
}
public int getValue() {
return value;
}
public void setValue(int v) {
value = v;
}
public Node getNextNode() {
return nextNode;
}
public void setNextNode(Node n) {
nextNode = n;
}
public Node getPrevNode() {
return prevNode;
}
public void setPrevNode(Node n) {
prevNode = n;
}
}
// Holds a reference to the head and tail of the list
private Node headNode;
private Node tailNode;
public DLinkedList() {
headNode = null;
tailNode = null;
}
public Object getHeadValue() {
if (headNode == null)
return null;
return headNode.value;
}
public Object getTailValue() {
if (tailNode == null)
return null;
return tailNode.value;
}
public void addAtHead(int o) {
Node newNode = new Node(o);
newNode.setNextNode(headNode);
if (headNode != null)
headNode.setPrevNode(newNode);
headNode = newNode;
// special case for empty list
if (tailNode == null)
tailNode = newNode;
}
public void addAtTail(int o) {
Node newNode = new Node(o);
// this means that headNode == null too!
if (tailNode == null) {
tailNode = newNode;
headNode = newNode;
} else {
newNode.setPrevNode(tailNode);
tailNode.setNextNode(newNode);
tailNode = newNode;
}
}
public int deleteAtHead() {
// list is empty
if (headNode == null) {
headNode = null;
tailNode = null;
return -1;
}
// singleton: must update tailnode too
if (headNode == tailNode) {
int res = headNode.getValue();
headNode = null;
tailNode = null;
return res;
}
int res = headNode.getValue();
headNode = headNode.getNextNode();
headNode.setPrevNode(null);
return res;
}
public int deleteAtTail() {
// list is empty
if (tailNode == null) {
headNode = null;
tailNode = null;
return -1;
}
// singleton: must update tailnode too
if (headNode == tailNode) {
int res = tailNode.getValue();
headNode = null;
tailNode = null;
return res;
}
int res = tailNode.getValue();
tailNode = tailNode.getPrevNode();
tailNode.setNextNode(null);
return res;
}
public int delete(Node n) {
if (n == null)
return -1;
Node next = n.getNextNode();
Node prev = n.getPrevNode();
int val = n.getValue();
if (prev != null)
prev.setNextNode(next);
if (next != null)
next.setPrevNode(prev);
// deleting at the end
if (n == tailNode)
tailNode = prev;
// deleteing at beginning
if (n == headNode)
headNode = next;
return val;
}
public void insertAfter(Node n, int val) {
if (n == null) { // this is the headNode
addAtHead(val);
return;
}
Node next = n.getNextNode();
Node newNode = new Node(val);
newNode.setPrevNode(n);
newNode.setNextNode(next);
n.setNextNode(newNode);
if (next == null) { // insert at tail
tailNode = newNode;
} else {
next.setPrevNode(newNode);
}
}
// computes the size of the list
public int size() {
if (headNode == null)
return 0;
Node n = headNode;
int size = 0;
while (n != null) {
size++;
n = n.getNextNode();
}
return size;
}
// Predicate to check if the linked list is sorted
public boolean isSorted() {
if (headNode == null || headNode.nextNode == null)
return true;
Node i = headNode.nextNode;
while (i != null) {
if (i.getValue() < i.getPrevNode().getValue())
return false;
i = i.nextNode;
}
return true;
}
// toString methods to override printing of object
public String toString() {
Node n = headNode;
StringBuffer buf = new StringBuffer();
while (n != null) {
buf.append(n.getValue());
buf.append(" ");
n = n.getNetNode();
}
return buf.toString();
}
public void insertionSort(DLinkedList arr) {
Node current=arr.headNode.getNextNode();
while(current!=null)
{
Node nextNode=current.getNextNode();
Node searchNode=current.prevNode;
while(searchNode!=null && searchNode.getValue()>current.getValue())
{
searchNode=searchNode.prevNode;
}
delete(current);
if(searchNode==null) {
current.prevNode=null;
addAtHead(current.getValue());
}
else {
insertAfter(searchNode,searchNode.getValue());
}
current=nextNode;
}
}
public static void main(String[] args) {
DLinkedList d = new DLinkedList();
d.addAtHead(4);
d.addAtHead(1);
d.addAtHead(7);
d.addAtHead(10);
d.addAtHead(101);
System.out.println("Before sorting: " + d); // this will call the toString method
d.insertionSort(d);
System.out.println("After sorting: " + d);
}
}
the list i provided is 101 10 7 1 4
and the list after sorting comes 1 1 7 10 101
i have been scratching my head for days but can't figure it out
Function you all need to look at is
public void insertionSort(DLinkedList arr)

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.

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

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

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

How to search for a node in a binary tree and return it?

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

Categories