By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,142 Members | 732 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,142 IT Pros & Developers. It's quick & easy.

Write to a file all of the values of a Binary Tree Nodes at the specified depth from.

P: 2
1. Implement the following C# method to write to a file all the values of the binary tree nodes at the specified depth from the root. Root has depth 0. Start from the leftmost node. An iterative solution is preferred over a recursive one.

class TreeNode
{
public int Value { get; set; }
public TreeNode LeftChild { get; set; }
public TreeNode RightChild { get; set; }
public TreeNode Parent { get; set; }
};

static void WriteTreeLevelToFile(
string filename,
TreeNode root,
int depth);

2. When I enter this code into Visual Studio I get an error at, "Static Void".

3. I need to write the results out to a file.

This is my try at the solution:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace MS_TreeDemo
{
class TreeNode
{
public int Value { get; set; }
public TreeNode LeftChild { get; set; }
public TreeNode RightChild { get; set; }
public TreeNode Parent { get; set; }
};

static void WriteTreeLevelToFile(
string ("J:\MS_TreeDemo\TreeNode.txt"),
TreeNode root,
int depth);

}
}



Thanks for your help.
May 13 '10 #1
Share this Question
Share on Google+
4 Replies

tlhintoq
Expert 2.5K+
P: 3,525
You have explanation but no question. What are you asking?

Also please edit your post to put in code tags as mentioned when you created the post.
May 13 '10 #2

P: 2
@tlhintoq
Ok, let me restate my question:

1. How do I write the results of a Binary Tree in C# to a tetx file. I found that part but I have not found how to link the C# code to my main code to get output to a text file.

When you say put tags in my code can you give an example of what you mean, thanks.

2. Here is my output code to a text file:

By The Way, I am using Visual Studio 2008:

//Write a text file
using System;
using System.IO;
using System.Text;

namespace readwriteapp
{
class Class1
{
[STAThread]
static void Main(string[] args)
{

Int64 x;

try
{
//Open the File
StreamWriter sw = new StreamWriter(@"J:\VS_Consol_Project\Test1.txt", true, Encoding.ASCII);

//Writeout the numbers 1 to 10 on the same line.
for (x = 0; x < 10; x++)
{
sw.WriteLine(x);
}

//close the file
sw.Close();
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.Message);
}
finally
{
Console.WriteLine("Executing finally block.");
}
}
}
}

How would you like to see some tags?

3. This is my Binary Tree generating Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// Binary tree
/// </summary>
/// <typeparam name="T">The type held by the nodes</typeparam>
public class BinaryTree<T> : ICollection<T>, IEnumerable<T> where T : IComparable<T>
{
#region Constructor

/// <summary>
/// Constructor
/// </summary>
public BinaryTree()
{
NumberOfNodes = 0;
}

public BinaryTree(TreeNode<T> Root)
{
this.Root = Root;
NumberOfNodes = 0;
}

#endregion

#region Properties
/// <summary>
/// The root value
/// </summary>
public TreeNode<T> Root { get; set; }

/// <summary>
/// The number of nodes in the tree
/// </summary>
protected int NumberOfNodes { get; set; }

/// <summary>
/// Is the tree empty
/// </summary>
public bool IsEmpty { get { return Root == null; } }

/// <summary>
/// Gets the minimum value of the tree
/// </summary>
public T MinValue
{
get
{
if (IsEmpty)
throw new Exception("The tree is empty");
TreeNode<T> TempNode = Root;
while (TempNode.Left != null)
TempNode = TempNode.Left;
return TempNode.Value;
}
}

/// <summary>
/// Gets the maximum value of the tree
/// </summary>
public T MaxValue
{
get
{
if (IsEmpty)
throw new Exception("The tree is empty");
TreeNode<T> TempNode = Root;
while (TempNode.Right != null)
TempNode = TempNode.Right;
return TempNode.Value;
}
}

#endregion

#region IEnumerable<T> Members

public IEnumerator<T> GetEnumerator()
{
foreach (TreeNode<T> TempNode in Traversal(Root))
{
yield return TempNode.Value;
}
}

#endregion

#region IEnumerable Members

System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
foreach (TreeNode<T> TempNode in Traversal(Root))
{
yield return TempNode.Value;
}
}

#endregion

#region ICollection<T> Members

public void Add(T item)
{
if (Root == null)
{
Root = new TreeNode<T>(item);
++NumberOfNodes;
}
else
{
Insert(item);
}
}

public void Clear()
{
Root = null;
}

public bool Contains(T item)
{
if (IsEmpty)
return false;

TreeNode<T> TempNode = Root;
while (TempNode != null)
{
int ComparedValue = TempNode.Value.CompareTo(item);
if (ComparedValue == 0)
return true;
else if (ComparedValue < 0)
TempNode = TempNode.Left;
else
TempNode = TempNode.Right;
}
return false;
}

public void CopyTo(T[] array, int arrayIndex)
{
T[] TempArray = new T[NumberOfNodes];
int Counter = 0;
foreach (T Value in this)
{
TempArray[Counter] = Value;
++Counter;
}
Array.Copy(TempArray, 0, array, arrayIndex, this.NumberOfNodes);
}

public int Count
{
get { return NumberOfNodes; }
}

public bool IsReadOnly
{
get { return false; }
}

