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

Binary tree updated #1

Open
wants to merge 1 commit 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
151 changes: 151 additions & 0 deletions BTree/src/BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package com.company;

public class BinarySearchTree {
private Node root;


public void insert(int value) {
if (root == null) {
insertRoot(value);
return;
}
insert(value, root);
}

private void insertRoot(int value) {
this.root = new Node(value);
}

private void insert(int value, Node node) {
if (node.value == value) {
node.count++;
return;
}
if (node.value > value) {
if (node.left == null) {
node.left = new Node(value);
} else {
insert(value, node.left);
}
}
if (node.value < value) {
if (node.right == null) {
node.right = new Node(value);
} else {
insert(value, node.right);
}
}
}


public void display() {
display(root, "");
}

private void display(Node node, String indent) {
if (node == null) {
return;
}
System.out.println(indent + node.value);
display(node.left, indent + "\t");
display(node.right, indent + "\t");
}

public void populate(int[] data) {
populate(data, 0, data.length - 1);
}

private void populate(int[] data, int start, int end) {
if (start > end) {
return;
}
int mid = (start + end) / 2;
insert(data[mid]);
populate(data, start, mid - 1);
populate(data, start, mid + 1);
}

private void range(Node node, int k1, int k2) {
if (node == null) {
return;
}

if (node.value >= k1 && node.value <= k2) {
System.out.println(node.value);
}

range(node.left, k1, k2);
range(node.right, k1, k2);
}


public int smallest() {
return smallest(root);
}

public int succesor(Node node) {
return smallest(node.right);
}

private int smallest(Node node) {
if (node.left == null) {
return node.value;
}
return smallest(node.left);
}

public boolean isBST() {
return isBST(root, Integer.MAX_VALUE, Integer.MIN_VALUE);
}

private boolean isBST(Node node, int max, int min) {
if (node == null) {
return true;
}

if (node.value >= max || node.value <= min) {
return false;
}

return isBST(node.left, node.value, min) && isBST(node.right, max, node.value);
}

public Node rightRotate(Node node) {
// before swap
Node x = node;
Node y = node.left;
Node t2 = y.right;
// after swap
y.right = x;
x.left = t2;
return y;
}
public Node leftRotate(Node node) {

// before swap
Node y = node;
Node x = y.right;
Node t2 = x.left;


// after swap
x.left = y;
y.right = t2;

return x;
}

private class Node {
private int value;
private Node left;
private Node right;
private Node parent;
private int count;

public Node(int value) {
this.value = value;
this.count = 1;
}

}
}