Skip to content

Commit

Permalink
Merge pull request #1148 from KumarKelashMeghwar/master
Browse files Browse the repository at this point in the history
close #280. Added Java programs
  • Loading branch information
fineanmol authored Oct 23, 2021
2 parents 4d767ba + 37b772b commit a67d58d
Show file tree
Hide file tree
Showing 3 changed files with 510 additions and 0 deletions.
93 changes: 93 additions & 0 deletions AVLTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package tree;

/**
*
* @author Zohaib Hassan Soomro
*/
public class AVLTree {

private int key,height;
private AVLTree left,right;
public static final AVLTree NIL=new AVLTree();

public AVLTree(int key){
this.key=key;
left=right=NIL;
}
private AVLTree(){
left=right=this;
height=-1;
}
private AVLTree(int key,AVLTree left,AVLTree right){
this.key=key; this.left=left; this.right=right;
height= 1 + Math.max(left.height,right.height);
}
public int size(){
if(this==NIL)
return 0;
return 1 + left.size() +right.size();
}

public String toString(){
if(this==NIL)
return "";
return left+" "+key+" "+right;
}

public boolean add(int key){
int oldSize=size();
grow(key);
return size()>oldSize;
}
public AVLTree grow(int key){
if(this==NIL)
return new AVLTree(key);
if(key==this.key) return this; //avoid key duplication
if(key<this.key) left=left.grow(key);
else
right=right.grow(key);
rebalance();
height= 1 + Math.max(left.height,right.height);
return this;
}
private void rebalance(){
if (right.height>left.height+1) {
if(right.left.height >right.right.height)
right.rotateRight();
rotateLeft();
}
else if (left.height>right.height+1) {
if(left.right.height > left.left.height)
left.rotateLeft();
rotateRight();
}
}

public void rotateRight(){
right=new AVLTree(key,left.right,right);
key=left.key;
left=left.left;
}
public void rotateLeft(){
left=new AVLTree(key,left,right.left);
key=right.key;
right=right.right;
}

public static void main(String[] args) {
AVLTree tree=new AVLTree(4);
tree.add(3);
tree.add(13);
tree.add(11);
tree.add(9);
tree.add(45);
tree.add(5);
System.out.println(tree.height);
System.out.println(tree);
}
}
255 changes: 255 additions & 0 deletions BinarySearchTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package tree;