public bool Remove(T item)
{
TreeNode<T> Item = Find(item);
if (Item == null)
return false;
List<T> Values = new List<T>();
foreach (TreeNode<T> TempNode in Traversal(Item.Left))
{
Values.Add(TempNode.Value);
}
foreach (TreeNode<T> TempNode in Traversal(Item.Right))
{
Values.Add(TempNode.Value);
}
if (Item.Parent.Left == Item)
{
Item.Parent.Left = null;
}
else
{
Item.Parent.Right = null;
}
Item.Parent = null;
foreach (T Value in Values)
{
this.Add(Value);
}
return true;
}

#endregion

#region Private Functions

/// <summary>
/// Finds a specific object
/// </summary>
/// <param name="item">The item to find</param>
/// <returns>The node if it is found</returns>
protected TreeNode<T> Find(T item)
{
foreach (TreeNode<T> Item in Traversal(Root))
if (Item.Value.Equals(item))
return Item;
return null;
}

/// <summary>
/// Traverses the list
/// </summary>
/// <param name="Node">The node to start the search from</param>
/// <returns>The individual items from the tree</returns>
protected IEnumerable<TreeNode<T>> Traversal(TreeNode<T> Node)
{
if (Node.Left != null)
{
foreach (TreeNode<T> LeftNode in Traversal(Node.Left))
yield return LeftNode;
}
yield return Node;
if (Node.Right != null)
{
foreach (TreeNode<T> RightNode in Traversal(Node.Right))
yield return RightNode;
}
}

/// <summary>
/// Inserts a value
/// </summary>
/// <param name="item">item to insert</param>
protected void Insert(T item)
{
TreeNode<T> TempNode = Root;
bool Found = false;
while (!Found)
{
int ComparedValue = TempNode.Value.CompareTo(item);
if (ComparedValue < 0)
{
if (TempNode.Left == null)
{
TempNode.Left = new TreeNode<T>(item, TempNode);
++NumberOfNodes;
return;
}
else
{
TempNode = TempNode.Left;
}
}
else if (ComparedValue > 0)
{
if (TempNode.Right == null)
{
TempNode.Right = new TreeNode<T>(item, TempNode);
++NumberOfNodes;
return;
}
else
{
TempNode = TempNode.Right;
}
}
else
{
TempNode = TempNode.Right;
}
}
}

#endregion
}

/// <summary>
/// Node class for the Binary tree
/// </summary>
/// <typeparam name="T">The value type</typeparam>
public class TreeNode<T>
{
#region Constructors

/// <summary>
/// Constructor
/// </summary>
public TreeNode()
{
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="Value">Value of the node</param>
public TreeNode(T Value)
{
this.Value = Value;
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="Value">Value of the node</param>
/// <param name="Parent">Parent node</param>
public TreeNode(T Value, TreeNode<T> Parent)
{
this.Value = Value;
this.Parent = Parent;
}

/// <summary>
/// Constructor
/// </summary>
/// <param name="Value">Value of the node</param>
/// <param name="Parent">Parent node</param>
/// <param name="Left">Left node</param>
/// <param name="Right">Right node</param>
public TreeNode(T Value, TreeNode<T> Parent, TreeNode<T> Left, TreeNode<T> Right)
{
this.Value = Value;
this.Right = Right;
this.Left = Left;
this.Parent = Parent;
}

#endregion

#region Properties

/// <summary>
/// Value of the node
/// </summary>
public T Value { get; set; }

/// <summary>
/// Parent node
/// </summary>
public TreeNode<T> Parent { get; set; }

/// <summary>
/// Left node
/// </summary>
public TreeNode<T> Left { get; set; }

/// <summary>
/// Right node
/// </summary>
public TreeNode<T> Right { get; set; }

/// <summary>
/// Is this the root
/// </summary>
public bool IsRoot { get { return Parent == null; } }

/// <summary>
/// Is this a leaf
/// </summary>
public bool IsLeaf { get { return Left == null && Right == null; } }

internal bool Visited { get; set; }

#endregion

#region Public Overridden Functions

public override string ToString()
{
return Value.ToString();
}

#endregion
}

Again, how should I enter tags?

Thanks for your help, this is my first time asking for help.
May 13 '10 #3

P: 1
Did anyone got an answe to the original question.. Question is:
HOW to implement the following C# method to write to a file all the values of the binary tree nodes at the specified depth from the root. Root has depth 0. Start from the leftmost node. An iterative solution is preferred over a recursive one.

class TreeNode
{
public int Value { get; set; }
public TreeNode LeftChild { get; set; }
public TreeNode RightChild { get; set; }
public TreeNode Parent { get; set; }
};

static void WriteTreeLevelToFile(
string filename,
TreeNode root,
int depth);
Jun 6 '10 #4

Expert 100+
P: 218
You could use your Traversal(Root)-method. This returns all nodes in a flattened way, beginning with the leftest node as far as I've understood it.

Out of this (all) nodes, you take the nodes with a depth equal to your wanted specific depth.

You can calculate the depth of each node by determining the count of parents e.g.
Expand|Select|Wrap|Line Numbers
  1. int myDepth = 0;
  2. while(parent.Parent != null) {
  3.   myDepth ++;
  4.   parent = parent.Parent;
  5. }
  6.  
I think, this would be the easiest way to achieve your requirements. But it can be very slow if there are many nodes in your tree and there re sure a lot of faster way to get the nodes you want.
Jun 7 '10 #5

Post your reply

Sign in to post your reply or Sign up for a free account.