├── ex ├── .idea │ ├── .name │ ├── .gitignore │ ├── modules.xml │ └── misc.xml ├── .DS_Store ├── out │ ├── .DS_Store │ └── production │ │ └── .DS_Store ├── ex.iml └── src │ ├── Node.java │ ├── RopeTest.java │ └── Rope.java ├── README.md ├── .idea ├── .gitignore ├── vcs.xml ├── misc.xml └── modules.xml ├── .DS_Store ├── .gitignore └── Rope-Data-Structure.iml /ex/.idea/.name: -------------------------------------------------------------------------------- 1 | Rope -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rope-Data-Structure -------------------------------------------------------------------------------- /.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /ex/.idea/.gitignore: -------------------------------------------------------------------------------- 1 | # Default ignored files 2 | /shelf/ 3 | /workspace.xml 4 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MohammadRouintan/Rope-Data-Structure/HEAD/.DS_Store -------------------------------------------------------------------------------- /ex/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MohammadRouintan/Rope-Data-Structure/HEAD/ex/.DS_Store -------------------------------------------------------------------------------- /ex/out/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MohammadRouintan/Rope-Data-Structure/HEAD/ex/out/.DS_Store -------------------------------------------------------------------------------- /ex/out/production/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/MohammadRouintan/Rope-Data-Structure/HEAD/ex/out/production/.DS_Store -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /ex/.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /ex/.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | *.jar 15 | *.war 16 | *.nar 17 | *.ear 18 | *.zip 19 | *.tar.gz 20 | *.rar 21 | 22 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 23 | hs_err_pid* 24 | -------------------------------------------------------------------------------- /ex/ex.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /Rope-Data-Structure.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /ex/src/Node.java: -------------------------------------------------------------------------------- 1 | class Node { 2 | Node left, right; 3 | String data; 4 | int weight; 5 | 6 | public Node(String data) { 7 | this.data = data; 8 | this.left = null; 9 | this.right = null; 10 | this.weight = data.length(); 11 | } 12 | 13 | public Node() { 14 | this.data = null; 15 | this.left = null; 16 | this.right = null; 17 | this.weight = 0; 18 | } 19 | 20 | public static void printNode(Node node) { 21 | if (node != null) { 22 | printNode(node.left); 23 | if (node.data != null) { 24 | System.out.print(node.data); 25 | } 26 | printNode(node.right); 27 | } 28 | } 29 | 30 | } 31 | 32 | -------------------------------------------------------------------------------- /ex/src/RopeTest.java: -------------------------------------------------------------------------------- 1 | //import java.util.LinkedList; 2 | //import java.util.Scanner; 3 | // 4 | // 5 | public class RopeTest { 6 | public static void main(String[] args) { 7 | Rope rope = new Rope(); 8 | rope.append("Rope"); 9 | rope.append("_is"); 10 | rope.append("_st"); 11 | rope.append("ronger"); 12 | rope.printRoot(); 13 | System.out.println(); 14 | System.out.println(rope.findByIndex(10)); 15 | 16 | Rope rope1 = new Rope(); 17 | rope1.append("Rope"); 18 | rope1.append("_is"); 19 | rope1.append("_st"); 20 | rope1.append("ronger"); 21 | rope.concatenation(rope1); 22 | rope.printRoot(); 23 | System.out.println(); 24 | System.out.println(rope.substring(1, 5)); 25 | } 26 | } 27 | //public class RopeTest 28 | //{ 29 | // public static void main(String[] args) 30 | // { 31 | // Scanner scan = new Scanner(System.in); 32 | // Rope r = new Rope("I am Ali"); 33 | // System.out.println("Rope Test\n"); 34 | // char ch; 35 | // do 36 | // { 37 | // { 38 | // case 1 : 39 | // System.out.println("Enter string to concat"); 40 | // r.concat(scan.next()); 41 | // break; 42 | // case 2 : 43 | // System.out.println("Enter index"); 44 | // System.out.println("Character at index = "+ r.indexAt(scan.nextInt())); 45 | // break; 46 | // case 3 : 47 | // System.out.println("Enter integer start and end limit"); 48 | // System.out.println("Substring : "+ r.substring( scan.nextInt(), scan.nextInt() )); 49 | // break; 50 | // case 4 : 51 | // System.out.println("\nRope Cleared\n"); 52 | // r.makeEmpty(); 53 | // break; 54 | // default : 55 | // System.out.println("Wrong Entry \n "); 56 | // break; 57 | // } 58 | // /** Display rope **/ 59 | // System.out.print("\nRope : "); 60 | // r.print(); 61 | // 62 | // } while (ch == 'Y'|| ch == 'y'); 63 | // } 64 | //} 65 | -------------------------------------------------------------------------------- /ex/src/Rope.java: -------------------------------------------------------------------------------- 1 | 2 | class Rope { 3 | Node root; 4 | 5 | public Rope() { 6 | root = new Node(); 7 | } 8 | 9 | public void makeEmpty() { 10 | root = new Node(); 11 | } 12 | 13 | public void append(String str) { 14 | Node newNode = new Node(str); 15 | Node newRoot = new Node(); 16 | 17 | newRoot.left = root; 18 | newRoot.right = newNode; 19 | 20 | if (newRoot.left.right == null) { 21 | newRoot.weight = newRoot.left.weight; 22 | } else { 23 | newRoot.weight = newRoot.left.weight + newRoot.left.right.weight; 24 | } 25 | 26 | 27 | root = newRoot; 28 | } 29 | 30 | public char findByIndex(int index) { 31 | Node node = root; 32 | if (index > node.weight) { 33 | index -= node.weight; 34 | return node.right.data.charAt(index); 35 | } 36 | 37 | while (index < node.weight) { 38 | node = node.left; 39 | } 40 | 41 | index -= node.weight; 42 | return node.right.data.charAt(index); 43 | } 44 | 45 | public void concatenation(Rope rope) { 46 | Rope newRoot = new Rope(); 47 | newRoot.root.left = this.root; 48 | newRoot.root.right = rope.root; 49 | newRoot.root.weight = newRoot.root.left.weight; 50 | newRoot.root.weight += newRoot.root.left.right.weight; 51 | this.root = newRoot.root; 52 | } 53 | 54 | public String substring(int start, int end) { 55 | String str = ""; 56 | boolean found = false; 57 | Node tmp = this.root; 58 | if (end > tmp.weight) { 59 | found = true; 60 | end -= tmp.weight; 61 | if (start > tmp.weight) { 62 | start -= tmp.weight; 63 | str = tmp.right.data.substring(start, end); 64 | return str; 65 | } 66 | else 67 | str = tmp.right.data.substring(0, end); 68 | } 69 | if (!found) { 70 | while (end <= tmp.weight) 71 | tmp = tmp.left; 72 | end -= tmp.weight; 73 | if (start >= tmp.weight) { 74 | start -= tmp.weight; 75 | str = tmp.right.data.substring(start, end) + str; 76 | return str; 77 | } 78 | str = tmp.right.data.substring(0, end); 79 | } 80 | tmp = tmp.left; 81 | while (start < tmp.weight) { 82 | str = tmp.right.data + str; 83 | tmp = tmp.left; 84 | } 85 | start -= tmp.weight; 86 | str = tmp.right.data.substring(start) + str; 87 | 88 | return str; 89 | } 90 | 91 | 92 | public void printRoot() { 93 | Node.printNode(root); 94 | } 95 | } 96 | --------------------------------------------------------------------------------