Binary Tree Representation
Binary Tree Representation
Binary trees are usually represented using nodes and pointers. A TreeNode class may be defined as:
In some cases, a node might also contain a parent pointer which facilitates a “bottom-up” traversal of the tree. The tree is accessed by a pointer root of type Tree Node* to its root. When the binary tree is complete (i.e., there are 2i nodes at every level i, except possibly the last level which has nodes filled in from left to right), it is convenient to use an array representation. The complete binary tree in Figure 3.6(b) can be represented by the array
Observe that the children (if any) of a node located at position i of the array can be found at positions 2i and 2i + 1 and its parent at li/2J.
Comments
Post a Comment