Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- import java.util.Iterator;
- import java.util.NoSuchElementException;
- interface Tree<E> {
- ////////////Accessors ////////////
- public Node<E> root();
- public Node<E> parent(Node<E> node);
- public int childCount(Node<E> node);
- ////////////Transformers ////////////
- public void makeRoot(E elem);
- public Node<E> addChild(Node<E> node, E elem);
- public void remove(Node<E> node);
- ////////////Iterator ////////////
- public Iterator<E> children(Node<E> node);
- }
- interface Node<E> {
- public E getElement();
- public void setElement(E elem);
- }
- class SLLTree<E> implements Tree<E> {
- public SLLNode<E> root;
- public SLLTree() {
- root = null;
- }
- public Node<E> root() {
- return root;
- }
- public Node<E> parent(Node<E> node) {
- return ((SLLNode<E>) node).parent;
- }
- public int childCount(Node<E> node) {
- SLLNode<E> tmp = ((SLLNode<E>) node).firstChild;
- int num = 0;
- while (tmp != null) {
- tmp = tmp.sibling;
- num++;
- }
- return num;
- }
- public void makeRoot(E elem) {
- root = new SLLNode<E>(elem);
- }
- public Node<E> addChild(Node<E> node, E elem) {
- SLLNode<E> tmp = new SLLNode<E>(elem);
- SLLNode<E> curr = (SLLNode<E>) node;
- tmp.sibling = curr.firstChild;
- curr.firstChild = tmp;
- tmp.parent = curr;
- return tmp;
- }
- public void remove(Node<E> node) {
- SLLNode<E> curr = (SLLNode<E>) node;
- if (curr.parent != null) {
- if (curr.parent.firstChild == curr) {
- // The node is the first child of its parent
- // Reconnect the parent to the next sibling
- curr.parent.firstChild = curr.sibling;
- } else {
- // The node is not the first child of its parent
- // Start from the first and search the node in the sibling list and remove it
- SLLNode<E> tmp = curr.parent.firstChild;
- while (tmp.sibling != curr) {
- tmp = tmp.sibling;
- }
- tmp.sibling = curr.sibling;
- }
- } else {
- root = null;
- }
- }
- class SLLTreeIterator<T> implements Iterator<T> {
- SLLNode<T> start, current;
- public SLLTreeIterator(SLLNode<T> node) {
- start = node;
- current = node;
- }
- public boolean hasNext() {
- return (current != null);
- }
- public T next() throws NoSuchElementException {
- if (current != null) {
- SLLNode<T> tmp = current;
- current = current.sibling;
- return tmp.getElement();
- } else {
- throw new NoSuchElementException();
- }
- }
- public void remove() {
- if (current != null) {
- current = current.sibling;
- }
- }
- }
- public Iterator<E> children(Node<E> node) {
- return new SLLTreeIterator<E>(((SLLNode<E>) node).firstChild);
- }
- void printTreeRecursive(Node<E> node, int level) {
- if (node == null)
- return;
- int i;
- SLLNode<E> tmp;
- for (i=0;i<level;i++)
- System.out.print(" ");
- System.out.println(node.getElement().toString());
- tmp = ((SLLNode<E>)node).firstChild;
- while (tmp != null) {
- printTreeRecursive(tmp, level+1);
- tmp = tmp.sibling;
- }
- }
- public void printTree() {
- printTreeRecursive(root, 0);
- }
- }
- class SLLNode<P> implements Node<P> {
- // Holds the links to the needed nodes
- public SLLNode<P> parent, sibling, firstChild;
- // Hold the data
- public P element;
- public SLLNode(P o) {
- element = o;
- parent = sibling = firstChild = null;
- }
- public P getElement() {
- return element;
- }
- public void setElement(P o) {
- element = o;
- }
- }
- interface Stack<E> {
- // Elementi na stekot se objekti od proizvolen tip.
- // Metodi za pristap:
- public boolean isEmpty ();
- // Vrakja true ako i samo ako stekot e prazen.
- public E peek ();
- // Go vrakja elementot na vrvot od stekot.
- // Metodi za transformacija:
- public void clear ();
- // Go prazni stekot.
- public void push (E x);
- // Go dodava x na vrvot na stekot.
- public E pop ();
- // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
- }
- class ArrayStack<E> implements Stack<E> {
- private E[] elems;
- private int depth;
- @SuppressWarnings("unchecked")
- public ArrayStack (int maxDepth) {
- // Konstrukcija na nov, prazen stek.
- elems = (E[]) new Object[maxDepth];
- depth = 0;
- }
- public boolean isEmpty () {
- // Vrakja true ako i samo ako stekot e prazen.
- return (depth == 0);
- }
- public E peek () {
- // Go vrakja elementot na vrvot od stekot.
- if (depth == 0)
- throw new NoSuchElementException();
- return elems[depth-1];
- }
- public void clear () {
- // Go prazni stekot.
- for (int i = 0; i < depth; i++) elems[i] = null;
- depth = 0;
- }
- public void push (E x) {
- // Go dodava x na vrvot na stekot.
- elems[depth++] = x;
- }
- public E pop () {
- // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
- if (depth == 0)
- throw new NoSuchElementException();
- E topmost = elems[--depth];
- elems[depth] = null;
- return topmost;
- }
- }
- public class WindowsExplorer {
- public static int sortString(String no1, String no2){
- int min = Math.min(no1.length(), no2.length());
- for (int i = 0; i < min; i++) {
- if(no1.charAt(i) > no2.charAt(i)){
- return 2;
- }
- if(no1.charAt(i) < no2.charAt(i)){
- return 1;
- }
- }
- if(no1.length()==min){
- return 1;
- } else{
- return 2;
- }
- }
- public static void main(String[] args) throws Exception {
- int i,j,k;
- BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- int N = Integer.parseInt(br.readLine());
- String commands[] = new String[N];
- for (i=0;i<N;i++)
- commands[i] = br.readLine();
- br.close();
- SLLTree<String> tree = new SLLTree<String>();
- tree.makeRoot("c:");
- SLLNode<String> tmp = tree.root;
- for (int l = 0; l < N; l++) {
- String s = commands[l];
- if(s.contains("CREATE")){
- s = s.substring(7);
- SLLNode<String> thed = new SLLNode<String>(s);
- SLLNode<String> frst=null;
- SLLNode<String> scnd = tmp.firstChild;
- if(scnd == null){
- tree.addChild(tmp, s);
- }else{
- int g = sortString(scnd.element,s);
- while(((scnd!=null)&&g==1)){
- frst = scnd;
- scnd = scnd.sibling;
- if(scnd != null){
- g = sortString(scnd.element,s);
- }
- }
- if(frst == null){
- tree.addChild(tmp, s);
- }
- else{
- thed.parent = tmp;
- thed.sibling = scnd;
- frst.sibling = thed;
- }
- }
- }
- if(s.contains("OPEN")){
- s =s.substring(5);
- int h = tree.childCount(tmp);
- tmp = tmp.firstChild;
- while(!tmp.element.equals(s)){
- tmp = tmp.sibling;
- }
- }
- if(s.contains("BACK")){
- tmp = tmp.parent;
- }
- if(s.contains("DELETE")){
- s = s.substring(7);
- SLLNode<String> tht = new SLLNode<String>(s);
- SLLNode<String> tmp1 = tmp.firstChild;
- while(!tmp1.element.equals(s)){
- tmp1 = tmp1.sibling;
- }
- tree.remove(tmp1);
- }
- if(s.contains("PATH")){
- ArrayStack<SLLNode<String>> str = new ArrayStack<SLLNode<String>>(N);
- SLLNode<String> tmp1 = tmp;
- while(tmp1!=null){
- str.push(tmp1);
- tmp1 = tmp1.parent;
- }
- while(!str.isEmpty()){
- SLLNode<String> t = str.pop();
- System.out.print(t.element + "\\");
- }
- System.out.println();
- }
- if(s.contains("PRINT")){
- tree.printTree();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement