Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved BST.java #13

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions src/com/ds/LinkedListU.java
Original file line number Diff line number Diff line change
@@ -3,26 +3,29 @@
public class LinkedListU<T> {
Node head;
public LinkedListU() {
head= null;
head = null;
}
public void add(Object value){
Node newNode=new Node(value, null);
if(head==null)
head=newNode;
else{
// function to add value of type object and its children in linked list.
public void add(Object value) {
Node newNode = new Node(value, null);
if(head == null)
head = newNode;
else {
newNode.next=head;
head= newNode;
head = newNode;
}
}
// function to delete node of linked list.
public void delete(){
head=head.next;
head = head.next;

}
// function to display elements of the linked list.
public void display(){
Node n=head;
while(n!=null){
Node n = head;
while(n != null){
System.out.println((T)n.value);
n=n.next;
n = n.next;
}
}

43 changes: 22 additions & 21 deletions src/com/tree/BST.java
Original file line number Diff line number Diff line change
@@ -3,44 +3,45 @@
public class BST {
Node root;
public BST() {
root=null;
root = null;
}

public Node NodeCreate(int value){
// this function returns the new node by settingvalue to value and child to be null.
public Node NodeCreate(int value) {
return new Node(value, null, null);
}
public void add(Node start, Node newNode){
if(root==null){
root=newNode;
// function to add the elements in BST.
public void add(Node start, Node newNode) {
if(root == null){
root = newNode;
return;
}
if(newNode.value> start.value){
if( start.right==null)
start.right=newNode;
add(start.right,newNode);
if(newNode.value > start.value) {
if( start.right == null)
start.right = newNode;
add(start.right, newNode);
}
if(newNode.value< start.value){
if( start.left==null)
start.left=newNode;
add(start.left,newNode);
if(newNode.value < start.value) {
if( start.left == null)
start.left = newNode;
add(start.left, newNode);
}
}

public void Search(int value, Node start){
// function to search the given value using recursion.
public void Search(int value, Node start) {

if(start==null){
System.out.println("node isnot found");
if(start == null) {
System.out.println("node is not found");
return;
}
if(start.value==value)
if(start.value == value)
{
System.out.println("node is found");
return;
}
if( value>start.value){
if( value > start.value) {
Search(value, start.right);
}
if( value<start.value){
if( value < start.value) {
Search(value, start.left);
}
}