11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

210 Chap. 6 Non-Binary TreesRRABABCDEFC D E F(a)(b)Figure 6.13 A dynamic general tree represent<strong>at</strong>ion with linked lists of childpointers. (a) The general tree. (b) The tree represent<strong>at</strong>ion.6.3.4 Dynamic “Left-Child/Right-Sibling” Implement<strong>at</strong>ionThe “left-child/right-sibling” implement<strong>at</strong>ion of Section 6.3.2 stores a fixed numberof pointers with each node. This can be readily adapted to a dynamic implement<strong>at</strong>ion.In essence, we substitute a binary tree for a general tree. Each node of the“left-child/right-sibling” implement<strong>at</strong>ion points to two “children” in a new binarytree structure. The left child of this new structure is the node’s first child in thegeneral tree. The right child is the node’s right sibling. We can easily extend thisconversion to a forest of general trees, because the roots of the trees can be consideredsiblings. Converting from a forest of general trees to a single binary treeis illustr<strong>at</strong>ed by Figure 6.14. Here we simply include links from each node to itsright sibling <strong>and</strong> remove links to all children except the leftmost child. Figure 6.15shows how this might look in an implement<strong>at</strong>ion with two pointers <strong>at</strong> each node.Compared with the implement<strong>at</strong>ion illustr<strong>at</strong>ed by Figure 6.13 which requires overheadof three pointers/node, the implement<strong>at</strong>ion of Figure 6.15 only requires twopointers per node. The represent<strong>at</strong>ion of Figure 6.15 is likely to be easier to implement,space efficient, <strong>and</strong> more flexible than the other implement<strong>at</strong>ions presentedin this section.6.4 K-ary TreesK-ary trees are trees whose internal nodes all have exactly K children. Thus,a full binary tree is a 2-ary tree. The PR quadtree discussed in Section 13.3 is anexample of a 4-ary tree. Because K-ary tree nodes have a fixed number of children,unlike general trees, they are rel<strong>at</strong>ively easy to implement. In general, K-ary trees

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!