Data Representation | Program Representation | |||||
string int x[3] char x 0x9cd0f0ad 01101011 |
Objects Arrays Primitive types Addresses bits |
Java code C++ code C code x86 code IBCM hexadecimal |
High-level language Low-level language Assembly language Machine code |
|
|
All nodes have at most 2 children
|
In reality, any child not shown is really a |
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
}
NULL
NULL
→ |
→ |
findMin()
on the right sub-tree, and then removing that element→ |
|
→ |
→ |
→ |
→ |
→ |
→ |
\( 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
→ |
\( 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
|
|
|
→ |
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.
→ |
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.
→ |
The red node is what was inserted
|
left-left case | right-right case | left-right case | right-left case |