@@ -6,42 +6,39 @@ When implementing an AVL tree, there are some choices:
66* Storing the balance factor or height in a node.
77* Taking a recursive or iterative approach.
88
9- This is an iterative implementation of an AVL tree that stores the height in a node and does not store the parent pointer in it. Key points:
9+ This is an iterative implementation of an AVL tree that stores the height in a node
10+ and does not store the parent pointer in it. Key points:
1011* Written in pure Rust.
11- * The key data type can be any type that has Ord.
12+ * The key type can be any type that has ` Ord ` .
1213* Each node is identified by a key, and so there are no nodes with the same key.
1314
1415## Description
1516<picture >
1617 <source media =" (prefers-color-scheme: dark) " srcset =" img/AVLTreeDarkTheme.png " >
1718 <source media =" (prefers-color-scheme: light) " srcset =" img/AVLTreeLightTheme.png " >
18- <img src =" img/AVLTreeDarkTheme.png " width =" 650 " >
19+ <img src =" img/AVLTreeDarkTheme.png " width =" 620 " >
1920</picture >
2021
2122** A diagram of an AVL tree with a balance factor labeled below each node.**
2223
23-
2424The AVL tree, named after its inventors, is a self-balancing binary search tree (BST)
2525that has a worst-case time complexity of $O(log(n))$ on lookup, insertion, and deletion.
2626
2727An ordinary (unbalanced) BST has a worst-case time complexity of $O(n)$ for the basic operations.
28- Without rebalancing, the tree height keeps increasing on insertion, and the performance degrades to that of a linked list, that is, linear time.
28+ Without rebalancing, the tree height keeps increasing on insertion,
29+ and the performance degrades to that of a linked list, that is, linear time.
2930
3031Therefore, rebalancing through tree rotations is necessary to keep the useful property of BSTs.
3132
32- Here is
33- [ an interactive tool to visualize how AVL trees work] ( https://www.cs.usfca.edu/~galles/visualization/AVLtree.html ) .
34-
35- ## License
36- All code, including the test module, is released under the MIT license.
37-
38- The diagram above is my own work and is released into the public domain (CC0).
33+ Here is an
34+ [ interactive tool to visualize how AVL trees work] ( https://www.cs.usfca.edu/~galles/visualization/AVLtree.html ) .
3935
4036## Definitions
4137Below I provide the definitions used throughout this repository.
4238
4339### Depth
44- The depth of a node is the depth of its parent plus one. An empty node's depth is zero. A root node has no parent, thus its depth is $0 + 1 = 1$.[ ^ 1 ]
40+ The depth of a node is the depth of its parent plus one. An empty node's depth is zero.
41+ A root node has no parent, thus its depth is $0 + 1 = 1$.[ ^ 1 ]
4542
4643### Height
4744The height of a node $X$ is the maximum of the heights of its children plus one:
@@ -71,3 +68,21 @@ In the diagram above, the root node $R$ is left-heavy, since $BF(R) = Height(S)
7168[ ^ 1 ] : This differs from the usual definition, where a root's depth is zero.
7269[ ^ 2 ] : This differs from the usual definition, where a leaf's height is zero.
7370[ ^ 3 ] : In the usual definition, it's three.
71+
72+ ## Implementation notes
73+ All primitive integer types have ` Ord ` , so they can be used as the key type. So does ` String ` , since it has ` Ord ` .
74+
75+ IEEE 754 floating-point arithmetic has a special value called "Not a Number" (NaN),
76+ which is not equal to anything, not even to itself.
77+ Because NaN violates the [ trichotomy] ( https://en.wikipedia.org/wiki/Law_of_trichotomy ) ,
78+ floating-point types like ` f32 ` cannot have ` Ord ` , so they cannot be used as the key type.
79+
80+ A key-value pair is stored in the node itself.
81+ During a tree rotation, everything of the node is swapped with another (using ` std::mem::swap ` ).
82+ Therefore, if the key or value were large, expensive memory copies might take place during insertion or deletion.
83+
84+ ## License
85+ All code, including the [ test module] ( src/avl_test.rs ) , is released under the MIT license.
86+
87+ The diagram above is my own work and is released into the public domain
88+ ([ CC0] ( https://creativecommons.org/publicdomain/zero/1.0/ ) ).
0 commit comments