import
java.io.*;
import
java.util.Scanner;
class
PatriciaTrieNode {
int
number;
int
data;
PatriciaTrieNode leftChild, rightChild;
}
class
PatriciaTest {
private
PatriciaTrieNode root;
private
static
final
int
MaxBits =
10
;
public
PatriciaTest() { root =
null
; }
public
boolean
isEmpty() {
return
root ==
null
; }
public
void
makeEmpty() { root =
null
; }
private
boolean
bit(
int
k,
int
i)
{
String binary = Integer.toString(k,
2
);
while
(binary.length() != MaxBits)
binary =
"0"
+ binary;
if
(binary.charAt(i -
1
) ==
'1'
)
return
true
;
return
false
;
}
public
boolean
search(
int
k)
{
int
num = (
int
)(Math.log(k) / Math.log(
2
));
if
(num > MaxBits) {
System.out.println(
"Exceeded the limit"
);
return
false
;
}
PatriciaTrieNode searchNode = search(root, k);
if
(searchNode.data == k)
return
true
;
else
return
false
;
}
private
PatriciaTrieNode search(PatriciaTrieNode t,
int
k)
{
PatriciaTrieNode currentNode, nextNode;
if
(t ==
null
) {
return
null
;
}
nextNode = t.leftChild;
currentNode = t;
while
(nextNode.number > currentNode.number) {
currentNode = nextNode;
nextNode = (bit(k, nextNode.number))
? nextNode.rightChild
: nextNode.leftChild;
}
return
nextNode;
}
public
void
insert(
int
element)
{
int
num
= (
int
)(Math.log(element) / Math.log(
2
)) +
1
;
if
(num > MaxBits) {
System.out.println(
"We are full, The number is too large"
);
return
;
}
root = insert(root, element);
}
private
PatriciaTrieNode insert(PatriciaTrieNode t,
int
element)
{
PatriciaTrieNode current =
null
, parent, lastNode,
newNode;
int
i;
if
(t ==
null
) {
t =
new
PatriciaTrieNode();
t.number =
0
;
t.data = element;
t.leftChild = t;
t.rightChild =
null
;
return
t;
}
lastNode = search(t, element);
if
(element == lastNode.data) {
System.out.println(
"Key already Present"
);
return
t;
}
for
(i =
1
;
bit(element, i) == bit(lastNode.data, i); i++)
current = t.leftChild;
parent = t;
while
(current.number > parent.number
&& current.number < i) {
parent = current;
current = (bit(element, current.number))
? current.rightChild
: current.leftChild;
}
newNode =
new
PatriciaTrieNode();
newNode.number = i;
newNode.data = element;
newNode.leftChild
= bit(element, i) ? current : newNode;
newNode.rightChild
= bit(element, i) ? newNode : current;
if
(current == parent.leftChild) {
parent.leftChild = newNode;
}
else
{
parent.rightChild = newNode;
}
return
t;
}
}
public
class
GFG {
public
static
void
main(String[] args)
{
Scanner sc =
new
Scanner(System.in);
PatriciaTest pt =
new
PatriciaTest();
System.out.println(
"Patricia Trie\n"
);
char
ch;
do
{
System.out.println(
"\n Patricia Trie Operations\n"
);
System.out.println(
"1. Insert"
);
System.out.println(
"2. Search"
);
System.out.println(
"3. Check Empty"
);
System.out.println(
"4. Make Empty"
);
System.out.println(
"Make your choice"
);
int
choice = sc.nextInt();
switch
(choice) {
case
1
:
System.out.println(
"Enter element to insert"
);
pt.insert(sc.nextInt());
break
;
case
2
:
System.out.println(
"Enter element to search"
);
System.out.println(
"Search result:"
+ pt.search(sc.nextInt()));
break
;
case
3
:
System.out.print(
"Empty status : "
+ pt.isEmpty());
break
;
case
4
:
System.out.println(
"Patricie Trie Cleared"
);
pt.makeEmpty();
break
;
default
:
System.out.println(
"Wrong entry\n"
);
break
;
}
System.out.println(
"\n Do you want to continue (Type y or n)\n"
);
ch = sc.next().charAt(
0
);
}
while
(ch ==
'Y'
|| ch ==
'y'
);
}
}