CS 2150 Roadmap

Data Representation

Program Representation

 
 
string
 
 
 
int x[3]
 
 
 
char x
 
 
 
0x9cd0f0ad
 
 
 
01101011
vertical red double arrow  
Objects
 
Arrays
 
Primitive types
 
Addresses
 
bits
           
 
Java code
 
 
C++ code
 
 
C code
 
 
x86 code
 
 
IBCM
 
 
hexadecimal
vertical green double arrow  
High-level language
 
Low-level language
 
Assembly language
 
Machine code

Complex Relationships: Phylogenetic Tree

phylogenetic tree

Complex Relationships: Language Tree

language tree
language tree part

Tree Terms

  • Indo-European is the root
  • Nodes stacked on top of each other are siblings
  • English is a leaf, and has depth 5
    • Depth is the distance to the root
    • English → Old English → Anglo-Frisian → West Germanic → Germaic → Indo-European
  • Germanic is an internal node and has height 6
    • Height is the distance to the furthest leaf
    • Germanic → West Germanic → Low Franconian → West Low Franconian → Old Dutch → Dutch → Afrikaans
  • The tree rooted at Germanic forms a sub-tree of the overall tree

Other Examples of Trees

  • Files and folders on a computer
  • Compilers: parse tree
    a = (b+c) * d;
  • Genealogy trees
    • These become complicated with some complex family relationships
  • Lab 5: expression trees
expression tree 1

First child/next sibling

class TreeNode {
  private:
    string element;
    TreeNode *firstChild;
    TreeNode *nextSibling;
  public:
    // ...
}
file tree 1

Binary Trees

All nodes have at most 2 children

class BinaryNode {
  public:
    // ...
  private:
    int element;
    BinaryNode *left;
    BinaryNode *right;
};
bst-3
bst-1

Binary Trees: diagram details

In reality, any child not shown is really a NULL pointer, as shown here; but these are generally omitted from the diagrams

bst-4
bst-2

BST: insert

Do a find, and when we reach a NULL pointer, create a new node there

(no external source code)

void BST::insert(int x, BinaryNode * & curNode) {
    if (curNode==NULL)
        curNode = new BinaryNode(x,NULL,NULL);
    else if (x < curNode->element)
        insert(x, curNode->left);
    else if (x > curNode->element)
        insert(x, curNode->right);
    else
        ;    // duplicate... do nothing
}

BST: remove: no children

  • Just remove the node (reclaiming memory), adjusting the parent pointer to NULL
    • In this case, 9's left child link is changed to NULL
bst-7 bst-8

BST: remove: one child

  • Adjust pointer of parent to point at child, and reclaim memory
    • In this case, 4's left pointer is changed to point to 3
bst-9 bst-10

BST: remove: two children

  • Replace node with successor, then remove successor from tree
    • This requires running findMin() on the right sub-tree, and then removing that element
    • In this case, 5 is replaced by 7 (and the node that had 7 is removed)
bst-11 bst-12

BST Height

  • n-node BST: Worst case depth is n-1
  • This can easily happen if the data to be inserted is already sorted
  • Claim: The maximum number nodes in a binary tree of height h is 2h+1-1
bst-1

AVL single right rotation

avl-tree-18 avl-tree-19
  • The node just inserted was node 1 (blue)
  • The lowest node, immediately after the insert, with an imbalance is node 3 (red)
  • Because node 1 is in the "left subtree of the left child" of node 3, this means we need to perform a single right rotation

AVL single left rotation

avl-tree-20 avl-tree-19
  • The node just inserted was node 3 (red)
  • The lowest node, immediately after the insert, with an imbalance is node 1 (blue)
  • Because node 3 is in the "right subtree of the right child" of node 1, this means we need to perform a single left rotation

A side-effect of tree rotations

avl-tree-18 avl-tree-19
  • This is the single right rotation
  • Note that at least one node moves "up" (depth decreases)
    • In this case, nodes 1 and 2 both move up
  • And at least one node moves "down" (depth increases)
    • In this case, node 3 moves down
  • Similarly for a left rotation

AVL single right rotation: before & after

avl tree 4 avl tree 5
  • Node 1 (red) is what is being inserted
  • The lowest node with an imbalance is node 5 (balance: -2)
  • Because the insert was in 5's "left subtree of the left child", we perform a single right rotation on 5 (and its left child, 3)

AVL single right rotation: before & after

avl tree 4 avl tree 5
  • From the previous slide, we know we perform a single right rotation on 5 (and its left child, 3)
  • Thus, the two blue nodes are the 'pivots' of the rotation
  • Note that node 4 changes parents (from 3's right to 5's left)

AVL single right rotation: general case

avl tree 6 avl tree 7

\( X < b < Y < a < Z \)

The insert is into sub-tree X, increasing its height to h+1

Notice how sub-tree Y changes parent

Cases 2 & 3: attempt a single rotation

avl tree 8 avl tree 9

\( X < b < Y < a < Z \)

The insert is into sub-tree Y, increasing its height to h+1

Failure! b's left subtree has height h+1; right is h+3

Double rotation

  • Node 5 (red) was just inserted
  • The lowest node with an imbalance is node 8 (balance factor: -2)
    • When discussing these rotations, we will call this the "parent" node
  • Because the insert happened in 8's "right subtree of the left child", we perform a double rotation
  • This consists of a single left rotation on the "child" (node 4), followed by a single right rotation on the "parent" (node 8)
avl tree 14
  • Note that the two rotations are in different directions!

Double rotation, step 1

avl tree 10 avl tree 11

This is the single left rotation on the "child". The red node is what was inserted; the blue nodes are the 'pivots' of this single left rotation.

Double rotation, step 2

avl tree 12 avl tree 13

This is the single right rotation on the "parent". The red node is what was inserted; the green nodes are the 'pivots' of this single right rotation.

AVL double rotation: before & after

avl tree 14 avl tree 15

The red node is what was inserted

AVL double rotation: general case

avl tree 16
avl tree 17
 
\( W < b < X < c < Y < a < Z \)
 
The insert happens into X
Notice sub-trees X and Y change parents

Algorithmic determination of rotation

avl tree 21 avl tree 22 avl tree 23 avl tree 24
left-left
case
right-right
case
left-right
case
right-left
case
    
  • Given the lowest unbalanced node, and the child in the direction of the insert, compare the balance factors
  • -2/+1 means a double left-right, +2/+1 means a single left, etc.

All the tree rotations

tree rotations

The program tree

program tree