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 data and provides a function for index (i.e. hash code) ca
/ Inherit1.cpp : Defines the entry point for the console application. / #include "stdafx.h" #include <iostream> using namespace std; #define BEGIN { #define END } class Mammal BEGIN public: Mammal():itsAge(1) BEGIN cout < "Mammal constructor.\n"; END
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 parent node child node leaf (no children)
sub-tree Bina
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 the parent nodes value
BST Example BST
BST Search Algor
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 all values in the nodes subtrees
Inserting into Max He
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 data compression.
Huffman Tree
Huffman Tree is a binary tr
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 retrieving elements by key)!
Sets
Definition: Set is an associat
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 linear search is still O(N) SLOW! Some of you stored Huffm
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 element Swap the smallest element with the one at curr
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 for N-1 and combine this smaller solution with the case
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
Queue: Push Queue:
Max Lex Joe
Push
top
Max Lex Nika Vlad J
/ 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, _TCHAR* argv[]) BEGIN ofstream testout; char infi
#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); virtual ~Animal() { cout < "Animal destructor.\n"; } v
/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 Fish { public: Animal(int); virtual ~Animal() { cou
/ 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 object CWinApp theApp; using namespace std; int _tmain
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)
GET "libhdr" LET start() = VALOF cfw_ FOR i = 1 TO 5 DO
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 using a concrete type. Templates are powerful because the
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 accommodate more or less data.
C+ Vector = Array with Bene
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. not by index) - queues, stacks, linked lists - Require i
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
Joe
Max Lex Nika Vlad
top
Joe Max Lex Nika Vlad
top
St
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. Analyze requirements to make sure that they are consi