C program Simulate object structure according to an arbitrar

C++ program

Simulate object structure according to an arbitrary nesting job options.
Such a structure can be compared with the file system, which are two types of objects: file and folder, the folder - this is a list of files and folders. In software engineering structure is realized with the help of the pattern (design pattern) \"layout» (http://cpp-reference.ru/patterns/structural-patterns/composite/).
The object structure includes three classes, forming a hierarchy. The base class contains a pure virtual methods bool Equals (a reference to a base class object) and void ToString (char * buffer). Two classes of heir - a \"Member\" (for example, a file) and \"Container\" (for example, a folder). Heirs override virtual methods. \"Container\" includes an array (or list) of pointers to base class objects. To provide a virtual destructor.
Due to the fact that the \"container\" contains pointers to the base class, by which is meant the real child objects - \"Element\" and \"container\", achieved universal unlimited nesting of objects or tree.
As the \"Element\" in all variants of the job is to be the class that stores the number of different data types: int, float, bool, similar to the variant type of the Pascal language. Implement based union.

Option 1
Container - a vector (one-dimensional array)
option 2
Container - a matrix (two-dimensional array)
Option 3
Container - all (one-dimensional array) without the same elements
Option 4
Container - a stack (one-dimensional array) without the same elements

embodiment 5
Container - a vector (one-dimensional array) without the same elements
Option 6
Container - a three-dimensional array
Option 7
Container - all (one-dimensional array)
embodiment 8
Container - a stack (one-dimensional array)

In the program to demonstrate the formation of several structures of arbitrary nesting commands from the console. Each team must either create a new structure, or add to it, \"Element\" or \"container\".
The result of the program - the output to a file ToString method, the results of each of the formed structures, as well as the result of comparing each frame with each as a result of the Equals method.
An example of the ToString method: [1,2.0, false, [1,2], 4.2, [1, [2], to true]], where [] indicate the container.
Equals method - check equality of the two structures. Structures are considered equal if their corresponding elements are equal (including attachments).

Solution

public final category Huffman non-public Huffman() ;

non-public static category HuffmanNode
}

non-public static category HuffManComparator implements Comparator<HuffmanNode> come back node1.frequency - node2.frequency;
}
}

/**
* Compresses the string victimisation huffman formula.
* The huffman tree and also the huffman code area unit serialized to disk
*
* @param sentence The sentence to be serialized
* @throws FileNotFoundException If file isn\'t found
* @throws IOException If IO exception happens.
*/
public static void compress(String sentence) throws FileNotFoundException, IOException ought to atleast have one character.\");
}

final Map<Character, Integer> charFreq = getCharFrequency(sentence);
final HuffmanNode root = buildTree(charFreq);
final Map<Character, String> charCode = generateCodes(charFreq.keySet(), root);
final String encodedMessage = encodeMessage(charCode, sentence);
serializeTree(root);
serializeMessage(encodedMessage);
}

non-public static Map<Character, Integer> getCharFrequency(String sentence) else
}

come map;
}


/**
* Map<Character, Integer> map
* Some implementation of that treeSet is passed as parameter.
* @param map
*/
non-public static HuffmanNode buildTree(Map<Character, Integer> map) whereas (nodeQueue.size() > 1)

// take away it to forestall object leak.
come nodeQueue.remove();
}

non-public static Queue<HuffmanNode> createNodeQueue(Map<Character, Integer> map)
return pq;
}

non-public static Map<Character, String> generateCodes(Set<Character> chars, HuffmanNode node) {
final Map<Character, String> map = new HashMap<Character, String>();
doGenerateCode(node, map, \"\");
come map;
}


non-public static void doGenerateCode(HuffmanNode node, Map<Character, String> map, String s)   
doGenerateCode(node.left, map, s + \'0\');
doGenerateCode(node.right, map, s + \'1\' );
}


non-public static String encodeMessage(Map<Character, String> charCode, String sentence)
come stringBuilder.toString();
}

non-public static void serializeTree(HuffmanNode node) throws FileNotFoundException, IOException attempt (ObjectOutputStream oosTree = new ObjectOutputStream(new FileOutputStream(\"/Users/ap/Desktop/tree\"))) strive (ObjectOutputStream oosChar = new ObjectOutputStream(new FileOutputStream(\"/Users/ap/Desktop/char\"))) soft to mark finish of bit set relevant for deserialization.
oosTree.writeObject(bitSet);
}
}
}

non-public static category IntObject

/*
* Algo:
* 1. Access the node
* 2. Register the worth in bit set.
*
*
* here true and false dont correspond to left branch and right branch.
* there,
* - true means that \"a branch originates from leaf\"
* - false mens \"a branch originates from non-left\".
*
* additionally since branches originate from some node, the foundation node should be provided as supply
* or place to begin of initial branches.
*   
* Diagram and the way associate degree bit set would look as a result.
* (source node)
* / \\
* true true
* / \\
* (leaf node) (leaf node)
* | |
* false false
* | |
*
* thus currently somewhat set appears like [false, true, false, true]
*
*/
non-public static void preOrder(HuffmanNode node, ObjectOutputStream oosChar, BitSet bitSet, IntObject intObject) throws IOException
bitSet.set(intObject.bitPosition++, true); // register branch in bitset
preOrder(node.left, oosChar, bitSet, intObject); // take the branch.

bitSet.set(intObject.bitPosition++, true); // register branch in bitset
preOrder(node.right, oosChar, bitSet, intObject); // take the branch.
}

