BINARY SEARCH TREE ALGORITHMS
A Binary Search Tree (also called “Ordered Binary Tree” or “Sorted Binary Tree”), is a Rooted Binary Tree possessing Nodes that each
stores a “Key Value” greater than all the key values in its Left Sub-tree and a Key Value less than all the key values in its Right
Sub-tree. Binary Trees are usually implemented in order to store data such as numbers in a pre-defined strictly organised way. They
allow for all the operational advantages of Binary Search (fast lookup, random insertion & elimination of Nodes, etc.) and they are
ideal for developing both Dynamic Sets and Lookup Tables.
The C++ Code provided here is for the “Classic” implementation of the Binary Search Tree Algorithm & two of the nowadays most popular
variations on the Classic implementation, the “AVL Binary Search Tree” and the “Red-Black Binary Search Tree”.
The "Classic" Implementation:
The "AVL" Implementation (Original Algorithm):
AVL Binary Search Trees are a kind of self-balancing Binary Search Trees that maintain the difference between heights
of Left and Right sub-trees balanced by not allowing their difference to be more than one for all Nodes.
Tree Rotation is an operation that changes the structure of the Tree without interfering with the order of the elements
on the AVL Binary Search Tree. The aim is to move one Node “up” in the structure of the Tree and one Node “down”. Thus,
it is used to change the shape of the AVL Binary Search Tree, and to decrease its Height by moving Smaller sub-trees
“down” and Larger sub-trees “up”, resulting in the improved performance of many classic Tree operations. The Direction
of the Rotation depends on the side which the AVL Binary Search Tree Nodes are shifted upon, whilst in other schemes
than the traditional one it may Depend on which Child Node takes the Root Node’s place.
Function Descriptions (In The C++ Code):
TreeHeight(AVL *): Calculates the Height of the given AVL Binary Search Tree.
SubTreesHeightDifference(AVL *): Calculates the difference between Height of sub-trees of a given AVL Binary Search Tree.
AVL *RightToRightRotation(AVL *): A Right-To-Right Rotation is a combination of Right Rotation followed by another Right
Rotation.
AVL *LeftToLeftRotation(AVL *): A Left-To-Left Rotation is a combination of Left Rotation followed by another Left Rotation.
AVL *Left-To-RightRotation(AVL *): A Left-To-Right Rotation is a combination of Left Rotation followed by another Right
Rotation.
AVL *Right-To-LeftRotation(AVL *): A combination of Right Rotation followed by a Left Rotation.
AVL * BalanceTree(AVL *): Performs the Balance operation to the AVL Binary Research Tree by getting the Balance Factor.
AVL * InsertNode(AVL *, int): Performs the Insert Node operation by inserting Nodes in the AVL Binary Search Tree.
DisplayTree(AVL *, int): Displays the Nodes of the AVL Binary Search Tree.
InOrder(AVL *): Traverses an AVL Binary Search Tree in an “In-order” manner.
PreOrder(AVL *): Traverses an AVL Binary Search Tree in a “Pre-order” manner.
PostOrder(AVL *): Traverses an AVL binary Search Tree in a “Post-order” manner.
Download "BinarySearchTreeClassic"
Download "BinarySearchTreeAVL(I)"
The "AVL" Implementation (Variation):
Download "BinarySearchTreeAVL(II)"
The "Red-Black" Implementation (Original):
Download "BinarySearchTreeRedBlack(I)"
The "Red-Black" Implementation (Variation):
Download "BinarySearchTreeRedBlack(II)"