Professional Documents
Culture Documents
Advanced Data Structure
Advanced Data Structure
ENGINEERING
New Chandigarh, District Mohali Abhipur, Punjab
mport java.util.Scanner;
char ch;
/** Perform LinearProbingHashTable operations **/
do
{
System.out.println("\nHash Table Operations\n");
System.out.println("1. insert ");
System.out.println("2. remove");
System.out.println("3. get");
System.out.println("4. clear");
System.out.println("5. size");
/*
* Java Program to Implement Binary Tree
*/
import java.util.Scanner;
/* Class BTNode */
class BTNode
{
BTNode left, right;
int data;
/* Constructor */
public BTNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public BTNode(int n)
{
left = null;
right = null;
data = n;
}
/* Function to set left node */
public void setLeft(BTNode n)
{
left = n;
}
/* Function to set right node */
public void setRight(BTNode n)
{
right = n;
}
/* Function to get left node */
public BTNode getLeft()
{
return left;
}
/* Function to get right node */
public BTNode getRight()
{
return right;
}
/* Function to set data to node */
public void setData(int d)
{
data = d;
}
/* Function to get data from node */
public int getData()
{
return data;
}
}
/* Class BT */
class BT
{
private BTNode root;
/* Constructor */
public BT()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Functions to insert data */
public void insert(int data)
{
root = insert(root, data);
}
/* Function to insert data recursively */
private BTNode insert(BTNode node, int data)
{
if (node == null)
node = new BTNode(data);
else
{
if (node.getRight() == null)
node.right = insert(node.right, data);
else
node.left = insert(node.left, data);
}
return node;
}
/* Function to count number of nodes */
public int countNodes()
{
return countNodes(root);
}
/* Function to count number of nodes recursively */
private int countNodes(BTNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
/* Function to search for an element */
public boolean search(int val)
{
return search(root, val);
}
/* Function to search for an element recursively */
private boolean search(BTNode r, int val)
{
if (r.getData() == val)
return true;
if (r.getLeft() != null)
if (search(r.getLeft(), val))
return true;
if (r.getRight() != null)
if (search(r.getRight(), val))
return true;
return false;
}
/* Function for inorder traversal */
public void inorder()
{
inorder(root);
}
private void inorder(BTNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
/* Function for preorder traversal */
public void preorder()
{
preorder(root);
}
private void preorder(BTNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
/* Function for postorder traversal */
public void postorder()
{
postorder(root);
}
private void postorder(BTNode r)
{
if (r != null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
/* Class BinaryTree */
public class BinaryTree
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of BT */
BT bt = new BT();
/* Perform tree operations */
System.out.println("Binary Tree Test\n");
char ch;
do
{
System.out.println("\nBinary Tree Operations\n");
System.out.println("1. insert ");
System.out.println("2. search");
System.out.println("3. count nodes");
System.out.println("4. check empty");
import java.util.Scanner;
/* Class AVLNode */
class AVLNode
{
AVLNode left, right;
int data;
int height;
/* Constructor */
public AVLNode()
{
left = null;
right = null;
data = 0;
height = 0;
}
/* Constructor */
public AVLNode(int n)
{
left = null;
right = null;
data = n;
height = 0;
}
}
/* Class AVLTree */
class AVLTree
{
private AVLNode root;
/* Constructor */
public AVLTree()
{
root = null;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return root == null;
}
/* Make the tree logically empty */
public void makeEmpty()
{
root = null;
}
/* Function to insert data */
public void insert(int data)
{
root = insert(data, root);
}
/* Function to get height of node */
private int height(AVLNode t )
{
return t == null ? -1 : t.height;
}
/* Function to max of left/right node */
private int max(int lhs, int rhs)
{
return lhs > rhs ? lhs : rhs;
}
/* Function to insert data recursively */
private AVLNode insert(int x, AVLNode t)
{
if (t == null)
t = new AVLNode(x);
else if (x < t.data)
{
t.left = insert( x, t.left );
if( height( t.left ) - height( t.right ) == 2 )
if( x < t.left.data )
t = rotateWithLeftChild( t );
else
t = doubleWithLeftChild( t );
}
else if( x > t.data )
{
t.right = insert( x, t.right );
if( height( t.right ) - height( t.left ) == 2 )
if( x > t.right.data)
t = rotateWithRightChild( t );
else
t = doubleWithRightChild( t );
}
else
; // Duplicate; do nothing
t.height = max( height( t.left ), height( t.right ) ) + 1;
return t;
}
/* Rotate binary tree node with left child */
private AVLNode rotateWithLeftChild(AVLNode k2)
{
AVLNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
k2.height = max( height( k2.left ), height( k2.right ) ) + 1;
k1.height = max( height( k1.left ), k2.height ) + 1;
return k1;
}
import java.util.Scanner;
/* Class Node */
class RedBlackNode
{
RedBlackNode left, right;
int element;
int color;
/* Constructor */
public RedBlackNode(int theElement)
{
this( theElement, null, null );
}
/* Constructor */
public RedBlackNode(int theElement, RedBlackNode lt, RedBlackNode rt)
{
left = lt;
right = rt;
element = theElement;
color = 1;
}
}
/* Class RBTree */
class RBTree
{
private RedBlackNode current;
private RedBlackNode parent;
private RedBlackNode grand;
private RedBlackNode great;
private RedBlackNode header;
private static RedBlackNode nullNode;
/* static initializer for nullNode */
static
{
nullNode = new RedBlackNode(0);
nullNode.left = nullNode;
nullNode.right = nullNode;
}
/* Black - 1 RED - 0 */
static final int BLACK = 1;
static final int RED = 0;
/* Constructor */
public RBTree(int negInf)
{
header = new RedBlackNode(negInf);
header.left = nullNode;
header.right = nullNode;
}
/* Function to check if tree is empty */
public boolean isEmpty()
{
return header.right == nullNode;
}
/* Make the tree logically empty */
public void makeEmpty()
{
header.right = nullNode;
}
/* Function to insert item */
public void insert(int item )
{
current = parent = grand = header;
nullNode.element = item;
while (current.element != item)
{
great = grand;
grand = parent;
parent = current;
current = item < current.element ? current.left : current.right;
// Check if two red children and fix if so
if (current.left.color == RED && current.right.color == RED)
handleReorient( item );
}
// Insertion fails if already present
if (current != nullNode)
return;
current = new RedBlackNode(item, nullNode, nullNode);
// Attach to parent
if (item < parent.element)
parent.left = current;
else
parent.right = current;
handleReorient( item );
}
private void handleReorient(int item)
{
// Do the color flip
current.color = RED;
current.left.color = BLACK;
current.right.color = BLACK;
if (parent.color == RED)
{
// Have to rotate
grand.color = RED;
if (item < grand.element != item < parent.element)
parent = rotate( item, grand ); // Start dbl rotate
current = rotate(item, great );
current.color = BLACK;
}
// Make root black
header.right.color = BLACK;
}
private RedBlackNode rotate(int item, RedBlackNode parent)
{
if(item < parent.element)
return parent.left = item < parent.left.element ? rotateWithLeftChild(parent.left) :
rotateWithRightChild(parent.left) ;
else
return parent.right = item < parent.right.element ? rotateWithLeftChild(parent.right) :
rotateWithRightChild(parent.right);
}
/* Rotate binary tree node with left child */
private RedBlackNode rotateWithLeftChild(RedBlackNode k2)
{
RedBlackNode k1 = k2.left;
k2.left = k1.right;
k1.right = k2;
return k1;
}
/* Rotate binary tree node with right child */
private RedBlackNode rotateWithRightChild(RedBlackNode k1)
{
RedBlackNode k2 = k1.right;
k1.right = k2.left;
k2.left = k1;
return k2;
}
/* Functions to count number of nodes */
public int countNodes()
{
return countNodes(header.right);
}
private int countNodes(RedBlackNode r)
{
if (r == nullNode)
return 0;
else
{
int l = 1;
l += countNodes(r.left);
l += countNodes(r.right);
return l;
}
}
/* Functions to search for an element */
public boolean search(int val)
{
return search(header.right, val);
}
private boolean search(RedBlackNode r, int val)
{
boolean found = false;
while ((r != nullNode) && !found)
{
int rval = r.element;
if (val < rval)
r = r.left;
else if (val > rval)
r = r.right;
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
/* Function for inorder traversal */
public void inorder()
{
inorder(header.right);
}
private void inorder(RedBlackNode r)
{
if (r != nullNode)
{
inorder(r.left);
char c = 'B';
if (r.color == 0)
c = 'R';
System.out.print(r.element +""+c+" ");
inorder(r.right);
}
}
/* Function for preorder traversal */
public void preorder()
{
preorder(header.right);
}
private void preorder(RedBlackNode r)
{
if (r != nullNode)
{
char c = 'B';
if (r.color == 0)
c = 'R';
System.out.print(r.element +""+c+" ");
preorder(r.left);
preorder(r.right);
}
}
/* Function for postorder traversal */
public void postorder()
{
postorder(header.right);
}
private void postorder(RedBlackNode r)
{
if (r != nullNode)
{
postorder(r.left);
postorder(r.right);
char c = 'B';
if (r.color == 0)
c = 'R';
System.out.print(r.element +""+c+" ");
}
}
}
/* Class RedBlackTreeTest */
public class RedBlackTreeTest
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of RedBlack Tree */
RBTree rbt = new RBTree(Integer.MIN_VALUE);
System.out.println("Red Black Tree Test\n");
char ch;
/* Perform tree operations */
do
{
System.out.println("\nRed Black Tree Operations\n");
System.out.println("1. insert ");
System.out.println("2. search");
System.out.println("3. count nodes");
System.out.println("4. check empty");
System.out.println("5. clear tree");
1. /**
2. ** Java Program to implement Knuth Morris Pratt Algorithm
3. **/
4.
5. import java.io.BufferedReader;
6. import java.io.InputStreamReader;
7. import java.io.IOException;
8.
9. /** Class KnuthMorrisPratt **/
10. public class KnuthMorrisPratt
11. {
12. /** Failure array **/
13. private int[] failure;
14. /** Constructor **/
15. public KnuthMorrisPratt(String text, String pat)
16. {
17. /** pre construct failure array for a pattern **/
18. failure = new int[pat.length()];
19. fail(pat);
20. /** find match **/
21. int pos = posMatch(text, pat);
22. if (pos == -1)
23. System.out.println("\nNo match found");
24. else
25. System.out.println("\nMatch found at index "+ pos);
26. }
27. /** Failure function for a pattern **/
28. private void fail(String pat)
29. {
30. int n = pat.length();
31. failure[0] = -1;
32. for (int j = 1; j < n; j++)
33. {
34. int i = failure[j - 1];
35. while ((pat.charAt(j) != pat.charAt(i + 1)) && i >= 0)
36. i = failure[i];
37. if (pat.charAt(j) == pat.charAt(i + 1))
38. failure[j] = i + 1;
39. else
40. failure[j] = -1;
41. }
42. }
43. /** Function to find match for a pattern **/
44. private int posMatch(String text, String pat)
45. {
46. int i = 0, j = 0;
47. int lens = text.length();
48. int lenp = pat.length();
49. while (i < lens && j < lenp)
50. {
51. if (text.charAt(i) == pat.charAt(j))
52. {
53. i++;
54. j++;
55. }
56. else if (j == 0)
57. i++;
58. else
59. j = failure[j - 1] + 1;
60. }
61. return ((j == lenp) ? (i - lenp) : -1);
62. }
63. /** Main Function **/
64. public static void main(String[] args) throws IOException
65. {
66. BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
67. System.out.println("Knuth Morris Pratt Test\n");
68. System.out.println("\nEnter Text\n");
69. String text = br.readLine();
70. System.out.println("\nEnter Pattern\n");
71. String pattern = br.readLine();
72. KnuthMorrisPratt kmp = new KnuthMorrisPratt(text, pattern);
73. }
74. }
EXP 7: WAP to perform string matching using Boyer-Moore algorithm.
/**
**/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
if (pos == -1)
System.out.println("\nNo Match\n");
else
if (pattern.length == 0)
return 0;
if (j == 0)
return i;
return -1;
/** Makes the jump table based on the mismatched character information **/
table[i] = pattern.length;
table[pattern[i]] = pattern.length - 1 - i;
return table;
}
/** Makes the jump table based on the scan offset which mismatch occurs. **/
if (isPrefix(pattern, i + 1))
lastPrefixPosition = i + 1;
return table;
if (pattern[i] != pattern[j])
return false;
return true;
/** function to returns the maximum length of the substring ends at p and is a suffix **/
int len = 0;
len += 1;
return len;
System.out.println("\nEnter Text\n");
System.out.println("\nEnter Pattern\n");
bm.findPattern(text, pattern);
}
EXP 8:WAP to implement 2-D range search over computational geometry problem
//This is a java program to construct a KD tree for two dimensional static data
import java.io.IOException;
class KD2DNode
{
int axis;
double[] x;
int id;
boolean checked;
boolean orientation;
KD2DNode Parent;
KD2DNode Left;
KD2DNode Right;
return newNode;
}
return true;
}
class KD2DTree
{
KD2DNode Root;
double d_min;
KD2DNode nearest_neighbour;
int KD_id;
int nList;
KD2DNode CheckedNodes[];
int checked_nodes;
KD2DNode List[];
public KD2DTree(int i)
{
Root = null;
KD_id = 1;
nList = 0;
List = new KD2DNode[i];
CheckedNodes = new KD2DNode[i];
max_boundary = new boolean[2];
min_boundary = new boolean[2];
x_min = new double[2];
x_max = new double[2];
}
if (Root == null)
{
Root = new KD2DNode(x, 0);
Root.id = KD_id++;
List[nList++] = Root;
} else
{
KD2DNode pNode;
if ((pNode = Root.Insert(x)) != null)
{
pNode.id = KD_id++;
List[nList++] = pNode;
}
}
return true;
}
checked_nodes = 0;
KD2DNode parent = Root.FindParent(x);
nearest_neighbour = parent;
d_min = Root.distance2(x, parent.x, 2);
;
search_parent(parent, x);
uncheck();
return nearest_neighbour;
}
CheckedNodes[checked_nodes++] = node;
node.checked = true;
set_bounding_cube(node, x);
int dim = node.axis;
double d = node.x[dim] - x[dim];
if (d * d > d_min)
{
if (node.x[dim] > x[dim])
check_subtree(node.Left, x);
else
check_subtree(node.Right, x);
} else
{
check_subtree(node.Left, x);
check_subtree(node.Right, x);
}
}
if (d < d_min)
{
d_min = d;
nearest_neighbour = node;
}
}
return search_root;
}
x[0] = 0.0;
x[1] = 0.0;
kdt.add(x);
x[0] = 3.3;
x[1] = 1.5;
kdt.add(x);
x[0] = 4.7;
x[1] = 11.1;
kdt.add(x);
x[0] = 5.0;
x[1] = 12.3;
kdt.add(x);
x[0] = 5.1;
x[1] = 1.2;
kdt.add(x);
package practice;
// Java program to create a custom tree from a given set of links.
// The main class that represents tree and has main method
public class Tree {
// Constructor
public Tree(TreeNode root) { this.root = root; }
String [] links2 = {"a b", "a g", "b c", "c d", "d e", "c f",
"z y", "y x", "x w"};
System.out.println("------------ Forest 2 ----------------");
printForest(links2);
}
}
// Constructor
public TreeNode(char c) { this.c = c; this.children = new TreeNode[26];}