Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.Stack;
- 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;
- }
- }
- public class WindowsExplorer {
- 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:");
- // System.out.println( tree.root.parent );
- // vasiot kod stoi ovde
- SLLNode<String> tekoven = tree.root; // go cuva tekovniot otvoren folder
- String aCommand[] = new String[2];// se delat dvete komandi
- for (i = 0; i < N; i++) {
- aCommand = commands[i].split(" ");
- //System.out.println( i + " " );
- if (aCommand[0].equals("CREATE")) {
- if( tekoven.firstChild == null ) // ako tekoven e list
- tree.addChild( tekoven, aCommand[1] );
- else if( tekoven.firstChild.element.compareTo( aCommand[1] ) > 0) // ako firstChild na tekoven treba da dojde posle noviot node
- tree.addChild( tekoven, aCommand[1] );
- else {
- // compare to ako vtoriot e pogolem e poz
- SLLNode<String> insert_after = tekoven.firstChild;
- SLLNode<String> newFolder = new SLLNode<>( aCommand[1] );
- while( insert_after.sibling != null&&insert_after.sibling.element.compareTo( aCommand[1] ) < 0 )// naogjam posle koj treba da se stavi noviot folder
- insert_after = insert_after.sibling;
- newFolder.sibling = insert_after.sibling;// sibling pokazuva kon siblingot na prethodnikot
- newFolder.parent = tekoven;
- insert_after.sibling = newFolder; // prethodnikot za sivling go dobiva noviot folder
- }
- } else if (aCommand[0].equals("OPEN")) {
- SLLNode<String> temp = tekoven.firstChild;
- while( temp!= null && !temp.element.equals(aCommand[1]) )
- temp = temp.sibling;
- if( temp != null )
- tekoven = temp;
- } else if (aCommand[0].equals("DELETE")) {
- SLLNode<String> temp = tekoven.firstChild;
- if( temp.element.equals(aCommand[1]) ) // ako prvoto dete se bride
- tekoven.firstChild = temp.sibling;
- while( temp.sibling!= null && !temp.sibling.element.equals(aCommand[1]) )// go baram el
- temp = temp.sibling;
- if( temp.sibling != null ) {
- temp.sibling = temp.sibling.sibling;
- }
- } else if (aCommand[0].equals("BACK")) {
- // while( tekoven.parent != null )
- tekoven = tekoven.parent;
- } else if (aCommand[0].equals("PATH")) {
- Stack<String> st = new Stack<>();
- SLLNode<String> temp = tekoven;
- while( temp != null ) {
- st.push(temp.element);
- temp = temp.parent;
- }
- while ( !st.isEmpty() ) {
- System.out.print( st.pop() + "\\" );
- }
- System.out.println( "" );
- } else if (aCommand[0].equals("PRINT")) {
- tree.printTree();
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment