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

Segment Tree and Binary Search Tree Java #134

Merged
merged 3 commits into from
Oct 9, 2018
Merged
Show file tree
Hide file tree
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
123 changes: 123 additions & 0 deletions BST/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* @author Nikunj Khokhar
*/
public class BinarySearchTree {


class Node {
int key;
Node left, right;

public Node(int item) {
key = item;
left = right = null;
}
}


Node root;


BinarySearchTree() {
this.root = null;
}


void insert(int key) {
root = insertRec(root, key);
}


Node insertRec(Node root, int key) {


if (root == null) {
root = new Node(key);
return root;
}


if (key < root.key)
root.left = insertRec(root.left, key);
else if (key > root.key)
root.right = insertRec(root.right, key);


return root;
}


void inorder() {
inorderRec(root);
}


void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.println(root.key);
inorderRec(root.right);
}
}



void preorder() {
preorderRec(root);
}


void preorderRec(Node root) {
if (root != null) {
System.out.println(root.key);
preorderRec(root.left);
preorderRec(root.right);
}
}



void postorder() {
postorderRec(root);
}


void postorderRec(Node root) {
if (root != null) {

postorderRec(root.left);
postorderRec(root.right);
System.out.println(root.key);
}
}


}

public class Main {
public static void main(String args[])
{
BinarySearchTree tree = new BinarySearchTree();
tree.insert(50);
tree.insert( 30);
tree.insert( 20);
tree.insert( 40);
tree.insert( 70);
tree.insert( 60);
tree.insert(80);


System.out.println("Inorder traversal of the given tree : ");
tree.inorder();
System.out.print("\n\n");


System.out.println("Postorder traversal of the given tree : ");
tree.postorder();
System.out.print("\n\n");


System.out.println("Preorder traversal of the given tree : ");
tree.preorder();
}
}
127 changes: 127 additions & 0 deletions Segment Trees/Java/SegementTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
import java.util.Scanner;
/*
* @author Nikunj Khokhar
*/
public class SegmentTree {

static int MAX = 1000005;
static int arr[] ;
static int tree[] ;
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);

//Enter the value of the n which is the numner of element in the
System.out.println("Enter the numbers of elements in Array : ");
int n = sc.nextInt();
arr = new int[MAX];
tree = new int[MAX];



System.out.println("Enter the Array elements :");
for(int i=1;i<=n;i++)
{
arr[i] = sc.nextInt();
}



//Building the SegmentTree....
build(1,1,n);


//FInding the Minimum element between the range x to y......

System.out.println("Enter the Range :");
System.out.println("Enter the L : ");

int x = sc.nextInt();
System.out.println("Enter the R : ");
int y = sc.nextInt();

System.out.println("The Minimum element between the ranege " + x + "and "+ y +" :");
System.out.println(query(1,1,n,x,y));



//for updateing the Segment tree


System.out.println("Enter the index which you want to change :");
x = sc.nextInt();
System.out.println("Enter the number :");
y = sc.nextInt();

//Update the value at x with y....
update(1,1,n,x,y);



}


public static void build(int node, int start, int end)
{
if(start == end)
{
tree[node] = arr[start];
}
else
{
int mid = (start + end) / 2;
build(2*node, start, mid);
build(2*node+1, mid+1, end);
tree[node] = Math.min(tree[2*node],tree[2*node+1]);
}
}

public static int query(int node, int start, int end, int l, int r)
{
if(r < start || end < l)
{

return MAX;
}
if(l <= start && end <= r)
{

return tree[node];
}

int mid = (start + end) / 2;
int p1 = query(2*node, start, mid, l, r);
int p2 = query(2*node+1, mid+1, end, l, r);
return Math.min(p1,p2);
}

public static void update(int node, int start, int end, int idx, int val)
{
if(start == end)
{
// Leaf node
arr[idx] = val;
tree[node] = val;
}
else
{
int mid = (start + end) / 2;
if(start <= idx && idx <= mid)
{

update(2*node, start, mid, idx, val);
}
else
{

update(2*node+1, mid+1, end, idx, val);
}


tree[node] = Math.min(tree[2*node], tree[2*node +1]);
}
}

}


Loading