
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
646
|
Chapter 11: Data Structures and Algorithms
topLevel.DepthFirstSearch(b): b
topLevel.DepthFirstSearch(c): c
topLevel.DepthFirstSearch(d): d
topLevel.DepthFirstSearch(e): e
topLevel.DepthFirstSearch(f): f
d
Contains Left: NULL
Contains Right: f
e
Contains Left: NULL
Contains Right: NULL
f
Contains Left: e
Contains Right: g
g
Contains Left: NULL
Contains Right: NULL
d
Contains Left: NULL
Contains Right: NULL
See Also
See the “Queue Class” and “IComparable<T> Interface” topics in the MSDN docu-
mentation.
11.7 Creating an n-ary Tree
Problem
You need a tree that can store a number of child nodes in each of its nodes. A binary
tree will work if each node needs to have only two children, but in this case each
node needs to have a fixed number of child nodes greater than two.
Solution
Use the NTree<T> class shown in Example 11-14 to create the root node for the n-ary
tree.
Example 11-14. Generic NTree class
using System;
using System.Collections;
using System.Collections.Generic;
public class NTree<T> : IEnumerable<T>
where T : IComparable<T>
{
public NTree( )
{