T
Tony Johansson
Hello!
At the end I have a complete working program that creates a Binary Tree
using the generic class Tree<T>.
The generic class Tree<T> is implemneting the generic interface
IEnumerable<T> so we can use
foreach on the Binary tree. We use a iterator to implement the
IEnumerator<T>(Current,MoveNext).
Now to my question can somebody explain this code GetEnumerator(). I know it
uses recursion because I have run it through
the debugger. I find it hard to understand how recursion is used in the
code.
If I look at the method WalkTree below that is using recursion. It much more
easy to understand the code that use WalkTree
then to understand the code GetEnumerator. Both is using recursion but I can
see directly that WalkTree is using recursion
but it's not possible to see that GetEnumerator is using recursion.
public IEnumerator<T> GetEnumerator()
{
if (this.left != null)
{
foreach (T item in this.left)
{
yield return item;
}
}
yield return this.data;
if (this.right != null)
{
foreach (T item in this.right)
{
yield return item;
}
}
}
public void WalkTree()
{
if (this.LeftTree != null)
{
this.LeftTree.WalkTree();
}
Console.WriteLine(this.NodeData);
if (this.RightTree != null)
{
this.RightTree.WalkTree();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
public class Tree<T> : IEnumerable<T> where T : IComparable<T>,
IComparable
{
private T data;
private Tree<T> left;
private Tree<T> right;
public Tree(T data)
{
this.data = data;
left = null;
right = null;
}
public Tree<T> RightTree
{
get { return right; }
set { right = value; }
}
public Tree<T> LeftTree
{
get { return left; }
set { left = value; }
}
public T NodeData
{
get { return data; }
set { data = value; }
}
public void Insert(T newItem)
{
T currentItem = this.NodeData;
if (currentItem.CompareTo(newItem) > 0)
{
if (this.LeftTree == null)
{
this.LeftTree = new Tree<T>(newItem);
}
else
{
this.LeftTree.Insert(newItem);
}
}
else
{
if (this.RightTree == null)
{
this.RightTree = new Tree<T>(newItem);
}
else
{
this.RightTree.Insert(newItem);
}
}
}
public void WalkTree()
{
if (this.LeftTree != null)
{
this.LeftTree.WalkTree();
}
Console.WriteLine(this.NodeData);
if (this.RightTree != null)
{
this.RightTree.WalkTree();
}
}
public IEnumerator<T> GetEnumerator()
{
if (this.left != null)
{
foreach (T item in this.left)
{
yield return item;
}
}
yield return this.data;
if (this.right != null)
{
foreach (T item in this.right)
{
yield return item;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
class Program
{
private static Tree<T> BuildTree<T>(params T[] data) where T :
IComparable<T>, IComparable
{
Tree<T> sortTree = new Tree<T>(data[0]);
for (int i = 1; i < data.Length; i++)
{
sortTree.Insert(data);
}
return sortTree;
}
static void Main(string[] args)
{
Tree<int> intTree = BuildTree<int>(1, 5, 3, 2, 45, 7, 8, 9, 0, 12,
65, 566, 11, 44, 66, 77);
foreach (int item in intTree)
Console.WriteLine(item);
}
}
//Tony
At the end I have a complete working program that creates a Binary Tree
using the generic class Tree<T>.
The generic class Tree<T> is implemneting the generic interface
IEnumerable<T> so we can use
foreach on the Binary tree. We use a iterator to implement the
IEnumerator<T>(Current,MoveNext).
Now to my question can somebody explain this code GetEnumerator(). I know it
uses recursion because I have run it through
the debugger. I find it hard to understand how recursion is used in the
code.
If I look at the method WalkTree below that is using recursion. It much more
easy to understand the code that use WalkTree
then to understand the code GetEnumerator. Both is using recursion but I can
see directly that WalkTree is using recursion
but it's not possible to see that GetEnumerator is using recursion.
public IEnumerator<T> GetEnumerator()
{
if (this.left != null)
{
foreach (T item in this.left)
{
yield return item;
}
}
yield return this.data;
if (this.right != null)
{
foreach (T item in this.right)
{
yield return item;
}
}
}
public void WalkTree()
{
if (this.LeftTree != null)
{
this.LeftTree.WalkTree();
}
Console.WriteLine(this.NodeData);
if (this.RightTree != null)
{
this.RightTree.WalkTree();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
public class Tree<T> : IEnumerable<T> where T : IComparable<T>,
IComparable
{
private T data;
private Tree<T> left;
private Tree<T> right;
public Tree(T data)
{
this.data = data;
left = null;
right = null;
}
public Tree<T> RightTree
{
get { return right; }
set { right = value; }
}
public Tree<T> LeftTree
{
get { return left; }
set { left = value; }
}
public T NodeData
{
get { return data; }
set { data = value; }
}
public void Insert(T newItem)
{
T currentItem = this.NodeData;
if (currentItem.CompareTo(newItem) > 0)
{
if (this.LeftTree == null)
{
this.LeftTree = new Tree<T>(newItem);
}
else
{
this.LeftTree.Insert(newItem);
}
}
else
{
if (this.RightTree == null)
{
this.RightTree = new Tree<T>(newItem);
}
else
{
this.RightTree.Insert(newItem);
}
}
}
public void WalkTree()
{
if (this.LeftTree != null)
{
this.LeftTree.WalkTree();
}
Console.WriteLine(this.NodeData);
if (this.RightTree != null)
{
this.RightTree.WalkTree();
}
}
public IEnumerator<T> GetEnumerator()
{
if (this.left != null)
{
foreach (T item in this.left)
{
yield return item;
}
}
yield return this.data;
if (this.right != null)
{
foreach (T item in this.right)
{
yield return item;
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
class Program
{
private static Tree<T> BuildTree<T>(params T[] data) where T :
IComparable<T>, IComparable
{
Tree<T> sortTree = new Tree<T>(data[0]);
for (int i = 1; i < data.Length; i++)
{
sortTree.Insert(data);
}
return sortTree;
}
static void Main(string[] args)
{
Tree<int> intTree = BuildTree<int>(1, 5, 3, 2, 45, 7, 8, 9, 0, 12,
65, 566, 11, 44, 66, 77);
foreach (int item in intTree)
Console.WriteLine(item);
}
}
//Tony