Draft for an AVL tree insertion visualization with multiple levels of engagement T-106.6200 Special Course in Software Techniques: Directions for Future Research in Algorithm Visualization Lasse Hakulinen 2011-06-23 A V
AVL tree insertion Mode 1: Animation
The AVL tree is now balaced Insert the next key (5) to the AVL tree Input: 5 2 3 9 10 4 1 Balance factor = height of left subtree – height of right subtree 5
Insert the next key (2) to the AVL tree The AVL tree is now balaced Input: 5 2 3 9 10 4 1 5 1 2
Insert the next key (0) to the AVL tree The tree is unbalanced: The balance factor of node with key ”5” is 2. Do a single rotation right. Insert the next key (0) to the AVL tree Input: 5 2 3 9 10 4 1 5 2 1 2 1
The AVL tree is now balaced The tree is unbalanced: The balance factor of the node with key ”5” is 2. Do a single rotation right. Input: 5 2 3 9 10 4 1 5 1 2
Mode 2: User defined input AVL tree insertion Mode 2: User defined input
The AVL tree is now balaced Insert a key Input key: 12 Insert Insert random key 12
The AVL tree is now balaced Insert a key The AVL tree is now balaced Input key: 100 Insert Insert random key 12 -1 100
The tree is unbalanced: The balance factor of node with key ”12” is -2. Do a right-left double rotation Insert a key Input key: Insert Insert random key 12 -1 -2 TODO: Smooth animation of the rotation 1 100 13
The AVL tree is now balaced Input key: Insert Insert random key 13 12 100
Mode 3: Challenge yourself AVL tree insertion Mode 3: Challenge yourself
Task 1: Insert a key so that the node ”21” will be the new root The tree is unbalanced: The balance factor of node with key ”5” is -2. Do a single left rotation Input key: 30 Insert Insert random key 5 -1 -2 TODO: Smooth animation of the rotation 3 21 -1 -1 11 28 30
Task correct! Input key: Insert Insert random key 21 5 28 -1 30 3 11
Task 2: Insert a key so that the node ”11” will be the new root The tree is unbalanced: The balance factor of node with key ”5” is -2. Do a double right-left rotation Task 2: Insert a key so that the node ”11” will be the new root Input key: 7 Insert Insert random key 5 -1 -2 TODO: Smooth animation of the rotation 3 21 1 1 11 28 7
Task correct! Input key: Insert Insert random key 11 5 21 -1 28 3 7
Task 3: Insert maximum of 3 keys so that the right child of root's left child (highlighted) has a key ”16”. The tree is unbalanced: The balance factor of node with key ”20” is 2. Do a double left-right rotation Input key: 16 Insert Insert random key 20 1 2 TODO: Smooth animation of the rotation -1 9 27 -1 3 14 16
Task 3: Insert maximum of 3 keys so that the right child of root's left child (highlighted) has a key ”16”. Input key: Insert Insert random key 14 1 9 20 3 16 27
Let's try again...
Task 3: Insert maximum of 3 keys so that the right child of root's left child (highlighted) has a key ”16”. Input key: 30 Insert Insert random key 20 1 -1 9 27 3 14 30
Task 3: Insert maximum of 3 keys so that the right child of root's left child (highlighted) has a key ”16”. Input key: 16 Insert Insert random key 20 -1 9 27 3 14 30 16
Task 3: Insert maximum of 3 keys so that the right child of root's left child (highlighted) has a key ”16”. Input key: 17 Insert Insert random key 20 1 -1 -1 9 27 -1 3 14 30 16 17
Task 3: Insert maximum of 3 keys so that the right child of root's left child (highlighted) has a key ”16”. The tree is unbalanced: The balance factor of node with key ”14” is -2. Do a single left rotation Input key: 17 Insert Insert random key 20 2 TODO: Smooth animation of the rotation -2 -1 9 27 -2 3 14 30 -1 16 17
Task 3: Insert maximum of 3 keys so that the right child of root's left child (highlighted) has a key ”16”. Task correct! Input key: Insert Insert random key 1 20 -1 -1 9 27 3 16 30 14 17