non-public static void serializeMessage(String message) throws IOException attempt (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(\"/Users/ap/Desktop/encodedMessage\")))
}

non-public static BitSet getBitSet(String message) else
}
bitSet.set(i, true); // dummy bit set to grasp the length
come bitSet;
}

/**
* Retrieves back the initial string.
*
*
* @return the initial uncompressed string
* @throws FileNotFoundException If the file isn\'t found
* @throws ClassNotFoundException If category isn\'t found
* @throws IOException If IOException happens
*/
public static String expand() throws FileNotFoundException, ClassNotFoundException, IOException {
final HuffmanNode root = deserializeTree();
come decodeMessage(root);
}

non-public static HuffmanNode deserializeTree() throws FileNotFoundException, IOException, ClassNotFoundException strive (ObjectInputStream oisBranch = new ObjectInputStream(new FileInputStream(\"/Users/ap/Desktop/tree\"))) strive (ObjectInputStream oisChar = new ObjectInputStream(new FileInputStream(\"/Users/ap/Desktop/char\"))) agencyBranch.readObject();
come preOrder(bitSet, oisChar, new IntObject());
}
}
}

/*
* Construct a tree from:
* input [false, true, false, true, (dummy faithful mark the top of bit set)]
* The input is built from preorder traversal
*
* Algo:
* one produce the node.
* 2. scan what\'s registered in bitset, and judge if created node is meant to be a leaf or non-leaf
*
*/
non-public static HuffmanNode preOrder(BitSet bitSet, ObjectInputStream oisChar, IntObject o) throws IOException crucial if created node is that the leaf or non-leaf.
if (!bitSet.get(o.bitPosition)) succeeding position to subsequent stack frame by doing computation before preOrder is named.
node.ch = oisChar.readChar();
come node;
}

o.bitPosition = o.bitPosition + 1; // feed subsequent position to subsequent stack frame by doing computation before preOrder is named.
node.left = preOrder(bitSet, oisChar, o);

o.bitPosition = o.bitPosition + 1; // feed subsequent position to subsequent stack frame by doing computation before preOrder is named.
node.right = preOrder(bitSet, oisChar, o);

come node;
}

non-public static String decodeMessage(HuffmanNode node) throws FileNotFoundException, IOException, ClassNotFoundException strive (ObjectInputStream Office of Intelligence Support = new ObjectInputStream(new FileInputStream(\"/Users/ameya.patil/Desktop/encodedMessage\"))) worker = node;
// since huffman code generates full binary tree, temp.right is definitely null if temporary worker.left is null.
whereas (temp.left != null) {
if (!bitSet.get(i)) {
temporary worker = temporary worker.left;
} else {
temporary worker = temporary worker.right;
}
i = i + 1;
}
stringBuilder.append(temp.ch);
}
come stringBuilder.toString();
}
}

public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException range of characters
Huffman.compress(\"some\");
Assert.assertEquals(\"some\", Huffman.expand());

// odd range of characters
Huffman.compress(\"someday\");
Assert.assertEquals(\"someday\", Huffman.expand());

// continuance even range of characters + area + non-ascii
Huffman.compress(\"some some#\");
Assert.assertEquals(\"some some#\", Huffman.expand());

// odd range of characters + area + non-ascii
Huffman.compress(\"someday someday&\");
Assert.assertEquals(\"someday someday&\", Huffman.expand());
}
}

C++ program Simulate object structure according to an arbitrary nesting job options. Such a structure can be compared with the file system, which are two types
C++ program Simulate object structure according to an arbitrary nesting job options. Such a structure can be compared with the file system, which are two types
C++ program Simulate object structure according to an arbitrary nesting job options. Such a structure can be compared with the file system, which are two types
C++ program Simulate object structure according to an arbitrary nesting job options. Such a structure can be compared with the file system, which are two types
C++ program Simulate object structure according to an arbitrary nesting job options. Such a structure can be compared with the file system, which are two types

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site