Access Millions of academic & study documents

About Tree structures Solution public class BinarySearchTree

Content type
User Generated
Showing Page:
1/3
about Tree structures..
Solution
public class BinarySearchTree { public static Node
root; public BinarySearchTree(){ this.root = null; }
public boolean find(int id){ Node current = root;
while(current!=null){ if(current.data==id){ return true;
}else if(current.data>id){ current = current.left; }else{
current = current.right; } } return false; }
public boolean delete(int id){ Node parent = root;
Node current = root; boolean isLeftChild = false;
while(current.data!=id){ parent = current;
if(current.data>id){ isLeftChild = true; current =
current.left; }else{ isLeftChild = false; current =
current.right; } if(current ==null){ return false; }
} //if i am here that means we have found the node
//Case 1: if node to be deleted has no children
if(current.left==null && current.right==null){
if(current==root){ root = null; } if(isLeftChild
==true){ parent.left = null; }else{ parent.right =
null; } } //Case 2 : if node to be deleted has only
one child else if(current.right==null){

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/3
if(current==root){ root = current.left; }else
if(isLeftChild){ parent.left = current.left; }else{
parent.right = current.left; } } else
if(current.left==null){ if(current==root){ root =
current.right; }else if(isLeftChild){ parent.left =
current.right; }else{ parent.right = current.right; }
}else if(current.left!=null && current.right!=null){
//now we have found the minimum element in the right sub
tree Node successor = getSuccessor(current);
if(current==root){ root = successor; }else
if(isLeftChild){ parent.left = successor; }else{
parent.right = successor; } successor.left =
current.left; } return true; } public Node
getSuccessor(Node deleleNode){ Node successsor
=null; Node successsorParent =null; Node current =
deleleNode.right; while(current!=null){
successsorParent = successsor; successsor = current;
current = current.left; } //check if successor has the
right child, it cannot have left child for sure // if it does
have the right child, add it to the left of successorParent.
// successsorParent if(successsor!=deleleNode.right){
successsorParent.left = successsor.right;
successsor.right = deleleNode.right; } return
successsor; } public void insert(int id){ Node
newNode = new Node(id); if(root==null){ root =

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/3

Sign up to view the full document!

lock_open Sign Up
Unformatted Attachment Preview
about Tree structures.. Solution public class BinarySearchTree { root; public BinarySearchTree(){ public boolean find(int id){ while(current!=null){ current = current.right; Node current = root; if(current.data>id){ current = current.left; } } return false; }else{ } Node parent = root; parent = current; isLeftChild = true; }else{ } return true ; boolean isLeftChild = false; while(current.data!=id){ current.right; current = isLeftChild = false; if(current ==null){ current = return false; //if i am here that means we have found the node //Case 1: if node to be deleted has no children if(current.left==null && current.right==null){ if(current==root){ root = null; ==true){ parent.left = null; null; } } one child } Node current = root; public boolean delete(int id){ current.left; this.root = null; if(current.data==id){ }else if(current.data>id){ } publ ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4