
Member • Dec 23, 2008
Binary tree searching
(code is either in java or c++ better in java)😛
Member • Dec 23, 2008
Administrator • Dec 23, 2008
Friend, you want everything ready made? How about sharing your efforts with us? We'd love to help you with the problems you are facing while writing the code than providing you with all the code ready made.rama_krish627Hi can any one give me code for searching a key node in a binary tree.
(code is either in java or c++ better in java)😛
Member • Dec 24, 2008
Member • Dec 25, 2008
Member • Dec 25, 2008
/* * BinaryTreeSearch.java * * Created on December 26, 2008, 1:41 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package myjava; /** * * @author shalinig */ public class BinaryTreeSearch { /** Creates a new instance of BinaryTreeSearch */ public BinaryTreeSearch() { } public static void main(String[] args) { //Add your code for creating abinary tree. //call searchInTree( BinaryTreeNode node, String item ) for searching } static boolean searchInTree( BinaryTreeNode node, String item ) { // Return true if item is one of the items in the binary // sort tree to which node points. Return false if not. if ( node == null ) { // Tree is empty, so it certainly doesn't contain item. return false; } else if ( item.equals(node.item) ) { // Yes, the item has been found in the root node. return true; } else if ( item.compareTo(node.item) < 0 ) { // If the item occurs, it must be in the left subtree. // So, return the result of searching the left subtree. return searchInTree( node.left, item ); } else { // If the item occurs, it must be in the right subtree. // So, return the result of searching the right subtree. return searchInTree( node.right, item ); } } // end treeContains() class BinaryTreeNode { // An object of type BinaryTreeNode represents one node // in a binary tree of strings. String item; // The data in this node. BinaryTreeNode left; // Pointer to left subtree. BinaryTreeNode right; // Pointer to right subtree. BinaryTreeNode(String str) { // Constructor. Make a node containing str. item = str; } } // end class BinaryTreeNode }Hope this may be of some help !! 😀
Member • Dec 26, 2008
Hi Shalini. I think this code work for the Binary Search tree why because you compared the items in the tree whether they are less than or greater than a particular node.shalini_goel14Hi rama_krish,
I feel the problem in above code is using following logic:
if(root.lc!=null)
call function again
else
return null
Here you should check right sub tree,instead of returning null, so every time after function do not find any item in left sub tree,it returns null without checking in right subtree.
Why don't you try something like below:
/* * BinaryTreeSearch.java * * Created on December 26, 2008, 1:41 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package myjava; /** * * @author shalinig */ public class BinaryTreeSearch { /** Creates a new instance of BinaryTreeSearch */ public BinaryTreeSearch() { } public static void main(String[] args) { //Add your code for creating abinary tree. //call searchInTree( BinaryTreeNode node, String item ) for searching } static boolean searchInTree( BinaryTreeNode node, String item ) { // Return true if item is one of the items in the binary // sort tree to which node points. Return false if not. if ( node == null ) { // Tree is empty, so it certainly doesn't contain item. return false; } else if ( item.equals(node.item) ) { // Yes, the item has been found in the root node. return true; } else if ( item.compareTo(node.item) < 0 ) { // If the item occurs, it must be in the left subtree. // So, return the result of searching the left subtree. return searchInTree( node.left, item ); } else { // If the item occurs, it must be in the right subtree. // So, return the result of searching the right subtree. return searchInTree( node.right, item ); } } // end treeContains() class BinaryTreeNode { // An object of type BinaryTreeNode represents one node // in a binary tree of strings. String item; // The data in this node. BinaryTreeNode left; // Pointer to left subtree. BinaryTreeNode right; // Pointer to right subtree. BinaryTreeNode(String str) { // Constructor. Make a node containing str. item = str; } } // end class BinaryTreeNode }Hope this may be of some help !! 😀
Member • Dec 26, 2008
Member • Dec 29, 2008
bt* delnode(bt *t) { bt *ptr,*prev,*temp; int n; ptr=t; printf("\nENTER THE NODE TO BE DELETED : "); scanf("%d",&n); while(ptr!=NULL && ptr->data!=n) { prev=ptr; if(n<ptr->data) ptr=ptr->lc; else ptr=ptr->rc; } if(ptr->data!=n) { printf("\nNODE NOT FOUND "); return(t); } if(ptr->lc==NULL && ptr->rc==NULL) { if(n<prev->data) prev->lc=NULL; else prev->rc=NULL; } else if(ptr->lc==NULL && ptr->rc!=NULL) { if(n<prev->data) prev->lc=ptr->rc; else prev->rc=ptr->rc; } else if(ptr->lc!=NULL && ptr->rc==NULL) { if(n<prev->data) prev->lc=ptr->lc; else prev->rc=ptr->lc; } else { temp=ptr->rc; while(temp->lc!=NULL) temp=temp->lc; temp->lc=ptr->lc; if(ptr==t) t=ptr->rc; else { if(n<prev->data) prev->lc=ptr->rc; else prev->rc=ptr->rc; } } free(ptr); return(t); } */ bt* delNode(bt* root) { bt*p,*q,*f,*rp,*s; int data; printf("\nEnter data to be search"); scanf("%d",&data); p=root; q=NULL; while(p!=NULL && p->data!=data) { q=p; if(data<p->data) p=p->lc; else p=p->rc; } if(p==NULL) { printf("\nData not Present in Tree..."); return root; } if(p->lc==NULL) rp=p->rc; if(p->rc==NULL) rp=p->lc; if(p->lc && p->rc) { f=p; rp=p->rc; s=rp->lc; while(s) { f=rp; rp=s; s=s->lc; } if(f!=p) { f->lc=rp->rc; rp->rc=p->rc; } rp->lc=p->lc; } if(q==NULL) root=rp; else { if(p==q->lc) q->lc=rp; else q->rc=rp; } return root; }
Member • Dec 31, 2008
Member • Jan 4, 2009