/**
*
* @author Zohaib Hassan Soomro
*/
public class BinarySearchTree {
int key;
BinarySearchTree left, right;

BinarySearchTree(int key) {
this.key = key;
}

BinarySearchTree(int key, BinarySearchTree left, BinarySearchTree right) {
this.key = key;
this.left = left;
this.right = right;
}

/*//in-order traversal of Binary Tree
public void inOrder() {
if(this==null)
System.out.println("");
if(this.left==null)
System.out.print(".");
else{
System.out.print(left.key+"=>");
left.inOrder();
}
System.out.print(key);
if(this.right==null)
System.out.println(".");
else{
System.out.println("<="+right.key);
right.inOrder();
}
}*/

//in-order traversal of Binary Tree
public String toString() {
if(this==null)
return " ";
String buf="";
if(left!=null)
buf+=left+" ";
buf+=key;
if (right!=null)
buf+=" "+right;
return buf;
}

public String preOrder() {
if(this==null)
return " ";
String buf=key+"";
if(left!=null)
buf+=" "+left.preOrder();
if (right!=null)
buf+=" "+right.preOrder();
return buf;
}
public String postOrder() {
if(this==null)
return " ";
String buf="";
if(left!=null)
buf+=left.postOrder()+" ";
if (right!=null)
buf+=right.postOrder()+" ";
buf+=key;
return buf;
}
public void traverse(){
System.out.println("Inorder : "+toString());
System.out.println("Preorder : "+preOrder());
System.out.println("Postorder: "+postOrder());
}

public BinarySearchTree insert(int key){
return insert(this,key);
}
private BinarySearchTree insert(BinarySearchTree root, int key){
if(root==null)
return new BinarySearchTree(key);
if(key<root.key){
root.left=insert(root.left,key);
}
if(key>root.key){
root.right=insert(root.right,key);
}
else if(key==root.key){
System.out.println("Duplicate values not allowed here!");
return root;
}
return root;
}

public boolean search(int key){
return search(this,key);
}
private boolean search(BinarySearchTree root, int key){
if(root==null)
return false;
if(key<root.key){
return search(root.left,key);
}
if(key>root.key){
return search(root.right,key);
}
else if(key==root.key){
return true;
}
return false;
}

public boolean delete(int key){
return delete(this,key);
}

public boolean delete(BinarySearchTree root,int key){
if(root==null)
return false;
if(key<root.key)
return left.delete(left,key);
if(key>root.key)
return right.delete(right,key);
if(root.left==null && root.right==null){
root=null;
return true;
}
if (root.right==null) {
root=root.left;
root.right=root.left.right;
root.left=root.left.left;
return true;
}

if (root.left==null) {
root=root.right;
root.left=root.right.left;
root.right=root.right.right;
return true;
}

if(root.right.left==null){
root=root.right;
root.right=root.right.right;
return true;
}

root=deleteMinimum(root.right);
return true;
}

private BinarySearchTree deleteMinimum(BinarySearchTree root){
if(root.left.left==null){
BinarySearchTree temp=root.left;
root.left=root.left.right;
return temp;
}
return root.deleteMinimum(root.left);
}
/*private BinarySearchTree delete(BinarySearchTree root, int key){
if(root==null)
return root;
if(!search(root,key)){
System.out.println("Value not found!");
return root;
}
if(key<root.key)
return delete(root.left,key);
if(key>root.key)
return delete(root.right,key);
if(key==root.key && root.left==null && root.right==null){
BinarySearchTree p=getParent(root);
if(p.left==root)
return p.left=root.left; //return root=root.right
else if(p.right==root)
return p.right=root.right;
}
if(root.right==null){
BinarySearchTree p=getParent(root);
if(p.left==root)
return p.left=root.left; //return root=root.right
else if(p.right==root)
return p.right=root.left;
}
if(root.left==null){
BinarySearchTree p=getParent(root);
if(p.left==root)
return p.left=root.right; //return root=root.right
else if(p.right==root)
return p.right=root.right;
}
BinarySearchTree p=getParent(root);
if(p.left==root)
return p.left=deleteMinimum(root.right);//return root=root.right
else
return p.right=deleteMinimum(root.right);
}
private BinarySearchTree getParent(BinarySearchTree node){
BinarySearchTree p=this;
if(p==node)
return null; //there's no parent
if(p.left==node || p.right==node)
return p;
if(p.left!=null)
p=p.left.getParent(node);
if(p.right!=null)
p=p.right.getParent(node);
return p;
}*/

/*private BinarySearchTree deleteMinimum(BinarySearchTree root){
if(root==null)
return root;
if(root.left==null){
BinarySearchTree temp=root;
root=root.right;
return temp;
}
if(root.left.left==null && root.left.right==null){
BinarySearchTree temp=root;
root=root.left;
return temp;
}
return deleteMinimum(root.left);
}*/

public static void main(String[] args) {

BinarySearchTree treeA=new BinarySearchTree(3);
treeA=treeA.insert(9);
treeA=treeA.insert(2);
treeA=treeA.insert(8);
treeA=treeA.insert(6);
treeA=treeA.insert(4);
treeA=treeA.insert(10);
treeA=treeA.insert(11);
treeA=treeA.insert(56);
//treeA.inOrder();
System.out.println(treeA);
treeA.delete(4);
System.out.println(treeA);
//System.out.println(treeA.search(57));


}
}
Loading

0 comments on commit a67d58d

Please sign in to comment.