Balanced Binary Tree Algorithmby Per NilssonSource Code: BinaryTreeDemo.zip 10 KB Environment: Developed in (but not restricted to) VC6 This article give a brief, non-academic (=no dwelling into time complexity issues), explanation of what binary trees are about. It also provide source and demo of a generic tree class. Functionality to balance the tree is also described/provided. About binary treesSay you have a list or array holding N numbers of elements, and want to search for a specific element. You then have to go through (aka traverse) each element one by one until you find it (or find out that it is not in the list). The worst case (if the element is last in the list, or isn‘t there at all) you‘d have to make N comparisons, which can be quite a treat if the list/array is VeryVeryBig (tm). By having the elements stored in a tree rather a list you get some key benefits:
How does it work thenIn a linked list you have a line of elements, each elements pointing to the next:
In a binary tree, each element instead point to two other elements, one "down to the left" (left child) and one "down to the right" (right child). The left child‘s key value is less than its parent‘s, and the right child‘s key value is greater than or equal than its parent‘s:
Thanks to this, can you fairly easy decide where to search (and where to not search) for a particular element. If the current node doesn‘t match the search criteria, query the left or the right child, depending of wheter the search criteria is < or >= than current node‘s key value. SortingSince all elements are stored according to their key values, you can easily retrieve them in an ordered (or reversed) manner. Oh, and when working with trees, you really see the benefits of using recursion... Example of printing the elements in order:
Example of printing the elements in reversed order:
Bringing balance to the forceNow...there is a slight catch of course :-). The order in which you add elements to the tree affect how the tree looks. Lets say we have a bunch or integer elements 1..9 If they are added to the tree in this order: 3,6,4,8,1,9,2,7,5, you‘d get a tree looking somewhat like this:
BUT if they instead are added in order (1,2,3,4,5,6,7,8,9) you get a tree looking like this, which pretty much is a linked list that has no benefits of actually supporting a tree structure.
This is where the topic of balance come in, the more evenly distributed the elements are, the better the tree is balanced. Fig 3 above shows a somewhat balanced tree, fig. 4 shows an utterly unbalanced tree. So, how do you keep the tree balanced? Well you could...
There are probably more variants, but I‘ll illustrate one way of Rebuild the entire tree, making it balanced. This is also how it is done in the source code provided. Rebuild the entire treeSo...you have a tree you want to balance...here‘s one way to do it:
(1) is easily done. As mentioned, a tree is sorted "by default", its just a matter of traversing properly. If we are using pointers to the elements (we are) we don‘t copy the elements, but just copy the pointers. (2) is also quite easy, simply remove the top most element. (3) now this is a tad bit tricky....what do we mean by "un-ordered". Well, anyway, we want the "middle-most" element to be on the top, and then the middle-most of the left portion to be left child and the middle-most of the right portion to be right child etc, kinda like this:
Hmmmm...feels like something about recursion is afoot:
Deleting elementsAs you probably realize can you not just delete an element and get away with it, the child elements would then be lost in cyberspace. First of all, if you‘re gonna delete the Element E, you will have to do something to E‘s parent, like NULLing its child reference to E. There are essentially two ways of getting the parent of an element:
So, how to remove the element, E? One quite simple solution is to:
Example:
Hmmm...that‘s about it. For more details, check the demo project‘s source code. DownloadsThe demo project is a console application holding the source (commented) to a generic tree structure and some stuff illustrating its use. It also shows how to have a quite flexible tree traversing mechanism using callback functions. Key files:
Contact the Author: Per Nilsson |
|