mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-15 01:40:49 +08:00
docs: update the whole repository
* fix some bugs * delete duplicate files * format code
This commit is contained in:
@ -1,19 +1,21 @@
|
||||
package DataStructures.Trees;
|
||||
|
||||
public class AVLTree {
|
||||
|
||||
|
||||
private Node root;
|
||||
|
||||
|
||||
private class Node {
|
||||
private int key;
|
||||
private int balance;
|
||||
private int height;
|
||||
private Node left, right, parent;
|
||||
|
||||
|
||||
Node(int k, Node p) {
|
||||
key = k;
|
||||
parent = p;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public boolean insert(int key) {
|
||||
if (root == null)
|
||||
root = new Node(key, null);
|
||||
@ -23,12 +25,12 @@ public class AVLTree {
|
||||
while (true) {
|
||||
if (n.key == key)
|
||||
return false;
|
||||
|
||||
|
||||
parent = n;
|
||||
|
||||
|
||||
boolean goLeft = n.key > key;
|
||||
n = goLeft ? n.left : n.right;
|
||||
|
||||
|
||||
if (n == null) {
|
||||
if (goLeft) {
|
||||
parent.left = new Node(key, parent);
|
||||
@ -42,38 +44,38 @@ public class AVLTree {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void delete(Node node){
|
||||
if(node.left == null && node.right == null){
|
||||
if(node.parent == null) root = null;
|
||||
else{
|
||||
|
||||
private void delete(Node node) {
|
||||
if (node.left == null && node.right == null) {
|
||||
if (node.parent == null) root = null;
|
||||
else {
|
||||
Node parent = node.parent;
|
||||
if(parent.left == node){
|
||||
if (parent.left == node) {
|
||||
parent.left = null;
|
||||
}else parent.right = null;
|
||||
} else parent.right = null;
|
||||
rebalance(parent);
|
||||
}
|
||||
return;
|
||||
}
|
||||
if(node.left!=null){
|
||||
if (node.left != null) {
|
||||
Node child = node.left;
|
||||
while (child.right!=null) child = child.right;
|
||||
while (child.right != null) child = child.right;
|
||||
node.key = child.key;
|
||||
delete(child);
|
||||
}else{
|
||||
} else {
|
||||
Node child = node.right;
|
||||
while (child.left!=null) child = child.left;
|
||||
while (child.left != null) child = child.left;
|
||||
node.key = child.key;
|
||||
delete(child);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void delete(int delKey) {
|
||||
if (root == null)
|
||||
return;
|
||||
Node node = root;
|
||||
Node child = root;
|
||||
|
||||
|
||||
while (child != null) {
|
||||
node = child;
|
||||
child = delKey >= node.key ? node.right : node.left;
|
||||
@ -83,43 +85,43 @@ public class AVLTree {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void rebalance(Node n) {
|
||||
setBalance(n);
|
||||
|
||||
|
||||
if (n.balance == -2) {
|
||||
if (height(n.left.left) >= height(n.left.right))
|
||||
n = rotateRight(n);
|
||||
else
|
||||
n = rotateLeftThenRight(n);
|
||||
|
||||
|
||||
} else if (n.balance == 2) {
|
||||
if (height(n.right.right) >= height(n.right.left))
|
||||
n = rotateLeft(n);
|
||||
else
|
||||
n = rotateRightThenLeft(n);
|
||||
}
|
||||
|
||||
|
||||
if (n.parent != null) {
|
||||
rebalance(n.parent);
|
||||
} else {
|
||||
root = n;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Node rotateLeft(Node a) {
|
||||
|
||||
|
||||
Node b = a.right;
|
||||
b.parent = a.parent;
|
||||
|
||||
|
||||
a.right = b.left;
|
||||
|
||||
|
||||
if (a.right != null)
|
||||
a.right.parent = a;
|
||||
|
||||
|
||||
b.left = a;
|
||||
a.parent = b;
|
||||
|
||||
|
||||
if (b.parent != null) {
|
||||
if (b.parent.right == a) {
|
||||
b.parent.right = b;
|
||||
@ -127,25 +129,25 @@ public class AVLTree {
|
||||
b.parent.left = b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setBalance(a, b);
|
||||
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
private Node rotateRight(Node a) {
|
||||
|
||||
|
||||
Node b = a.left;
|
||||
b.parent = a.parent;
|
||||
|
||||
|
||||
a.left = b.right;
|
||||
|
||||
|
||||
if (a.left != null)
|
||||
a.left.parent = a;
|
||||
|
||||
|
||||
b.right = a;
|
||||
a.parent = b;
|
||||
|
||||
|
||||
if (b.parent != null) {
|
||||
if (b.parent.right == a) {
|
||||
b.parent.right = b;
|
||||
@ -153,39 +155,39 @@ public class AVLTree {
|
||||
b.parent.left = b;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setBalance(a, b);
|
||||
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
private Node rotateLeftThenRight(Node n) {
|
||||
n.left = rotateLeft(n.left);
|
||||
return rotateRight(n);
|
||||
}
|
||||
|
||||
|
||||
private Node rotateRightThenLeft(Node n) {
|
||||
n.right = rotateRight(n.right);
|
||||
return rotateLeft(n);
|
||||
}
|
||||
|
||||
|
||||
private int height(Node n) {
|
||||
if (n == null)
|
||||
return -1;
|
||||
return n.height;
|
||||
}
|
||||
|
||||
|
||||
private void setBalance(Node... nodes) {
|
||||
for (Node n : nodes) {
|
||||
reheight(n);
|
||||
n.balance = height(n.right) - height(n.left);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void printBalance() {
|
||||
printBalance(root);
|
||||
}
|
||||
|
||||
|
||||
private void printBalance(Node n) {
|
||||
if (n != null) {
|
||||
printBalance(n.left);
|
||||
@ -193,20 +195,20 @@ public class AVLTree {
|
||||
printBalance(n.right);
|
||||
}
|
||||
}
|
||||
|
||||
private void reheight(Node node){
|
||||
if(node!=null){
|
||||
node.height=1 + Math.max(height(node.left), height(node.right));
|
||||
|
||||
private void reheight(Node node) {
|
||||
if (node != null) {
|
||||
node.height = 1 + Math.max(height(node.left), height(node.right));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
AVLTree tree = new AVLTree();
|
||||
|
||||
|
||||
System.out.println("Inserting values 1 to 10");
|
||||
for (int i = 1; i < 10; i++)
|
||||
tree.insert(i);
|
||||
|
||||
|
||||
System.out.print("Printing balance: ");
|
||||
tree.printBalance();
|
||||
}
|
||||
|
@ -1,272 +1,275 @@
|
||||
package DataStructures.Trees;
|
||||
|
||||
/**
|
||||
* This entire class is used to build a Binary Tree data structure.
|
||||
* There is the Node Class and the Tree Class, both explained below.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
* This entire class is used to build a Binary Tree data structure.
|
||||
* There is the Node Class and the Tree Class, both explained below.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* This class implements the nodes that will go on the Binary Tree.
|
||||
* They consist of the data in them, the node to the left, the node
|
||||
* to the right, and the parent from which they came from.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Node{
|
||||
/** Data for the node */
|
||||
public int data;
|
||||
/** The Node to the left of this one */
|
||||
public Node left;
|
||||
/** The Node to the right of this one */
|
||||
public Node right;
|
||||
/** The parent of this node */
|
||||
public Node parent;
|
||||
* A binary tree is a data structure in which an element
|
||||
* has two successors(children). The left child is usually
|
||||
* smaller than the parent, and the right child is usually
|
||||
* bigger.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class BinaryTree {
|
||||
|
||||
/**
|
||||
* Constructor of Node
|
||||
*
|
||||
* @param value Value to put in the node
|
||||
*/
|
||||
public Node(int value){
|
||||
data = value;
|
||||
left = null;
|
||||
right = null;
|
||||
parent = null;
|
||||
}
|
||||
/**
|
||||
* This class implements the nodes that will go on the Binary Tree.
|
||||
* They consist of the data in them, the node to the left, the node
|
||||
* to the right, and the parent from which they came from.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Node {
|
||||
/** Data for the node */
|
||||
public int data;
|
||||
/** The Node to the left of this one */
|
||||
public Node left;
|
||||
/** The Node to the right of this one */
|
||||
public Node right;
|
||||
/** The parent of this node */
|
||||
public Node parent;
|
||||
|
||||
/**
|
||||
* Constructor of Node
|
||||
*
|
||||
* @param value Value to put in the node
|
||||
*/
|
||||
public Node(int value) {
|
||||
data = value;
|
||||
left = null;
|
||||
right = null;
|
||||
parent = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** The root of the Binary Tree */
|
||||
private Node root;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public BinaryTree() {
|
||||
root = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to find a Node with a certain value
|
||||
*
|
||||
* @param key Value being looked for
|
||||
* @return The node if it finds it, otherwise returns the parent
|
||||
*/
|
||||
public Node find(int key) {
|
||||
Node current = root;
|
||||
while (current != null) {
|
||||
if (key < current.data) {
|
||||
if (current.left == null)
|
||||
return current; //The key isn't exist, returns the parent
|
||||
current = current.left;
|
||||
} else if (key > current.data) {
|
||||
if (current.right == null)
|
||||
return current;
|
||||
current = current.right;
|
||||
} else { // If you find the value return it
|
||||
return current;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts certain value into the Binary Tree
|
||||
*
|
||||
* @param value Value to be inserted
|
||||
*/
|
||||
public void put(int value) {
|
||||
Node newNode = new Node(value);
|
||||
if (root == null)
|
||||
root = newNode;
|
||||
else {
|
||||
//This will return the soon to be parent of the value you're inserting
|
||||
Node parent = find(value);
|
||||
|
||||
//This if/else assigns the new node to be either the left or right child of the parent
|
||||
if (value < parent.data) {
|
||||
parent.left = newNode;
|
||||
parent.left.parent = parent;
|
||||
return;
|
||||
} else {
|
||||
parent.right = newNode;
|
||||
parent.right.parent = parent;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a given value from the Binary Tree
|
||||
*
|
||||
* @param value Value to be deleted
|
||||
* @return If the value was deleted
|
||||
*/
|
||||
public boolean remove(int value) {
|
||||
//temp is the node to be deleted
|
||||
Node temp = find(value);
|
||||
|
||||
//If the value doesn't exist
|
||||
if (temp.data != value)
|
||||
return false;
|
||||
|
||||
//No children
|
||||
if (temp.right == null && temp.left == null) {
|
||||
if (temp == root)
|
||||
root = null;
|
||||
|
||||
//This if/else assigns the new node to be either the left or right child of the parent
|
||||
else if (temp.parent.data < temp.data)
|
||||
temp.parent.right = null;
|
||||
else
|
||||
temp.parent.left = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
//Two children
|
||||
else if (temp.left != null && temp.right != null) {
|
||||
Node successor = findSuccessor(temp);
|
||||
|
||||
//The left tree of temp is made the left tree of the successor
|
||||
successor.left = temp.left;
|
||||
successor.left.parent = successor;
|
||||
|
||||
//If the successor has a right child, the child's grandparent is it's new parent
|
||||
if (successor.right != null && successor.parent != temp) {
|
||||
successor.right.parent = successor.parent;
|
||||
successor.parent.left = successor.right;
|
||||
successor.right = temp.right;
|
||||
successor.right.parent = successor;
|
||||
}
|
||||
if (temp == root) {
|
||||
successor.parent = null;
|
||||
root = successor;
|
||||
return true;
|
||||
}
|
||||
|
||||
//If you're not deleting the root
|
||||
else {
|
||||
successor.parent = temp.parent;
|
||||
|
||||
//This if/else assigns the new node to be either the left or right child of the parent
|
||||
if (temp.parent.data < temp.data)
|
||||
temp.parent.right = successor;
|
||||
else
|
||||
temp.parent.left = successor;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//One child
|
||||
else {
|
||||
//If it has a right child
|
||||
if (temp.right != null) {
|
||||
if (temp == root) {
|
||||
root = temp.right;
|
||||
return true;
|
||||
}
|
||||
|
||||
temp.right.parent = temp.parent;
|
||||
|
||||
//Assigns temp to left or right child
|
||||
if (temp.data < temp.parent.data)
|
||||
temp.parent.left = temp.right;
|
||||
else
|
||||
temp.parent.right = temp.right;
|
||||
return true;
|
||||
}
|
||||
//If it has a left child
|
||||
else {
|
||||
if (temp == root) {
|
||||
root = temp.left;
|
||||
return true;
|
||||
}
|
||||
|
||||
temp.left.parent = temp.parent;
|
||||
|
||||
//Assigns temp to left or right side
|
||||
if (temp.data < temp.parent.data)
|
||||
temp.parent.left = temp.left;
|
||||
else
|
||||
temp.parent.right = temp.left;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method finds the Successor to the Node given.
|
||||
* Move right once and go left down the tree as far as you can
|
||||
*
|
||||
* @param n Node that you want to find the Successor of
|
||||
* @return The Successor of the node
|
||||
*/
|
||||
public Node findSuccessor(Node n) {
|
||||
if (n.right == null)
|
||||
return n;
|
||||
Node current = n.right;
|
||||
Node parent = n.right;
|
||||
while (current != null) {
|
||||
parent = current;
|
||||
current = current.left;
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root of the Binary Tree
|
||||
*
|
||||
* @return the root of the Binary Tree
|
||||
*/
|
||||
public Node getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints leftChild - root - rightChild
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void inOrder(Node localRoot) {
|
||||
if (localRoot != null) {
|
||||
inOrder(localRoot.left);
|
||||
System.out.print(localRoot.data + " ");
|
||||
inOrder(localRoot.right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints root - leftChild - rightChild
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void preOrder(Node localRoot) {
|
||||
if (localRoot != null) {
|
||||
System.out.print(localRoot.data + " ");
|
||||
preOrder(localRoot.left);
|
||||
preOrder(localRoot.right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints rightChild - leftChild - root
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void postOrder(Node localRoot) {
|
||||
if (localRoot != null) {
|
||||
postOrder(localRoot.left);
|
||||
postOrder(localRoot.right);
|
||||
System.out.print(localRoot.data + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A binary tree is a data structure in which an element
|
||||
* has two successors(children). The left child is usually
|
||||
* smaller than the parent, and the right child is usually
|
||||
* bigger.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Tree{
|
||||
/** The root of the Binary Tree */
|
||||
private Node root;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public Tree(){
|
||||
root = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Method to find a Node with a certain value
|
||||
*
|
||||
* @param key Value being looked for
|
||||
* @return The node if it finds it, otherwise returns the parent
|
||||
*/
|
||||
public Node find(int key) {
|
||||
Node current = root;
|
||||
while (current != null) {
|
||||
if(key < current.data) {
|
||||
if(current.left == null)
|
||||
return current; //The key isn't exist, returns the parent
|
||||
current = current.left;
|
||||
} else if(key > current.data) {
|
||||
if(current.right == null)
|
||||
return current;
|
||||
current = current.right;
|
||||
} else { // If you find the value return it
|
||||
return current;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts certain value into the Binary Tree
|
||||
*
|
||||
* @param value Value to be inserted
|
||||
*/
|
||||
public void put(int value){
|
||||
Node newNode = new Node(value);
|
||||
if(root == null)
|
||||
root = newNode;
|
||||
else{
|
||||
//This will return the soon to be parent of the value you're inserting
|
||||
Node parent = find(value);
|
||||
|
||||
//This if/else assigns the new node to be either the left or right child of the parent
|
||||
if(value < parent.data){
|
||||
parent.left = newNode;
|
||||
parent.left.parent = parent;
|
||||
return;
|
||||
}
|
||||
else{
|
||||
parent.right = newNode;
|
||||
parent.right.parent = parent;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a given value from the Binary Tree
|
||||
*
|
||||
* @param value Value to be deleted
|
||||
* @return If the value was deleted
|
||||
*/
|
||||
public boolean remove(int value){
|
||||
//temp is the node to be deleted
|
||||
Node temp = find(value);
|
||||
|
||||
//If the value doesn't exist
|
||||
if(temp.data != value)
|
||||
return false;
|
||||
|
||||
//No children
|
||||
if(temp.right == null && temp.left == null){
|
||||
if(temp == root)
|
||||
root = null;
|
||||
|
||||
//This if/else assigns the new node to be either the left or right child of the parent
|
||||
else if(temp.parent.data < temp.data)
|
||||
temp.parent.right = null;
|
||||
else
|
||||
temp.parent.left = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
//Two children
|
||||
else if(temp.left != null && temp.right != null){
|
||||
Node successor = findSuccessor(temp);
|
||||
|
||||
//The left tree of temp is made the left tree of the successor
|
||||
successor.left = temp.left;
|
||||
successor.left.parent = successor;
|
||||
|
||||
//If the successor has a right child, the child's grandparent is it's new parent
|
||||
if(successor.right != null && successor.parent != temp){
|
||||
successor.right.parent = successor.parent;
|
||||
successor.parent.left = successor.right;
|
||||
successor.right = temp.right;
|
||||
successor.right.parent = successor;
|
||||
}
|
||||
if(temp == root){
|
||||
successor.parent = null;
|
||||
root = successor;
|
||||
return true;
|
||||
}
|
||||
|
||||
//If you're not deleting the root
|
||||
else{
|
||||
successor.parent = temp.parent;
|
||||
|
||||
//This if/else assigns the new node to be either the left or right child of the parent
|
||||
if(temp.parent.data < temp.data)
|
||||
temp.parent.right = successor;
|
||||
else
|
||||
temp.parent.left = successor;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
//One child
|
||||
else{
|
||||
//If it has a right child
|
||||
if(temp.right != null){
|
||||
if(temp == root){
|
||||
root = temp.right; return true;}
|
||||
|
||||
temp.right.parent = temp.parent;
|
||||
|
||||
//Assigns temp to left or right child
|
||||
if(temp.data < temp.parent.data)
|
||||
temp.parent.left = temp.right;
|
||||
else
|
||||
temp.parent.right = temp.right;
|
||||
return true;
|
||||
}
|
||||
//If it has a left child
|
||||
else{
|
||||
if(temp == root){
|
||||
root = temp.left; return true;}
|
||||
|
||||
temp.left.parent = temp.parent;
|
||||
|
||||
//Assigns temp to left or right side
|
||||
if(temp.data < temp.parent.data)
|
||||
temp.parent.left = temp.left;
|
||||
else
|
||||
temp.parent.right = temp.left;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method finds the Successor to the Node given.
|
||||
* Move right once and go left down the tree as far as you can
|
||||
*
|
||||
* @param n Node that you want to find the Successor of
|
||||
* @return The Successor of the node
|
||||
*/
|
||||
public Node findSuccessor(Node n){
|
||||
if(n.right == null)
|
||||
return n;
|
||||
Node current = n.right;
|
||||
Node parent = n.right;
|
||||
while(current != null){
|
||||
parent = current;
|
||||
current = current.left;
|
||||
}
|
||||
return parent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the root of the Binary Tree
|
||||
*
|
||||
* @return the root of the Binary Tree
|
||||
*/
|
||||
public Node getRoot(){
|
||||
return root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints leftChild - root - rightChild
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void inOrder(Node localRoot){
|
||||
if(localRoot != null){
|
||||
inOrder(localRoot.left);
|
||||
System.out.print(localRoot.data + " ");
|
||||
inOrder(localRoot.right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints root - leftChild - rightChild
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void preOrder(Node localRoot){
|
||||
if(localRoot != null){
|
||||
System.out.print(localRoot.data + " ");
|
||||
preOrder(localRoot.left);
|
||||
preOrder(localRoot.right);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Prints rightChild - leftChild - root
|
||||
*
|
||||
* @param localRoot The local root of the binary tree
|
||||
*/
|
||||
public void postOrder(Node localRoot){
|
||||
if(localRoot != null){
|
||||
postOrder(localRoot.left);
|
||||
postOrder(localRoot.right);
|
||||
System.out.print(localRoot.data + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,100 +0,0 @@
|
||||
/**
|
||||
*
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
*
|
||||
*/
|
||||
import java.util.LinkedList;
|
||||
|
||||
public class FindHeightOfTree {
|
||||
|
||||
// Driver Program
|
||||
public static void main(String[] args) {
|
||||
Node tree = new Node(5);
|
||||
tree.insert(3);
|
||||
tree.insert(7);
|
||||
tree.insert(1);
|
||||
tree.insert(-1);
|
||||
tree.insert(29);
|
||||
tree.insert(93);
|
||||
tree.insert(6);
|
||||
tree.insert(0);
|
||||
tree.insert(-5);
|
||||
tree.insert(-6);
|
||||
tree.insert(-8);
|
||||
tree.insert(-1);
|
||||
|
||||
// A level order representation of the tree
|
||||
tree.printLevelOrder();
|
||||
System.out.println();
|
||||
|
||||
System.out.println("Height of the tree is: " + tree.findHeight());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The Node class which initializes a Node of a tree
|
||||
* printLevelOrder: ROOT -> ROOT's CHILDREN -> ROOT's CHILDREN's CHILDREN -> etc
|
||||
* findHeight: Returns the height of the tree i.e. the number of links between root and farthest leaf
|
||||
*/
|
||||
class Node {
|
||||
Node left, right;
|
||||
int data;
|
||||
|
||||
public Node(int data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void insert (int value) {
|
||||
if (value < data) {
|
||||
if (left == null) {
|
||||
left = new Node(value);
|
||||
}
|
||||
else {
|
||||
left.insert(value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (right == null) {
|
||||
right = new Node(value);
|
||||
}
|
||||
else {
|
||||
right.insert(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void printLevelOrder() {
|
||||
LinkedList<Node> queue = new LinkedList<>();
|
||||
queue.add(this);
|
||||
while(!queue.isEmpty()) {
|
||||
Node n = queue.poll();
|
||||
System.out.print(n.data + " ");
|
||||
if (n.left != null) {
|
||||
queue.add(n.left);
|
||||
}
|
||||
if (n.right != null) {
|
||||
queue.add(n.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int findHeight() {
|
||||
return findHeight(this);
|
||||
}
|
||||
|
||||
private int findHeight(Node root) {
|
||||
if (root.left == null && root.right == null) {
|
||||
return 0;
|
||||
}
|
||||
else if (root.left != null && root.right != null) {
|
||||
return 1 + Math.max(findHeight(root.left), findHeight(root.right));
|
||||
}
|
||||
else if (root.left == null && root.right != null) {
|
||||
return 1 + findHeight(root.right);
|
||||
}
|
||||
else {
|
||||
return 1 + findHeight(root.left);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,3 +1,5 @@
|
||||
package DataStructures.Trees;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Scanner;
|
||||
@ -224,3 +226,4 @@ public class GenericTree {
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,74 +1,55 @@
|
||||
class Node
|
||||
{
|
||||
int data;
|
||||
Node left, right;
|
||||
public Node(int item)
|
||||
{
|
||||
data = item;
|
||||
left = right = null;
|
||||
package DataStructures.Trees;
|
||||
|
||||
public class LevelOrderTraversal {
|
||||
|
||||
class Node {
|
||||
int data;
|
||||
Node left, right;
|
||||
|
||||
public Node(int item) {
|
||||
data = item;
|
||||
left = right = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class LevelOrderTraversal
|
||||
{
|
||||
|
||||
// Root of the Binary Tree
|
||||
Node root;
|
||||
|
||||
public LevelOrderTraversal()
|
||||
{
|
||||
|
||||
public LevelOrderTraversal() {
|
||||
root = null;
|
||||
}
|
||||
|
||||
|
||||
/* function to print level order traversal of tree*/
|
||||
void printLevelOrder()
|
||||
{
|
||||
void printLevelOrder() {
|
||||
int h = height(root);
|
||||
int i;
|
||||
for (i=1; i<=h; i++)
|
||||
for (i = 1; i <= h; i++)
|
||||
printGivenLevel(root, i);
|
||||
}
|
||||
|
||||
|
||||
/* Compute the "height" of a tree -- the number of
|
||||
nodes along the longest path from the root node
|
||||
down to the farthest leaf node.*/
|
||||
int height(Node root)
|
||||
{
|
||||
int height(Node root) {
|
||||
if (root == null)
|
||||
return 0;
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
else {
|
||||
/**
|
||||
* Return the height of larger subtree
|
||||
*/
|
||||
return Math.max(height(root.left),height(root.right)) + 1;
|
||||
return Math.max(height(root.left), height(root.right)) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* Print nodes at the given level */
|
||||
void printGivenLevel (Node root ,int level)
|
||||
{
|
||||
void printGivenLevel(Node root, int level) {
|
||||
if (root == null)
|
||||
return;
|
||||
if (level == 1)
|
||||
System.out.print(root.data + " ");
|
||||
else if (level > 1)
|
||||
{
|
||||
printGivenLevel(root.left, level-1);
|
||||
printGivenLevel(root.right, level-1);
|
||||
else if (level > 1) {
|
||||
printGivenLevel(root.left, level - 1);
|
||||
printGivenLevel(root.right, level - 1);
|
||||
}
|
||||
}
|
||||
|
||||
/* Driver program to test above functions */
|
||||
public static void main(String args[])
|
||||
{
|
||||
LevelOrderTraversal tree = new LevelOrderTraversal();
|
||||
tree.root= new Node(1);
|
||||
tree.root.left= new Node(2);
|
||||
tree.root.right= new Node(3);
|
||||
tree.root.left.left= new Node(4);
|
||||
tree.root.left.right= new Node(5);
|
||||
|
||||
System.out.println("Level order traversal of binary tree is ");
|
||||
tree.printLevelOrder();
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +1,48 @@
|
||||
package DataStructures.Trees;
|
||||
|
||||
import java.util.Queue;
|
||||
import java.util.LinkedList;
|
||||
|
||||
/* Class to represent Tree node */
|
||||
class Node {
|
||||
int data;
|
||||
Node left, right;
|
||||
|
||||
public Node(int item) {
|
||||
data = item;
|
||||
left = null;
|
||||
right = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/* Class to print Level Order Traversal */
|
||||
public class LevelOrderTraversalQueue {
|
||||
|
||||
|
||||
/* Class to represent Tree node */
|
||||
class Node {
|
||||
int data;
|
||||
Node left, right;
|
||||
|
||||
public Node(int item) {
|
||||
data = item;
|
||||
left = null;
|
||||
right = null;
|
||||
}
|
||||
}
|
||||
|
||||
Node root;
|
||||
|
||||
|
||||
/* Given a binary tree. Print its nodes in level order
|
||||
using array for implementing queue */
|
||||
void printLevelOrder()
|
||||
{
|
||||
void printLevelOrder() {
|
||||
Queue<Node> queue = new LinkedList<Node>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty())
|
||||
{
|
||||
while (!queue.isEmpty()) {
|
||||
|
||||
/* poll() removes the present head.
|
||||
For more information on poll() visit
|
||||
http://www.tutorialspoint.com/java/util/linkedlist_poll.htm */
|
||||
Node tempNode = queue.poll();
|
||||
System.out.print(tempNode.data + " ");
|
||||
|
||||
|
||||
/*Enqueue left child */
|
||||
if (tempNode.left != null) {
|
||||
queue.add(tempNode.left);
|
||||
}
|
||||
|
||||
|
||||
/*Enqueue right child */
|
||||
if (tempNode.right != null) {
|
||||
queue.add(tempNode.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String args[])
|
||||
{
|
||||
/* creating a binary tree and entering
|
||||
the nodes */
|
||||
LevelOrderTraversalQueue tree_level = new LevelOrderTraversalQueue();
|
||||
tree_level.root = new Node(1);
|
||||
tree_level.root.left = new Node(2);
|
||||
tree_level.root.right = new Node(3);
|
||||
tree_level.root.left.left = new Node(4);
|
||||
tree_level.root.left.right = new Node(5);
|
||||
|
||||
System.out.println("Level order traversal of binary tree is - ");
|
||||
tree_level.printLevelOrder();
|
||||
}
|
||||
}
|
@ -1,87 +1,88 @@
|
||||
// Java program to print top view of Binary tree
|
||||
import java.util.*;
|
||||
|
||||
package DataStructures.Trees;// Java program to print top view of Binary tree
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
// Class for a tree node
|
||||
class TreeNode
|
||||
{
|
||||
class TreeNode {
|
||||
// Members
|
||||
int key;
|
||||
TreeNode left, right;
|
||||
|
||||
|
||||
// Constructor
|
||||
public TreeNode(int key)
|
||||
{
|
||||
public TreeNode(int key) {
|
||||
this.key = key;
|
||||
left = right = null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// A class to represent a queue item. The queue is used to do Level
|
||||
// order traversal. Every Queue item contains node and horizontal
|
||||
// distance of node from root
|
||||
class QItem
|
||||
{
|
||||
TreeNode node;
|
||||
int hd;
|
||||
public QItem(TreeNode n, int h)
|
||||
{
|
||||
class QItem {
|
||||
TreeNode node;
|
||||
int hd;
|
||||
|
||||
public QItem(TreeNode n, int h) {
|
||||
node = n;
|
||||
hd = h;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Class for a Binary Tree
|
||||
class Tree
|
||||
{
|
||||
class Tree {
|
||||
TreeNode root;
|
||||
|
||||
|
||||
// Constructors
|
||||
public Tree() { root = null; }
|
||||
public Tree(TreeNode n) { root = n; }
|
||||
|
||||
public Tree() {
|
||||
root = null;
|
||||
}
|
||||
|
||||
public Tree(TreeNode n) {
|
||||
root = n;
|
||||
}
|
||||
|
||||
// This method prints nodes in top view of binary tree
|
||||
public void printTopView()
|
||||
{
|
||||
public void printTopView() {
|
||||
// base case
|
||||
if (root == null) { return; }
|
||||
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Creates an empty hashset
|
||||
HashSet<Integer> set = new HashSet<>();
|
||||
|
||||
|
||||
// Create a queue and add root to it
|
||||
Queue<QItem> Q = new LinkedList<QItem>();
|
||||
Q.add(new QItem(root, 0)); // Horizontal distance of root is 0
|
||||
|
||||
|
||||
// Standard BFS or level order traversal loop
|
||||
while (!Q.isEmpty())
|
||||
{
|
||||
while (!Q.isEmpty()) {
|
||||
// Remove the front item and get its details
|
||||
QItem qi = Q.remove();
|
||||
int hd = qi.hd;
|
||||
TreeNode n = qi.node;
|
||||
|
||||
|
||||
// If this is the first node at its horizontal distance,
|
||||
// then this node is in top view
|
||||
if (!set.contains(hd))
|
||||
{
|
||||
if (!set.contains(hd)) {
|
||||
set.add(hd);
|
||||
System.out.print(n.key + " ");
|
||||
}
|
||||
|
||||
|
||||
// Enqueue left and right children of current node
|
||||
if (n.left != null)
|
||||
Q.add(new QItem(n.left, hd-1));
|
||||
Q.add(new QItem(n.left, hd - 1));
|
||||
if (n.right != null)
|
||||
Q.add(new QItem(n.right, hd+1));
|
||||
Q.add(new QItem(n.right, hd + 1));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Driver class to test above methods
|
||||
public class PrintTopViewofTree
|
||||
{
|
||||
public static void main(String[] args)
|
||||
{
|
||||
public class PrintTopViewofTree {
|
||||
public static void main(String[] args) {
|
||||
/* Create following Binary Tree
|
||||
1
|
||||
/ \
|
||||
|
@ -1,330 +1,333 @@
|
||||
package DataStructures.Trees;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jack870131
|
||||
*/
|
||||
public class RedBlackBST {
|
||||
|
||||
private final int R = 0;
|
||||
private final int B = 1;
|
||||
private final int R = 0;
|
||||
private final int B = 1;
|
||||
|
||||
private class Node {
|
||||
private class Node {
|
||||
|
||||
int key = -1, color = B;
|
||||
Node left = nil, right = nil, p = nil;
|
||||
int key = -1, color = B;
|
||||
Node left = nil, right = nil, p = nil;
|
||||
|
||||
Node(int key) {
|
||||
this.key = key;
|
||||
}
|
||||
}
|
||||
Node(int key) {
|
||||
this.key = key;
|
||||
}
|
||||
}
|
||||
|
||||
private final Node nil = new Node(-1);
|
||||
private Node root = nil;
|
||||
private final Node nil = new Node(-1);
|
||||
private Node root = nil;
|
||||
|
||||
public void printTree(Node node) {
|
||||
if (node == nil) {
|
||||
return;
|
||||
}
|
||||
printTree(node.left);
|
||||
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
|
||||
printTree(node.right);
|
||||
}
|
||||
public void printTree(Node node) {
|
||||
if (node == nil) {
|
||||
return;
|
||||
}
|
||||
printTree(node.left);
|
||||
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
|
||||
printTree(node.right);
|
||||
}
|
||||
|
||||
public void printTreepre(Node node) {
|
||||
if (node == nil) {
|
||||
return;
|
||||
}
|
||||
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
|
||||
printTree(node.left);
|
||||
printTree(node.right);
|
||||
}
|
||||
public void printTreepre(Node node) {
|
||||
if (node == nil) {
|
||||
return;
|
||||
}
|
||||
System.out.print(((node.color == R) ? " R " : " B ") + "Key: " + node.key + " Parent: " + node.p.key + "\n");
|
||||
printTree(node.left);
|
||||
printTree(node.right);
|
||||
}
|
||||
|
||||
private Node findNode(Node findNode, Node node) {
|
||||
if (root == nil) {
|
||||
return null;
|
||||
}
|
||||
if (findNode.key < node.key) {
|
||||
if (node.left != nil) {
|
||||
return findNode(findNode, node.left);
|
||||
}
|
||||
} else if (findNode.key > node.key) {
|
||||
if (node.right != nil) {
|
||||
return findNode(findNode, node.right);
|
||||
}
|
||||
} else if (findNode.key == node.key) {
|
||||
return node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
private Node findNode(Node findNode, Node node) {
|
||||
if (root == nil) {
|
||||
return null;
|
||||
}
|
||||
if (findNode.key < node.key) {
|
||||
if (node.left != nil) {
|
||||
return findNode(findNode, node.left);
|
||||
}
|
||||
} else if (findNode.key > node.key) {
|
||||
if (node.right != nil) {
|
||||
return findNode(findNode, node.right);
|
||||
}
|
||||
} else if (findNode.key == node.key) {
|
||||
return node;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private void insert(Node node) {
|
||||
Node temp = root;
|
||||
if (root == nil) {
|
||||
root = node;
|
||||
node.color = B;
|
||||
node.p = nil;
|
||||
} else {
|
||||
node.color = R;
|
||||
while (true) {
|
||||
if (node.key < temp.key) {
|
||||
if (temp.left == nil) {
|
||||
temp.left = node;
|
||||
node.p = temp;
|
||||
break;
|
||||
} else {
|
||||
temp = temp.left;
|
||||
}
|
||||
} else if (node.key >= temp.key) {
|
||||
if (temp.right == nil) {
|
||||
temp.right = node;
|
||||
node.p = temp;
|
||||
break;
|
||||
} else {
|
||||
temp = temp.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
fixTree(node);
|
||||
}
|
||||
}
|
||||
private void insert(Node node) {
|
||||
Node temp = root;
|
||||
if (root == nil) {
|
||||
root = node;
|
||||
node.color = B;
|
||||
node.p = nil;
|
||||
} else {
|
||||
node.color = R;
|
||||
while (true) {
|
||||
if (node.key < temp.key) {
|
||||
if (temp.left == nil) {
|
||||
temp.left = node;
|
||||
node.p = temp;
|
||||
break;
|
||||
} else {
|
||||
temp = temp.left;
|
||||
}
|
||||
} else if (node.key >= temp.key) {
|
||||
if (temp.right == nil) {
|
||||
temp.right = node;
|
||||
node.p = temp;
|
||||
break;
|
||||
} else {
|
||||
temp = temp.right;
|
||||
}
|
||||
}
|
||||
}
|
||||
fixTree(node);
|
||||
}
|
||||
}
|
||||
|
||||
private void fixTree(Node node) {
|
||||
while (node.p.color == R) {
|
||||
Node y = nil;
|
||||
if (node.p == node.p.p.left) {
|
||||
y = node.p.p.right;
|
||||
private void fixTree(Node node) {
|
||||
while (node.p.color == R) {
|
||||
Node y = nil;
|
||||
if (node.p == node.p.p.left) {
|
||||
y = node.p.p.right;
|
||||
|
||||
if (y != nil && y.color == R) {
|
||||
node.p.color = B;
|
||||
y.color = B;
|
||||
node.p.p.color = R;
|
||||
node = node.p.p;
|
||||
continue;
|
||||
}
|
||||
if (node == node.p.right) {
|
||||
node = node.p;
|
||||
rotateLeft(node);
|
||||
}
|
||||
node.p.color = B;
|
||||
node.p.p.color = R;
|
||||
rotateRight(node.p.p);
|
||||
} else {
|
||||
y = node.p.p.left;
|
||||
if (y != nil && y.color == R) {
|
||||
node.p.color = B;
|
||||
y.color = B;
|
||||
node.p.p.color = R;
|
||||
node = node.p.p;
|
||||
continue;
|
||||
}
|
||||
if (node == node.p.left) {
|
||||
node = node.p;
|
||||
rotateRight(node);
|
||||
}
|
||||
node.p.color = B;
|
||||
node.p.p.color = R;
|
||||
rotateLeft(node.p.p);
|
||||
}
|
||||
}
|
||||
root.color = B;
|
||||
}
|
||||
if (y != nil && y.color == R) {
|
||||
node.p.color = B;
|
||||
y.color = B;
|
||||
node.p.p.color = R;
|
||||
node = node.p.p;
|
||||
continue;
|
||||
}
|
||||
if (node == node.p.right) {
|
||||
node = node.p;
|
||||
rotateLeft(node);
|
||||
}
|
||||
node.p.color = B;
|
||||
node.p.p.color = R;
|
||||
rotateRight(node.p.p);
|
||||
} else {
|
||||
y = node.p.p.left;
|
||||
if (y != nil && y.color == R) {
|
||||
node.p.color = B;
|
||||
y.color = B;
|
||||
node.p.p.color = R;
|
||||
node = node.p.p;
|
||||
continue;
|
||||
}
|
||||
if (node == node.p.left) {
|
||||
node = node.p;
|
||||
rotateRight(node);
|
||||
}
|
||||
node.p.color = B;
|
||||
node.p.p.color = R;
|
||||
rotateLeft(node.p.p);
|
||||
}
|
||||
}
|
||||
root.color = B;
|
||||
}
|
||||
|
||||
void rotateLeft(Node node) {
|
||||
if (node.p != nil) {
|
||||
if (node == node.p.left) {
|
||||
node.p.left = node.right;
|
||||
} else {
|
||||
node.p.right = node.right;
|
||||
}
|
||||
node.right.p = node.p;
|
||||
node.p = node.right;
|
||||
if (node.right.left != nil) {
|
||||
node.right.left.p = node;
|
||||
}
|
||||
node.right = node.right.left;
|
||||
node.p.left = node;
|
||||
} else {
|
||||
Node right = root.right;
|
||||
root.right = right.left;
|
||||
right.left.p = root;
|
||||
root.p = right;
|
||||
right.left = root;
|
||||
right.p = nil;
|
||||
root = right;
|
||||
}
|
||||
}
|
||||
void rotateLeft(Node node) {
|
||||
if (node.p != nil) {
|
||||
if (node == node.p.left) {
|
||||
node.p.left = node.right;
|
||||
} else {
|
||||
node.p.right = node.right;
|
||||
}
|
||||
node.right.p = node.p;
|
||||
node.p = node.right;
|
||||
if (node.right.left != nil) {
|
||||
node.right.left.p = node;
|
||||
}
|
||||
node.right = node.right.left;
|
||||
node.p.left = node;
|
||||
} else {
|
||||
Node right = root.right;
|
||||
root.right = right.left;
|
||||
right.left.p = root;
|
||||
root.p = right;
|
||||
right.left = root;
|
||||
right.p = nil;
|
||||
root = right;
|
||||
}
|
||||
}
|
||||
|
||||
void rotateRight(Node node) {
|
||||
if (node.p != nil) {
|
||||
if (node == node.p.left) {
|
||||
node.p.left = node.left;
|
||||
} else {
|
||||
node.p.right = node.left;
|
||||
}
|
||||
void rotateRight(Node node) {
|
||||
if (node.p != nil) {
|
||||
if (node == node.p.left) {
|
||||
node.p.left = node.left;
|
||||
} else {
|
||||
node.p.right = node.left;
|
||||
}
|
||||
|
||||
node.left.p = node.p;
|
||||
node.p = node.left;
|
||||
if (node.left.right != nil) {
|
||||
node.left.right.p = node;
|
||||
}
|
||||
node.left = node.left.right;
|
||||
node.p.right = node;
|
||||
} else {
|
||||
Node left = root.left;
|
||||
root.left = root.left.right;
|
||||
left.right.p = root;
|
||||
root.p = left;
|
||||
left.right = root;
|
||||
left.p = nil;
|
||||
root = left;
|
||||
}
|
||||
}
|
||||
node.left.p = node.p;
|
||||
node.p = node.left;
|
||||
if (node.left.right != nil) {
|
||||
node.left.right.p = node;
|
||||
}
|
||||
node.left = node.left.right;
|
||||
node.p.right = node;
|
||||
} else {
|
||||
Node left = root.left;
|
||||
root.left = root.left.right;
|
||||
left.right.p = root;
|
||||
root.p = left;
|
||||
left.right = root;
|
||||
left.p = nil;
|
||||
root = left;
|
||||
}
|
||||
}
|
||||
|
||||
void transplant(Node target, Node with) {
|
||||
if (target.p == nil) {
|
||||
root = with;
|
||||
} else if (target == target.p.left) {
|
||||
target.p.left = with;
|
||||
} else
|
||||
target.p.right = with;
|
||||
with.p = target.p;
|
||||
}
|
||||
void transplant(Node target, Node with) {
|
||||
if (target.p == nil) {
|
||||
root = with;
|
||||
} else if (target == target.p.left) {
|
||||
target.p.left = with;
|
||||
} else
|
||||
target.p.right = with;
|
||||
with.p = target.p;
|
||||
}
|
||||
|
||||
Node treeMinimum(Node subTreeRoot) {
|
||||
while (subTreeRoot.left != nil) {
|
||||
subTreeRoot = subTreeRoot.left;
|
||||
}
|
||||
return subTreeRoot;
|
||||
}
|
||||
Node treeMinimum(Node subTreeRoot) {
|
||||
while (subTreeRoot.left != nil) {
|
||||
subTreeRoot = subTreeRoot.left;
|
||||
}
|
||||
return subTreeRoot;
|
||||
}
|
||||
|
||||
boolean delete(Node z) {
|
||||
if ((z = findNode(z, root)) == null)
|
||||
return false;
|
||||
Node x;
|
||||
Node y = z;
|
||||
int yorigcolor = y.color;
|
||||
boolean delete(Node z) {
|
||||
if ((z = findNode(z, root)) == null)
|
||||
return false;
|
||||
Node x;
|
||||
Node y = z;
|
||||
int yorigcolor = y.color;
|
||||
|
||||
if (z.left == nil) {
|
||||
x = z.right;
|
||||
transplant(z, z.right);
|
||||
} else if (z.right == nil) {
|
||||
x = z.left;
|
||||
transplant(z, z.left);
|
||||
} else {
|
||||
y = treeMinimum(z.right);
|
||||
yorigcolor = y.color;
|
||||
x = y.right;
|
||||
if (y.p == z)
|
||||
x.p = y;
|
||||
else {
|
||||
transplant(y, y.right);
|
||||
y.right = z.right;
|
||||
y.right.p = y;
|
||||
}
|
||||
transplant(z, y);
|
||||
y.left = z.left;
|
||||
y.left.p = y;
|
||||
y.color = z.color;
|
||||
}
|
||||
if (yorigcolor == B)
|
||||
deleteFixup(x);
|
||||
return true;
|
||||
}
|
||||
if (z.left == nil) {
|
||||
x = z.right;
|
||||
transplant(z, z.right);
|
||||
} else if (z.right == nil) {
|
||||
x = z.left;
|
||||
transplant(z, z.left);
|
||||
} else {
|
||||
y = treeMinimum(z.right);
|
||||
yorigcolor = y.color;
|
||||
x = y.right;
|
||||
if (y.p == z)
|
||||
x.p = y;
|
||||
else {
|
||||
transplant(y, y.right);
|
||||
y.right = z.right;
|
||||
y.right.p = y;
|
||||
}
|
||||
transplant(z, y);
|
||||
y.left = z.left;
|
||||
y.left.p = y;
|
||||
y.color = z.color;
|
||||
}
|
||||
if (yorigcolor == B)
|
||||
deleteFixup(x);
|
||||
return true;
|
||||
}
|
||||
|
||||
void deleteFixup(Node x) {
|
||||
while (x != root && x.color == B) {
|
||||
if (x == x.p.left) {
|
||||
Node w = x.p.right;
|
||||
if (w.color == R) {
|
||||
w.color = B;
|
||||
x.p.color = R;
|
||||
rotateLeft(x.p);
|
||||
w = x.p.right;
|
||||
}
|
||||
if (w.left.color == B && w.right.color == B) {
|
||||
w.color = R;
|
||||
x = x.p;
|
||||
continue;
|
||||
} else if (w.right.color == B) {
|
||||
w.left.color = B;
|
||||
w.color = R;
|
||||
rotateRight(w);
|
||||
w = x.p.right;
|
||||
}
|
||||
if (w.right.color == R) {
|
||||
w.color = x.p.color;
|
||||
x.p.color = B;
|
||||
w.right.color = B;
|
||||
rotateLeft(x.p);
|
||||
x = root;
|
||||
}
|
||||
} else {
|
||||
Node w = x.p.left;
|
||||
if (w.color == R) {
|
||||
w.color = B;
|
||||
x.p.color = R;
|
||||
rotateRight(x.p);
|
||||
w = x.p.left;
|
||||
}
|
||||
if (w.right.color == B && w.left.color == B) {
|
||||
w.color = R;
|
||||
x = x.p;
|
||||
continue;
|
||||
} else if (w.left.color == B) {
|
||||
w.right.color = B;
|
||||
w.color = R;
|
||||
rotateLeft(w);
|
||||
w = x.p.left;
|
||||
}
|
||||
if (w.left.color == R) {
|
||||
w.color = x.p.color;
|
||||
x.p.color = B;
|
||||
w.left.color = B;
|
||||
rotateRight(x.p);
|
||||
x = root;
|
||||
}
|
||||
}
|
||||
}
|
||||
x.color = B;
|
||||
}
|
||||
void deleteFixup(Node x) {
|
||||
while (x != root && x.color == B) {
|
||||
if (x == x.p.left) {
|
||||
Node w = x.p.right;
|
||||
if (w.color == R) {
|
||||
w.color = B;
|
||||
x.p.color = R;
|
||||
rotateLeft(x.p);
|
||||
w = x.p.right;
|
||||
}
|
||||
if (w.left.color == B && w.right.color == B) {
|
||||
w.color = R;
|
||||
x = x.p;
|
||||
continue;
|
||||
} else if (w.right.color == B) {
|
||||
w.left.color = B;
|
||||
w.color = R;
|
||||
rotateRight(w);
|
||||
w = x.p.right;
|
||||
}
|
||||
if (w.right.color == R) {
|
||||
w.color = x.p.color;
|
||||
x.p.color = B;
|
||||
w.right.color = B;
|
||||
rotateLeft(x.p);
|
||||
x = root;
|
||||
}
|
||||
} else {
|
||||
Node w = x.p.left;
|
||||
if (w.color == R) {
|
||||
w.color = B;
|
||||
x.p.color = R;
|
||||
rotateRight(x.p);
|
||||
w = x.p.left;
|
||||
}
|
||||
if (w.right.color == B && w.left.color == B) {
|
||||
w.color = R;
|
||||
x = x.p;
|
||||
continue;
|
||||
} else if (w.left.color == B) {
|
||||
w.right.color = B;
|
||||
w.color = R;
|
||||
rotateLeft(w);
|
||||
w = x.p.left;
|
||||
}
|
||||
if (w.left.color == R) {
|
||||
w.color = x.p.color;
|
||||
x.p.color = B;
|
||||
w.left.color = B;
|
||||
rotateRight(x.p);
|
||||
x = root;
|
||||
}
|
||||
}
|
||||
}
|
||||
x.color = B;
|
||||
}
|
||||
|
||||
public void insertDemo() {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
while (true) {
|
||||
System.out.println("Add items");
|
||||
public void insertDemo() {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
while (true) {
|
||||
System.out.println("Add items");
|
||||
|
||||
int item;
|
||||
Node node;
|
||||
int item;
|
||||
Node node;
|
||||
|
||||
item = scan.nextInt();
|
||||
while (item != -999) {
|
||||
node = new Node(item);
|
||||
insert(node);
|
||||
item = scan.nextInt();
|
||||
}
|
||||
printTree(root);
|
||||
System.out.println("Pre order");
|
||||
printTreepre(root);
|
||||
break;
|
||||
}
|
||||
}
|
||||
item = scan.nextInt();
|
||||
while (item != -999) {
|
||||
node = new Node(item);
|
||||
insert(node);
|
||||
item = scan.nextInt();
|
||||
}
|
||||
printTree(root);
|
||||
System.out.println("Pre order");
|
||||
printTreepre(root);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteDemo() {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
System.out.println("Delete items");
|
||||
int item;
|
||||
Node node;
|
||||
item = scan.nextInt();
|
||||
node = new Node(item);
|
||||
System.out.print("Deleting item " + item);
|
||||
if (delete(node)) {
|
||||
System.out.print(": deleted!");
|
||||
} else {
|
||||
System.out.print(": does not exist!");
|
||||
}
|
||||
public void deleteDemo() {
|
||||
Scanner scan = new Scanner(System.in);
|
||||
System.out.println("Delete items");
|
||||
int item;
|
||||
Node node;
|
||||
item = scan.nextInt();
|
||||
node = new Node(item);
|
||||
System.out.print("Deleting item " + item);
|
||||
if (delete(node)) {
|
||||
System.out.print(": deleted!");
|
||||
} else {
|
||||
System.out.print(": does not exist!");
|
||||
}
|
||||
|
||||
System.out.println();
|
||||
printTree(root);
|
||||
System.out.println("Pre order");
|
||||
printTreepre(root);
|
||||
}
|
||||
System.out.println();
|
||||
printTree(root);
|
||||
System.out.println("Pre order");
|
||||
printTreepre(root);
|
||||
}
|
||||
}
|
@ -1,10 +1,10 @@
|
||||
package DataStructures.Trees;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
*
|
||||
*/
|
||||
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||
*/
|
||||
|
||||
|
||||
// Driver Program
|
||||
@ -38,13 +38,13 @@ public class TreeTraversal {
|
||||
}
|
||||
|
||||
/**
|
||||
* The Node class which initializes a Node of a tree
|
||||
* Consists of all 4 traversal methods: printInOrder, printPostOrder, printPreOrder & printLevelOrder
|
||||
* printInOrder: LEFT -> ROOT -> RIGHT
|
||||
* printPreOrder: ROOT -> LEFT -> RIGHT
|
||||
* printPostOrder: LEFT -> RIGHT -> ROOT
|
||||
* printLevelOrder: Prints by level (starting at root), from left to right.
|
||||
*/
|
||||
* The Node class which initializes a Node of a tree
|
||||
* Consists of all 4 traversal methods: printInOrder, printPostOrder, printPreOrder & printLevelOrder
|
||||
* printInOrder: LEFT -> ROOT -> RIGHT
|
||||
* printPreOrder: ROOT -> LEFT -> RIGHT
|
||||
* printPostOrder: LEFT -> RIGHT -> ROOT
|
||||
* printLevelOrder: Prints by level (starting at root), from left to right.
|
||||
*/
|
||||
class Node {
|
||||
Node left, right;
|
||||
int data;
|
||||
@ -53,20 +53,17 @@ class Node {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public void insert (int value) {
|
||||
public void insert(int value) {
|
||||
if (value < data) {
|
||||
if (left == null) {
|
||||
left = new Node(value);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
left.insert(value);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (right == null) {
|
||||
right = new Node(value);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
right.insert(value);
|
||||
}
|
||||
}
|
||||
@ -103,9 +100,9 @@ class Node {
|
||||
}
|
||||
|
||||
/**
|
||||
* O(n) time algorithm.
|
||||
* Uses O(n) space to store nodes in a queue to aid in traversal.
|
||||
*/
|
||||
* O(n) time algorithm.
|
||||
* Uses O(n) space to store nodes in a queue to aid in traversal.
|
||||
*/
|
||||
public void printLevelOrder() {
|
||||
LinkedList<Node> queue = new LinkedList<>();
|
||||
queue.add(this);
|
||||
|
@ -1,10 +1,11 @@
|
||||
//Trie Data structure implementation without any libraries */
|
||||
package DataStructures.Trees;
|
||||
|
||||
/**
|
||||
* Trie Data structure implementation without any libraries
|
||||
*
|
||||
* @author Dheeraj Kumar Barnwal (https://github.com/dheeraj92)
|
||||
*
|
||||
*/
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class TrieImp {
|
||||
@ -13,34 +14,37 @@ public class TrieImp {
|
||||
TrieNode[] child;
|
||||
boolean end;
|
||||
|
||||
public TrieNode(){
|
||||
public TrieNode() {
|
||||
child = new TrieNode[26];
|
||||
end = false;
|
||||
}
|
||||
}
|
||||
|
||||
private final TrieNode root;
|
||||
public TrieImp(){
|
||||
|
||||
public TrieImp() {
|
||||
root = new TrieNode();
|
||||
}
|
||||
|
||||
public void insert(String word){
|
||||
public void insert(String word) {
|
||||
TrieNode currentNode = root;
|
||||
for(int i=0; i < word.length();i++){
|
||||
TrieNode node = currentNode.child[word.charAt(i)-'a'];
|
||||
if(node == null){
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
TrieNode node = currentNode.child[word.charAt(i) - 'a'];
|
||||
if (node == null) {
|
||||
node = new TrieNode();
|
||||
currentNode.child[word.charAt(i)-'a']=node;
|
||||
currentNode.child[word.charAt(i) - 'a'] = node;
|
||||
}
|
||||
currentNode = node;
|
||||
}
|
||||
currentNode.end = true;
|
||||
}
|
||||
public boolean search(String word){
|
||||
|
||||
public boolean search(String word) {
|
||||
TrieNode currentNode = root;
|
||||
for(int i=0;i<word.length();i++){
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
TrieNode node = currentNode.child[ch-'a'];
|
||||
if(node == null){
|
||||
TrieNode node = currentNode.child[ch - 'a'];
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
currentNode = node;
|
||||
@ -48,29 +52,31 @@ public class TrieImp {
|
||||
return currentNode.end;
|
||||
}
|
||||
|
||||
public boolean delete(String word){
|
||||
public boolean delete(String word) {
|
||||
TrieNode currentNode = root;
|
||||
for(int i=0;i<word.length();i++){
|
||||
for (int i = 0; i < word.length(); i++) {
|
||||
char ch = word.charAt(i);
|
||||
TrieNode node = currentNode.child[ch-'a'];
|
||||
if(node == null){
|
||||
TrieNode node = currentNode.child[ch - 'a'];
|
||||
if (node == null) {
|
||||
return false;
|
||||
}
|
||||
currentNode = node;
|
||||
}
|
||||
if(currentNode.end == true){
|
||||
if (currentNode.end == true) {
|
||||
currentNode.end = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static void sop(String print){
|
||||
public static void sop(String print) {
|
||||
System.out.println(print);
|
||||
}
|
||||
|
||||
//Regex to check if word contains only a-z character
|
||||
public static boolean isValid(String word){
|
||||
/**
|
||||
* Regex to check if word contains only a-z character
|
||||
*/
|
||||
public static boolean isValid(String word) {
|
||||
return word.matches("^[a-z]+$");
|
||||
}
|
||||
|
||||
@ -80,40 +86,40 @@ public class TrieImp {
|
||||
@SuppressWarnings("resource")
|
||||
Scanner scan = new Scanner(System.in);
|
||||
sop("string should contain only a-z character for all operation");
|
||||
while(true){
|
||||
while (true) {
|
||||
sop("1. Insert\n2. Search\n3. Delete\n4. Quit");
|
||||
try{
|
||||
try {
|
||||
int t = scan.nextInt();
|
||||
switch (t) {
|
||||
case 1:
|
||||
word = scan.next();
|
||||
if(isValid(word))
|
||||
if (isValid(word))
|
||||
obj.insert(word);
|
||||
else
|
||||
sop("Invalid string: allowed only a-z");
|
||||
break;
|
||||
case 2:
|
||||
word = scan.next();
|
||||
boolean resS=false;
|
||||
if(isValid(word))
|
||||
boolean resS = false;
|
||||
if (isValid(word))
|
||||
resS = obj.search(word);
|
||||
else
|
||||
sop("Invalid string: allowed only a-z");
|
||||
if(resS)
|
||||
if (resS)
|
||||
sop("word found");
|
||||
else
|
||||
sop("word not found");
|
||||
break;
|
||||
case 3:
|
||||
word = scan.next();
|
||||
boolean resD=false;
|
||||
if(isValid(word))
|
||||
boolean resD = false;
|
||||
if (isValid(word))
|
||||
resD = obj.delete(word);
|
||||
else
|
||||
sop("Invalid string: allowed only a-z");
|
||||
if(resD){
|
||||
if (resD) {
|
||||
sop("word got deleted successfully");
|
||||
}else{
|
||||
} else {
|
||||
sop("word not found");
|
||||
}
|
||||
break;
|
||||
@ -125,7 +131,7 @@ public class TrieImp {
|
||||
sop("Input int from 1-4");
|
||||
break;
|
||||
}
|
||||
}catch(Exception e){
|
||||
} catch (Exception e) {
|
||||
String badInput = scan.next();
|
||||
sop("This is bad input: " + badInput);
|
||||
}
|
||||
|
@ -1,38 +1,37 @@
|
||||
class Node
|
||||
{
|
||||
int data;
|
||||
Node left, right;
|
||||
|
||||
public Node(int item)
|
||||
{
|
||||
data = item;
|
||||
left = right = null;
|
||||
package DataStructures.Trees;
|
||||
|
||||
public class ValidBSTOrNot {
|
||||
|
||||
class Node {
|
||||
int data;
|
||||
Node left, right;
|
||||
|
||||
public Node(int item) {
|
||||
data = item;
|
||||
left = right = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class ValidBSTOrNot
|
||||
{
|
||||
|
||||
//Root of the Binary Tree
|
||||
Node root;
|
||||
|
||||
/* can give min and max value according to your code or
|
||||
can write a function to find min and max value of tree. */
|
||||
|
||||
|
||||
/* returns true if given search tree is binary
|
||||
search tree (efficient version) */
|
||||
boolean isBST() {
|
||||
boolean isBST() {
|
||||
return isBSTUtil(root, Integer.MIN_VALUE,
|
||||
Integer.MAX_VALUE);
|
||||
Integer.MAX_VALUE);
|
||||
}
|
||||
|
||||
|
||||
/* Returns true if the given tree is a BST and its
|
||||
values are >= min and <= max. */
|
||||
boolean isBSTUtil(Node node, int min, int max)
|
||||
{
|
||||
boolean isBSTUtil(Node node, int min, int max) {
|
||||
/* an empty tree is BST */
|
||||
if (node == null)
|
||||
return true;
|
||||
|
||||
|
||||
/* false if this node violates the min/max constraints */
|
||||
if (node.data < min || node.data > max)
|
||||
return false;
|
||||
@ -40,23 +39,7 @@ public class ValidBSTOrNot
|
||||
/* otherwise check the subtrees recursively
|
||||
tightening the min/max constraints */
|
||||
// Allow only distinct values
|
||||
return (isBSTUtil(node.left, min, node.data-1) &&
|
||||
isBSTUtil(node.right, node.data+1, max));
|
||||
}
|
||||
|
||||
/* Driver program to test above functions */
|
||||
public static void main(String args[])
|
||||
{
|
||||
ValidBSTOrNot tree = new ValidBSTOrNot();
|
||||
tree.root = new Node(4);
|
||||
tree.root.left = new Node(2);
|
||||
tree.root.right = new Node(5);
|
||||
tree.root.left.left = new Node(1);
|
||||
tree.root.left.right = new Node(3);
|
||||
|
||||
if (tree.isBST())
|
||||
System.out.println("IS BST");
|
||||
else
|
||||
System.out.println("Not a BST");
|
||||
return (isBSTUtil(node.left, min, node.data - 1) &&
|
||||
isBSTUtil(node.right, node.data + 1, max));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user