Java
Abstract Data Type
We know that a data type signifies the type and space taken by the data used in programs. An abstract data type is a special data type that is defined by a set of values and a set of operations on that type.
We call these data types as “abstract” because these are independent of any implementation. We can use these data types and perform different operations with them , but we do not know how these operations are working internally.
An Abstract Data Type is defined only by its operations , not by its implementation.
Notes: Interfaces in java are not purely abstract as they can contain some implementation details e.g. default methods.
Some commonly used ADT’s are :
- Stacks: Structures that supports last-in first-out retrieval of elements
- push(int x) : puts x on the top of the stack
- int pop() : takes the element on the top of the stack
- Lists : an ordered set of elements
- add(int i): adds an element
- int get(int i) : gets element at index i
- Sets: an unordered set of unique elements(no repeat)
- add(int i) : adds an element
- contains(int i): returns a boolean for whether or not the set contains the value
- Maps : set of key/value pairs
- put(K key, V value) : puts a key value pair into the map
- V get(K key): gets the value corresponding the key
- The bolded ADT’s are sub-interfaces of a bigger overarching interface called Collection
- Among the most important interfaces in the java.util.library are those that extend the Collection interface. ( interfaces can extend other interfaces)
Types and Operations of ADT
- Types
- We can classify the Abstract data types either as built-in or user-defined or as mutable or immutable
- Operations
- Creators : Creators create new objects of the type. It may take an object as an argument
- Producers : Producers create new objects from old objects of the type.
- Observers : Observers take the objects of the abtract data type and return objects of a different type . eg: size() method of the List returns an ine
- Mutators : Mutators change objects . eg: the add() method of List change a list by adding an element to the end.
- Some Abstract Data type with some of their operations and the types
- int is a primitive integer type of java and is immutable. So its operations are:
- creators : the numeric literals 0,1,2,3
- producers : arithmetic operators : + - * /
- obeservers : comparison operators ==,!= ,<,>
- mutators: None
- list is an interface of java List. List is mutable. So its operations are:
- creators : ArrayList and LinkedList constructors, Collections.singletonList
- producers : Collections.unmodifiableList
- observers : size, get
- mutators: add, remove, addAll, Collections.sort
- string is java’s string type and is immutable. So its operations are:
- creators : String constructors
- producers : concat, substring, toUpperCase
- observers : length, charAt
- mutators: none (it’s immutable)
- int is a primitive integer type of java and is immutable. So its operations are:
Binary Search Tree
- Properties of trees
- Trees are composed of
- Nodes
- Edges that connect those nodes
- Constraint : there is only one path between any two nodes
- (Leaves) :nodes with no children
- root node has no parents
- Trees are composed of
- Binary Trees
- binary property constraint : each node has either 0, 1, 2
- Binary Search Tree
- For every node X in the tree, every key in the left subtree is less than X’s key
- For every node X in the tree, every key in the right subtree is greater than X’s key
- Search : If the tree is relatively “bushy” , the find operation will run in log(n) time because the height of the tree is log(n).
- Insert : always insert at a leaf node. First we search in the tree for the node. If we find it , then we do not do anything . If we do not find it , we will be at the leaf node already . At this point, we can just add the new element to either the left or the right of the leaf ,preserving the BST property.
- Delete : 3 categories:
- the node we are trying to delete has no children : just delete
- has 1 child : reassgin the parent’s child pointer to the node’s child
- has 2 children : need to choose a new node to replace the deleted one. To find these nodes, we can take the right-most node in the left sub-tree or the left-most node in the right subtree. Hibbard deletion.
BST Perfomance
- depth : the number of links between a node and the root
- height : the lowest depth of a tree
- average depth : average of the total depths in the tree . We can calculate this by taking ∑ i = 0 D d i n i N \frac{\sum_{i=0}^{D}d_i n_i}{N} N