1
Answer

please help me with this code,explanations required

joby

joby

15y
2.7k
1

public
SortTreeNode Add(ref string str)
{
SortTreeNode node;
if(Root == null)
{
Root =
new SortTreeNode();
node = Root;
}
else
{
node = Root;
while(true)
{
if(node.Text == str)
{
node.Count++;
return node;
}
if(node.Text.CompareTo(str) > 0)
{
// add the node at the small branch
if(node.Small == null)
{
node.Small =
new SortTreeNode();
node.Small.Parent = node;
node = node.Small;
break;
}
node = node.Small;
}
else
{
// add the node at the great branch
if(node.Great == null)
{
node.Great =
new SortTreeNode();
node.Great.Parent = node;
node = node.Great;
break;
}
node = node.Great;
}
}
}
node.Text = str;
node.ID =
this.Count++;
node.Count++;
Modified =
true;

return node;
}

public void Save(string fileName, System.Text.Encoding code)
{
FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None);
StreamWriter writer = new StreamWriter(stream, code);

BitArray bitArray = new BitArray(this.Count, false);
SortTreeNode node = this.Root;
int nCount = this.Count;
while(nCount > 0)
{
if(node.Small != null && bitArray.Get(node.Small.ID) == false)
node = node.Small;
else if(bitArray.Get(node.ID) == false)
OutNode(node, writer, bitArray,
ref nCount);
else if(node.Great != null && bitArray.Get(node.Great.ID) == false)
node = node.Great;
else
{
if(bitArray.Get(node.ID) == false)
OutNode(node, writer, bitArray,
ref nCount);
node = node.Parent;
}
}
writer.Close();
stream.Close();

Modified =
false;
}
Answers (1)