Javascript data structures - the binary tree |

Written by Mike James | ||||

Thursday, 18 September 2014 | ||||

Page 1 of 3 Binary trees in JavaScript? Easy with the right storage mapping function. Find out how to code a binary tree right up to a depth first traversal.
This is one of a series of articles on implementing data structures in JavaScript. ## JavaScript Data Structures- String Object
- Array object
- Associative Array
- Speed dating - the art of the JavaScript Date object
- Collection Object
- Stacks
- Linked List
- a Lisp-like list
- Binary Tree
- Typed Arrays I
- Typed Arrays II
- Reading A BMP File In JavaScript
The binary tree is one of the more useful of the "advanced" data structures and it is fairly easy to implement in JavaScript. However the usual method is to use object references, links or pointers and this makes searching the tree an expensive task. To find a given node in the tree you generally have to traverse the tree and examine all the nodes on the way to the target node. For trees with fixed branching factors e.g. a binary tree with two child nodes attached to every parent node or a ternary tree with three child nodes and so on... there is a very simple but often overlooked way of doing the job. Instead of using references to link the tree together you can use a storage mapping function to store the data at fixed location. You can think of it as an example of a perfect hash function if you want to but it is simpler to think in terms of storage mapping functions. In a nutshell a Storage Mapping Function SMF is a function that when given a set of indices that specify which element of a structure you want it returns the address or location of the element. For example for an array you would give the SMF the index of the array element you want and it gives you the address where it is stored. Notice that this method only works for trees with a fixed branching factor and no "missing" nodes i.e. every tree is complete and balanced.
## The Binary Tree SMFA binary tree is a tree structure such that each node, apart from the final or terminal nodes, has exactly two children - hence the binary in binary tree. You can specify the element of the tree that you want by giving two indices - the level i.e. how far down the tree and the node number at that level i.e. how far to the right the node is. Each node is associated with a value that takes n bytes to store then a suitable SMF is:
which gives the location of the value stored at ith node at the jth level. To see why this works consider the following small binary tree:
and so on. Notice that going from one level to the next doubles the number of nodes and you should be able to work out that the number of nodes at level j is simply 2 With this insight you should now also be able to see how the SMF works. The tree is stored as lines of nodes one level at a time. That is:
Storage mapping functions are used in low level code to create the data structures that high level language offer. But there is no reason they can't be used in a high level language to create new structures. To make use of this SMF in a high-level language all you have to do is declare an array of elements that will hold the node values and use the SMF with n=1 to determine which element node i at level j is stored in. You should be able to see that you can create a storage mapping function for any tree with a constant branching factor b. All you have to do is change the 2 to b to give:
## A JavaScript binary treeThe JavaScript Array object can be used to store any object at an indexed location so using this as the basic storage component for out binary tree is an obvious choice. The first thing we need is the storage mapping function and here we hit our first minor problem. JavaScript doesn't have a "raise to a power" operator. Instead it has the pow method of the Math object. For example,
raises 2 to the kth power. This is what we would have to use for a general tree with a b way branch at each node but for a binary tree we can take a slightly different and theoretically more efficient route. The shift operators >> and << are equivalent to divide by 2 and multiply by 2 respectively. So
gives the result 2^k. Using this the storage mapping function:
is easy enough to translate into JavaScript. Taking an object oriented approach let's package everything into a BinaryTree object and its associated constructor function:
From this point on everything will be defined within the BinaryTree constructor function - a complete listing can be found at the end if you get confused about what goes where. The btSMF function takes a node and level number and returns the location within a standard Array that the node should be stored. We need to declare the array but JavaScript arrays are so good natured we don't have to specify its size - it will grow as needed.
## Get And SetNext we need a basic set and get functions for a node.
These two functions simply store and retrieve the data at the location indicated by the storage mapping function. To try them out let's store a simple three level tree as shown in the diagram above:
and to retrieve a value you would use something like:
<ASIN:0137054890> <ASIN:0596517742> |
||||

Last Updated ( Thursday, 18 September 2014 ) |