Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Operation

Operations: Java
1. add(T el): Insert element el in this priority.
NullPointerException if el is null
ClassCastException if the order used by this priority queue
cannot be applied to el.
2. clear(): Remove all the elements from this priority queue.
3. Comparator<? super T> comparator() : Return a comparator
to be used to order elements of this priority queue or null if
Priority Queue: elements are ordered with the method compareTo().
Can be assigned to anable a particular process without affecting 4. contains(Object el): Return true if element el is in this priority
ovarall system operation. queue.
5. offer(T el): Insert element el in this priority.
FIFO rule is broken in these situations. NullPointerException if el is null.
ClassCastException if the order used by this priority queue
In preority queues, elements are dequeued according to their cannot be applied to el.
priority and their current queue position

Operations:
1. clear(): Clear the queue.
Quick Sort 2. isEmpty(): Check to see if the queue is empty.
Best: O(n log(n)) 3. enqueue(el): Put the element el at the end of the queue.
Average: O(n log(n)) 4. dequeue(): Take the first element from the queue.
Worst: O(n2) 5. firstEl() / font() / peek(): Return the first element in the queue
Space worst: O(1) without removing it.
Operation:
Exception: EmptyQueueException 1. clear(): Clear the stack.
2. isEmpty(): Check to see if the stack is empty.
Queue: First in First out Structures. 3. push(el) / offer(el); Put the element el on the top of the stack.
Merge Sort
Can be accessed at both ends 4. pop(): Take the top most element from the stack.
Best: O(n log(n))
Hashing One for adding and one for removing 5. topEl() / peek: Return the topmost element in the stack without
Average: O(n log(n))
Worst: O(n2) removing it
Space worst: O(n)
Circular List Exception: EmptyStackException

Heap Sort Recursion


Best: O(n log(n)) Good case: poping and pushing are executed in constant time
Average: O(n log(n)) O(1).
Worst: O(n log(n)) Algorithms Worst case: When the stack is full -> create a new stack and
Space worst: O(1) Data Structures Linked List: copy -> O(n) time to finish.
Single Linked List
Data Structures and Algorithms
Sorting
Bubble Sort
Best O(n)
Average: O(n2) Doubly Linked List Stack: Last in First out Structures. Algorithms Delimitter matching
Worst: O(n2) Only be accessed at the end element
Space worst: O(1)

Data compression
Insertion Sort
Best: O(n)
Average: O(n2)
Worst: O(n2)
Space worst: O(1)

Breadth-first traversal
Selection Sort A node is visited first, then all it's children, then all its
Best: O(n2) grandchildren,....
Avarage: O(n2)
Worst: O(n2) Pre-order traversal
Space worst: O(1) A node is visited before its descendants
Travel
Breadth First Post-order travelsal
Depth First A node is visited after its descendants
Tree traversal
DIijkstra's Algorithm Is the process of visiting each node in the tree exactly one
Complexity: O(|V|2) time. Ordered tree
Graph
Floy's Algorithm
Complexity: O(|V|3)

Tree
Tree is an abstract model of a hierarchical structure
A tree consists of nodes with a parent-child relation Traversal:
Applications: Organization charts, File systems, Breadth-first traversal is visiting
Programming environments each node starting from the
Internal node: Node with at least one child lowest(or highest) level and moving
External(leaf) node: Node without children down(or up) level by level, visiting
Sub tree: A node with its descendants nodes on each level from left to
Level(depth) max = height Binary Tree right(or from right to left)
A tree which each node has at most 2 children Pre order: Root -> left -> right
Proper/Full binary tree : Every node other than the leaves In order: Left -> root -> right
has 2 children Post order: Left -> right -> root
Complete binary tree: All non-terminal nodes have both their
children, and all leaves are at the same level
Implementing
Arrays (Can't predict the size of the array)
Linked List

AVL Trees O(log n): is a height-balanced binary search tree


Balanced Binary Tree
Height-balanced : If for every internal node p of T, the heights
of the children of p differ by at most 1
Perfectly-balanced: If it is height-balanced and all leaves are
to be found on 1 level or 2 levels

Multiway Tree

Operation Delete: 3 cases


The node is a leaf
The node has 1 child
Deletetion by Merging
The node has 2 children --->
Bring the rightmost node of the left subtree or the leftmost
node of the right subtree to be the root of that 2 subtrees.
Binary search tree(BST) Then put it in the deleted node.
Is a node based binary tree data structure which has the
following properties:
The left node value < node value
The right node value > node value
--> left < node < right
Both left and right must be binary search trees Deletion by Copying
--> Each node value has a distinct value If the node has 2 children, the cases can be reduced to
The node is a leaf
The node has only one non-empty child
--> Solution: Replace the key being deleted with its immediate
predecessor(or successor)

You might also like