Skip to content

Commit cfbe3d7

Browse files
committed
Tokarski project added
1 parent 04896ea commit cfbe3d7

16 files changed

+4292
-0
lines changed
152 Bytes
Binary file not shown.

tokarski/.Main.java.kate-swp

229 Bytes
Binary file not shown.

tokarski/BinarySearchTree.java

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/**
2+
* @author Konrad S³oniewski, ks321221
3+
* @version 1.0, 29 Apr 2012
4+
*/
5+
6+
/*
7+
* The implementation of Binary Search Tree
8+
*/
9+
public class BinarySearchTree {
10+
11+
protected BinaryNode root;
12+
13+
public BinarySearchTree( ) {
14+
root = null;
15+
}
16+
17+
public void insert( Index ind ) {
18+
root = insert( ind, root );
19+
}
20+
21+
protected BinaryNode insert( Index ind, BinaryNode node ) {
22+
if( node == null )
23+
node = new BinaryNode( ind );
24+
else if( ind.compareTo( node.element ) < 0 )
25+
node.left = insert( ind, node.left );
26+
else if( ind.compareTo( node.element ) > 0 )
27+
node.right = insert( ind, node.right );
28+
else
29+
throw new DuplicateItemException( ind.toString( ) );
30+
return node;
31+
}
32+
33+
public void remove( String str ){
34+
remove( new Index(str) );
35+
}
36+
37+
public void remove( Index ind ) {
38+
root = remove( ind, root );
39+
}
40+
41+
protected BinaryNode remove( Index ind, BinaryNode node ) {
42+
if( node == null )
43+
throw new ItemNotFoundException( ind.toString( ) );
44+
if( ind.compareTo( node.element ) < 0 )
45+
node.left = remove( ind, node.left );
46+
else if( ind.compareTo( node.element ) > 0 )
47+
node.right = remove( ind, node.right );
48+
else if( node.left != null && node.right != null ) {
49+
node.element = findMin( node.right ).element;
50+
node.right = removeMin( node.right );
51+
} else
52+
node = ( node.left != null ) ? node.left : node.right;
53+
return node;
54+
}
55+
56+
protected BinaryNode findMin( BinaryNode t ) {
57+
if( t != null )
58+
while( t.left != null )
59+
t = t.left;
60+
61+
return t;
62+
}
63+
64+
protected BinaryNode removeMin( BinaryNode t ) {
65+
if( t == null )
66+
throw new ItemNotFoundException( );
67+
else if( t.left != null ) {
68+
t.left = removeMin( t.left );
69+
return t;
70+
} else
71+
return t.right;
72+
}
73+
74+
private Index elementAt( BinaryNode t ) {
75+
return t == null ? null : t.element;
76+
}
77+
78+
public Index find( Index ind ) {
79+
return elementAt( find( ind, root ) );
80+
}
81+
82+
public Index find( String lex ) {
83+
return find( new Index( lex ) );
84+
}
85+
86+
private BinaryNode find( Index x, BinaryNode t ) {
87+
while( t != null ) {
88+
if( x.compareTo( t.element ) < 0 )
89+
t = t.left;
90+
else if( x.compareTo( t.element ) > 0 )
91+
t = t.right;
92+
else
93+
return t; // Found
94+
}
95+
return null; // Not found
96+
}
97+
98+
}
99+
100+
/*
101+
* The class of Binary Search Tree node objects
102+
*/
103+
class BinaryNode {
104+
105+
Index element; // The data in the node
106+
BinaryNode left; // Left child
107+
BinaryNode right; // Right child
108+
109+
BinaryNode( Index theElement ) {
110+
element = theElement;
111+
left = right = null;
112+
}
113+
114+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author Konrad S³oniewski, ks321221
3+
* @version 1.0, 29 Apr 2012
4+
*/
5+
6+
/*
7+
* Exception class for duplicate item errors
8+
* in binary search tree insertions.
9+
*/
10+
public class DuplicateItemException extends RuntimeException {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
/*
15+
* Constructors of this exception.
16+
*/
17+
public DuplicateItemException( ) {
18+
super( );
19+
}
20+
21+
public DuplicateItemException( String message ) {
22+
super( message );
23+
}
24+
}

tokarski/Index.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/**
2+
* @author Konrad S³oniewski, ks321221
3+
* @version 1.0, 29 Apr 2012
4+
*/
5+
6+
public class Index {
7+
8+
private String lex;
9+
private IndexElement[] elem;
10+
11+
12+
Index( String specialSigns, String lex, String line ) {
13+
this.lex = lex;
14+
elem = new IndexElement[1];
15+
elem[0] = new IndexElement(lex, specialSigns, line);
16+
}
17+
18+
Index( String lex, String line) {
19+
this.lex = lex;
20+
elem = new IndexElement[1];
21+
elem[0] = new IndexElement(lex, line);
22+
}
23+
24+
Index( String lex ) {
25+
this.lex = lex;
26+
}
27+
28+
public void addElement( String specialSigns, String line ) {
29+
int size = elem.length;
30+
IndexElement[] enhancedElem = new IndexElement[size + 1];
31+
for (int i = 0; i < size; i++)
32+
enhancedElem[i] = elem[i];
33+
enhancedElem[size] = new IndexElement(lex, specialSigns, line);
34+
elem = enhancedElem;
35+
}
36+
37+
public void addElement( String line ) {
38+
int size = elem.length;
39+
IndexElement[] enhancedElem = new IndexElement[size + 1];
40+
for (int i = 0; i < size; i++)
41+
enhancedElem[i] = elem[i];
42+
enhancedElem[size] = new IndexElement(lex, line);
43+
elem = enhancedElem;
44+
}
45+
46+
public IndexElement[] getElements() {
47+
return elem;
48+
}
49+
50+
public int compareTo( Index ind ) {
51+
if( lex.compareTo(ind.toString()) > 0) {
52+
return 1;
53+
}
54+
else if( lex.compareTo(ind.toString()) == 0) {
55+
return 0;
56+
}
57+
else return -1;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return lex;
63+
}
64+
}

tokarski/IndexElement.java

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/**
2+
* @author Konrad S³oniewski, ks321221
3+
* @version 1.0, 29 Apr 2012
4+
*/
5+
6+
public class IndexElement {
7+
8+
private String lex, sign, grammar, form, ending;
9+
10+
IndexElement( String lex, String specialSigns, String line ) {
11+
this.lex = lex;
12+
//System.out.println("dodany ind elem: "+lex);
13+
sign = specialSigns;
14+
//line format [ column2;column3;column4/ ]
15+
int semicolon = line.indexOf(';');
16+
String tmp;
17+
System.out.println("iii");
18+
if ( line.charAt(0) == ';' ) {
19+
grammar = "";
20+
form = "";
21+
} else {
22+
System.out.println("jjj");
23+
tmp = line.substring(0, semicolon);
24+
int space = tmp.indexOf(' ');
25+
System.out.println("kkk");
26+
if( space < 0) {
27+
System.out.println("mmm");
28+
grammar = tmp;
29+
form = "";
30+
} else {
31+
grammar.substring(0, space);
32+
System.out.println("lll");
33+
tmp = tmp.substring(space + 1);
34+
form = tmp;
35+
}
36+
}
37+
System.out.println("hhh");
38+
line = line.substring(semicolon + 1);
39+
//line format [ column3;column4/ ]
40+
41+
semicolon = line.indexOf(';');
42+
tmp = line.substring(0, semicolon);
43+
if ( line.charAt(0) == ';' || tmp.contains("*"))
44+
ending = "";
45+
else {
46+
ending = line.substring(0, semicolon);
47+
ending = ending.replaceAll("[^\\p{L}\\p{N}]", "");
48+
ending = ending.replaceAll("[CV]", "");
49+
}
50+
}
51+
52+
IndexElement( String lex, String line ) {
53+
//System.out.println("dodany ind elem: "+lex);
54+
this.lex = lex;
55+
//line format [column2 other]
56+
sign = form = ending = "";
57+
int space = line.indexOf(' ');
58+
if ( space == 0 ) //no space in line or space is first character
59+
grammar = "";
60+
else if ( space < 0 ) {
61+
grammar = line;
62+
} else {
63+
grammar = line.substring(0, space);
64+
if ( grammar.contains("*") )
65+
grammar = "";
66+
}
67+
}
68+
69+
public String getSign() {
70+
return sign;
71+
}
72+
73+
public String getEnding() {
74+
if ( ending == "" ) {
75+
//System.out.println("skorzystalem z lex");
76+
return lex;
77+
}
78+
else {
79+
//System.out.println("skorzystalem z ending");
80+
return ending;
81+
}
82+
}
83+
84+
public String getGrammar() {
85+
//System.out.println("gramatyka to: "+grammar);
86+
return grammar;
87+
}
88+
89+
public String getForm() {
90+
return form;
91+
}
92+
93+
@Override
94+
public String toString() {
95+
return sign;
96+
}
97+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author Konrad S³oniewski, ks321221
3+
* @version 1.0, 29 Apr 2012
4+
*/
5+
6+
/*
7+
* Exception class for failed item removes or finds
8+
* in binary search tree.
9+
*/
10+
public class ItemNotFoundException extends RuntimeException {
11+
12+
private static final long serialVersionUID = 1L;
13+
14+
/*
15+
* Constructors of this exception.
16+
*/
17+
public ItemNotFoundException( ) {
18+
super( );
19+
}
20+
21+
public ItemNotFoundException( String message ) {
22+
super( message );
23+
}
24+
}

0 commit comments

Comments
 (0)