Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
How to Implement Sets in Java and C++ In neither C++ nor Java is Set is a language primitive structure, but both provide low level utilities that can aid you if you wish to use them. In general, if a programmer wishes to implement a Set without assistance from the language support structure, the Set will be implemented either as a bit vector, or as some variant of a list. The list could be stored in a string, an array, a linked list, a hash table, or even a tree. Bit Vector Representation In a bit vector implementation, each set is allocated (ideally) as the same number of bits as the size of the universal set. The universal set will have a natural or imposed order, each 1 or 0 will represent the presence or absence of that member of the universal set. Some languages, such as PL/1 support bit strings, but most do not. Unfortunately, a bit array is usually not an efficient option as most compilers will not allocate the space correctly. An array of 100 booleans would likely be allocated as an array of 100 bytes or even 100 integers. For example, a Set of Capital Letters implemented as a bit vector in C++ or Java should probably be stored as an int. Only 26 of the 32 bits allocated would actually be needed. The 0th bit would indicate the presence or absence of ‘A’, bit 1 would do the same for ‘B’, and so on. Once a set has been created, the bitwise operations: and(&), inclusive or(|), and one’s complement(~) would be used to support set intersection, set union, and set complement operations. I/O is still problematic however. For example, the set of vowels as a 32 bit vector would be: vowels = {10001000100000100000100000} List Representation In the list representation, only those members of the set present are stored in the list. The list could be stored as a character string or some sort of array, or a linked list, or a hash table or even a tree. The list may or may not be ordered, but ordering the list is preferred. A set of letters or integers is easy to order, but a set of points may be difficult or impossible to order. Most operations on list-based sets are more difficult to implement than for bit vectors, except I/O which is now pretty easy. Three basic operations can be defined that can assist with the implementation of most of the others. The first tests whether a set contains a particular element. The second adds an element to an existing set if not already present. The third removes an element from the set if present. The assistance provided by both C++ and Java is in the list form, and both intrinsically reject duplicate values, and silently ignore commands to remove values not present. Java Support for Sets: Java provides hash sets and tree sets. The code fragment below demonstrates a little of what can be done with HashSets. Some of the methods used below are: add( theItem) remove(theItem) contains(theItem) import java.util.HashSet; import java.util.Set; import java.util.Scanner; … Set <String> names = new HashSet <String> (); // HashSet of Strings Scanner kb = new Scanner( System.in ); while( true ){ System.out.println( “ Add a name. Q when done.” ); String input = kb.next(); if( input.equalsIgnoreCase(“Q”) ) break; else names.add( input );// remove(input) could later remove it.} … Iterator <String> iter = names.iterator(); // If you get an iterator, then while( iter.hasNext() ) // hasNext() and next() can { String theName = iter.next(); // assist you in traversing. System.out.println( theName ); } … or for( String name : names ) // You can also use the “for { .. do some thing with the name } // each” loop to traverset. The Java TreeSet is also interesting in that it requires the Comparable interface be implemented, and has the distinctly pleasant side effect that it visits the elements in sorted order based upon your definition of the “compare” method. Obviously, it creates a BST. C++ Support for Sets In C++, you would #include <set> and there are methods such as insert( elementValue), erase(elementValue), begin(), and end().