Last active
January 7, 2018 14:25
-
-
Save little-einstien/d8b8e6416b12ea801d724b7665b22a0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> | |
<output url="file://$PROJECT_DIR$/out" /> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project version="4"> | |
<component name="ProjectModuleManager"> | |
<modules> | |
<module fileurl="file://$PROJECT_DIR$/Practise.iml" filepath="$PROJECT_DIR$/Practise.iml" /> | |
</modules> | |
</component> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<module type="JAVA_MODULE" version="4"> | |
<component name="NewModuleRootManager" inherit-compiler-output="true"> | |
<exclude-output /> | |
<content url="file://$MODULE_DIR$"> | |
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | |
</content> | |
<orderEntry type="inheritedJdk" /> | |
<orderEntry type="sourceFolder" forTests="false" /> | |
</component> | |
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.reflect.Array; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class BST<T extends Comparable<T>> { | |
private Node<T> root; | |
private List<T> preorder = new ArrayList<>(), | |
postorder = new ArrayList<>(), | |
inorder = new ArrayList<>(); | |
private int height = 0; | |
private class Node<T>{ | |
private T data; | |
private Node<T> leftRef,rightRef; | |
Node(T data){ | |
this.data = data; | |
leftRef = rightRef = null; | |
height ++; | |
} | |
public T getData(){ | |
return this.data; | |
} | |
public Node<T> getRightRef(){ | |
return this.rightRef; | |
} | |
public Node<T> getLeftRef(){ | |
return this.leftRef; | |
} | |
public void setRightRef(Node<T> rightRef){ | |
this.rightRef = rightRef; | |
} | |
public void setLeftRef(Node<T> leftRef){ | |
this.leftRef = leftRef; | |
} | |
@Override | |
public String toString() { | |
return "{" + | |
"\"data\":" + data + | |
", \"leftRef\":" + leftRef + | |
", \"rightRef\":" + rightRef + | |
'}'; | |
} | |
} | |
public int getHeight(){ | |
return height; | |
} | |
@Override | |
public String toString() { | |
return "BST{" + | |
"\"root\":" + root + | |
'}'; | |
} | |
public BST(T... elements){ | |
for(int i = 0; i< elements.length; i++){ | |
T data = elements[i]; | |
insert(data); | |
} | |
} | |
public void insert(T element){ | |
if(root == null){ | |
root = new Node<T>(element); | |
}else{ | |
insert(root,element); | |
} | |
} | |
private void insert(Node<T> root, T element){ | |
if(root.getData().compareTo(element) == 0) | |
return; | |
if(root.getData().compareTo(element) > 0 ){ | |
if(root.getLeftRef() != null){ | |
insert(root.getLeftRef(),element); | |
} | |
else{ | |
root.setLeftRef(new Node<>(element)); | |
} | |
}else if(root.getData().compareTo(element) < 0){ | |
if(root.getRightRef() != null) { | |
insert(root.getRightRef(), element); | |
} | |
else { | |
root.setRightRef(new Node<>(element)); | |
} | |
} | |
} | |
public List<T> getPreorder(){ | |
// System.out.println(root); | |
preorder(root); | |
return preorder; | |
} | |
public List<T> getPostorder(){ | |
postorder(root); | |
return postorder; | |
} | |
public List<T> getInorder(){ | |
inorder(root); | |
return inorder; | |
} | |
public void preorder(Node<T> root){ | |
preorder.add(root.getData()); | |
if(root.getLeftRef() != null) { | |
preorder(root.getLeftRef()); | |
} | |
if(root.getRightRef() != null){ | |
preorder(root.getRightRef()); | |
} | |
} | |
public void postorder(Node<T> root){ | |
if(root.getLeftRef() != null) { | |
postorder(root.getLeftRef()); | |
} | |
if(root.getRightRef() != null){ | |
postorder(root.getRightRef()); | |
} | |
postorder.add(root.getData()); | |
}public void inorder(Node<T> root){ | |
if(root.getLeftRef() != null) { | |
inorder(root.getLeftRef()); | |
} | |
inorder.add(root.getData()); | |
if(root.getRightRef() != null){ | |
inorder(root.getRightRef()); | |
} | |
} | |
public void delete(T element) { | |
delete(root,element); | |
} | |
public void setRoot(Node<T> root) { | |
this.root = root; | |
} | |
public boolean search(T element){ | |
return search(root,element); | |
} | |
public boolean search(Node<T> root,T element) { | |
if (root.getData().compareTo(element) == 0) { | |
return true; | |
} | |
if (root.getLeftRef() != null) { | |
if (search(root.getLeftRef(), element)) { | |
return true; | |
} | |
} | |
if (root.getRightRef() != null) { | |
if (search(root.getRightRef(), element)) { | |
return true; | |
} | |
} | |
return false; | |
} | |
public void delete(Node<T> root , T element){ | |
if(!search(root,element)){ | |
return; | |
} | |
if(element.compareTo(root.getData()) == 0){ | |
if(root.getLeftRef() == null){ | |
root = root.getRightRef(); | |
}else if(root.rightRef == null){ | |
root = root.getLeftRef(); | |
} | |
}else if(element.compareTo(root.getData())<0){ | |
delete(root.getLeftRef(),element); | |
}else{ | |
delete(root.getRightRef(),element); | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Demo { | |
public static void main(String[] args) { | |
BST<Integer> bst = new BST<>(11, 6, 8, 19, 4, 10, 5, 17, 43, 49, 31); | |
// System.out.println(bst.getHeight()); | |
System.out.println(bst.search(31)); | |
System.out.println(bst); | |
bst.delete(8); | |
System.out.println(bst); | |
// System.out.println(bst.getPreorder()); | |
//System.out.println(bst.getPostorder()); | |
//System.out.println(bst.getInorder()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment