Hash Table
How can we retrieve a value (by key) from an associative container in O(1) time? Yes we can, with the help of a Hash Table! Hash table typically implements an internal array for storing dat
/ IOtest.cpp : Defines the entry point for the console application. / #include "stdafx.h" #include <fstream> #include <iostream> using namespace std; #define BEGIN { #define END } int _tmain(int argc,
Binary Search Tree (BST)
Properties: - Each node has a value - The left subtree contains only values less than the parent nodes value - The right subtree contains only values greater than or equal to
Max Heap
Properties: - The value in each node is greater than all values in the nodes subtrees - Complete tree! (fills up from left to right)
Min Heap
Properties: - The value in each node is less than
Huffman Code
Huffman code uses different number of bits to represent characters: - fewer for frequent characters - more for rare characters. Example: e = 10, z = 1001101 Huffman codes are used for dat
Associative Containers
Data Retrieval Sequential Container O(N) Binary Search Tree O(logN) SLOW! How about constant time, O(1)? HOW? Use associative containers (e.g. sets and maps that allow retrievin
Character to Code Mapping
Recall Huffman Codec To find Huffman code for a character one had to search the Huffman tree O(N) Alternatively, one could store Huffman character codes in array, but the lin
Selection Sort
We are sorting an array of N elements. Algorithm 1) Set currentIndex = 0 2) For each sub-array that starts from currentIndex and goes until the end of the array (N-1): Find the smallest
Trees Trees
Nonlinear Containers
Containers we have studied so far are linear. To represent nonlinear, i.e. hierarchal data we use trees.
edge edge edge root node node leaf
Tree Terminology Tree
root
Recursion
Suppose you have a problem involving N data points. Recursive solution of such problem is a follows: If the problem can be solved directly for N points then solve it Else Solve the problem f
Queue
Queue First In / First Out (FIFO) data structure that supports two operations: - push for adding new element at the end of the queue - pop for removing new element at the front of the queue
Queu
#include <iostream> using namespace std; typedef int HANDS; enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ; class Animal / common base to both horse and bird { public: Animal(int); virt
/examples of Pure virtual functions /Note syntax #include <iostream> using namespace std; enum COLOR { Red, Green, Blue, Yellow, White, Black, Brown } ; class Animal / common base to both Mammal and F
/ Win23ConsApp.cpp : Defines the entry point for the console application. / #include "stdafx.h" #include "Win23ConsApp.h" #ifdef _DEBUG #define new DEBUG_NEW #endif / The one and only application obje
C+ History C+
CPL Combined Programming Language (1960s, University of Cambridge, University of London) BCPL Basic Combined Programming Language (1966, Martin Richards, language for writing compilers)
C+ Templates
Template is a declaration (similar to class declaration), which deals with generic types. Templates are evaluated to produce concrete classes when a template-base class is instantiated us
C Arrays
C arrays are limited: - they are represented by pointers (which may or may not be valid); - Indexes not checked (which means you can overrun your array); - Arrays cannot grow or shrink to acc
Containers
Hold data and provide access to it. Random-access containers: - Allow accessing any element by index - arrays, vectors Sequential containers: - Allow accessing elements sequentially (i.e. n
Stack
Last In First Out (LIFO) data structure that supports two operations: - push for adding new element at the top of stack - pop for removing new element at the top of stack
Stack: Push Stack:
Push
Problem Solving
1) 1) 2) 3) 4) Never start coding unless you understand the task! Gather requirements first. This means identify the problem and ask questions about it. Now you kind of know what to do