Home
Browse all 647 examples
Download all 647 examples
Download sample chapters
Reviews
Errata
Acknowledgments
Links on .NET
Paradoxal Press
Buy directly from Paradoxal Press at $33.99 (Save 43%)
Category: Programming
Level: Beginner to seasoned
900 pages
ISBN-10 097661322-0
ISBN-13 978-097661322-0
$59.99 USA
$79.99 CANADA
|
Listing 14-42 extracted from chapter Unsafe code, exceptions, anonymous methods, iterators
Listing 14-41< > Listing 14-43
This listing can be compiled with the command line: csc.exe /target:exe Example_14_42.cs Errors: 0 Warnings: 0
Example_14_42.cs
using System.Collections.Generic;
public class Node<T> {
public Node( T item , Node<T> leftNode , Node<T> rightNode ) {
m_Item = item;
m_LeftNode = leftNode;
m_RightNode = rightNode;
}
public Node<T> m_LeftNode;
public Node<T> m_RightNode;
public T m_Item;
}
public class BinaryTree<T> {
Node<T> m_Root;
public BinaryTree( Node<T> root ){
m_Root = root;
}
public IEnumerable<T> InOrder {
get{
return PrivateScanInOrder( m_Root );
}
}
private IEnumerable<T> PrivateScanInOrder( Node<T> root ) {
if ( root.m_LeftNode != null ) {
foreach ( T item in PrivateScanInOrder( root.m_LeftNode ) ) {
yield return item;
}
}
yield return root.m_Item;
if ( root.m_RightNode != null ) {
foreach ( T item in PrivateScanInOrder( root.m_RightNode ) ) {
yield return item;
}
}
}
}
class Program {
static void Main() {
BinaryTree<string> binaryTree = new BinaryTree<string> (
new Node<string>( "A",
new Node<string>( "B" , null , null ),
new Node<string>( "C" ,
new Node<string>( "D" , null , null ),
new Node<string>( "E" , null , null ) ) ) );
foreach ( string s in binaryTree.InOrder )
System.Console.WriteLine( s );
}
}
Copyright Patrick Smacchia 2006 2007
|