You are on page 1of 3

Trees / Binary search trees:

Moving through recursively:

template <class KeyType, class ValueType>


void MyMap<KeyType, ValueType>::insertNode(Node*& startingPoint, Node*&
toBeInserted)
{
if(startingPoint == NULL) // the funciton has found the location of insertion
{
startingPoint = toBeInserted;
}
else
{
if(toBeInserted->m_key < startingPoint->m_key) // traverse down the
left path
{
insertNode(startingPoint->m_leftChild, toBeInserted);
}
else // traverse down the right path
{
insertNode(startingPoint->m_rightChild, toBeInserted);
}
}
}

Some simple functions such as counting the number of nodes:

int nodecount(node* node)


{
if(node == NULL)
return 0;
else
{
for(int I = 0; I < node->children.size(); i++)
{
int m = nodecount(node->children[i]);
m++;
return m;
}
}
}

Big O look for loops. For each loop you most probably are going to
have an O(N) operation, and for nested loops that factor of N increases

Binary search tree O(logN)


Hash table O(1)

A complete binary tree is a tree in which all levels are filled except the
bottom level and its members are arranged in an order from left to
right

Class inheritance:
1. Construct the base
2. Construct the data members
3. Execute the body of the constructor

Similarly,
1. Execute the body of the destructor (derived)
2. Destroy the data members
3. Destroy the base part

How to manage a maxheap or a minheap:

Insertion:
Complete the binary tree
Compare the newly added node to its parent node
If greater swap them

Deletion:
Complete the binary tree by moving the last node to the top to
complete tree
Compare to both children and swap with greater of 2
Continue to trickle down to proper place

Heapsorting also is pretty efficient but it looks like its working in


reverse

A heap represented by an array

Each parent is floor((child-1)/2)

Each child is (2j + 1) and (2j + 2)

Templates:

template <class KeyType, class ValueType>


class MyMap{stuff};

When a function is written, you want to declare the thing again unless
you include it in the body of the class declaration
template <class KeyType, class ValueType>
const ValueType* MyMap<KeyType, ValueType>::find(const KeyType& key)
const

You might also like