Click here to Skip to main content
15,887,477 members
Home / Discussions / C#
   

C#

 
AnswerRe: WANT TO CREATE WIND OR RAIN EFFECT Pin
Pete O'Hanlon30-May-12 11:38
mvePete O'Hanlon30-May-12 11:38 
GeneralRe: WANT TO CREATE WIND OR RAIN EFFECT Pin
esaaco30-May-12 11:53
esaaco30-May-12 11:53 
JokeRe: WANT TO CREATE WIND OR RAIN EFFECT PinPopular
Peter_in_278030-May-12 11:51
professionalPeter_in_278030-May-12 11:51 
GeneralRe: WANT TO CREATE WIND OR RAIN EFFECT Pin
Richard Andrew x6430-May-12 12:37
professionalRichard Andrew x6430-May-12 12:37 
AnswerRe: WANT TO CREATE WIND OR RAIN EFFECT Pin
Roger Wright30-May-12 19:56
professionalRoger Wright30-May-12 19:56 
GeneralRe: WANT TO CREATE WIND OR RAIN EFFECT Pin
esaaco30-May-12 22:02
esaaco30-May-12 22:02 
AnswerRe: WANT TO CREATE WIND OR RAIN EFFECT Pin
Keith Barrow30-May-12 22:49
professionalKeith Barrow30-May-12 22:49 
QuestionNode In C# And The Halff Man Node Method Pin
Hussein39930-May-12 10:02
Hussein39930-May-12 10:02 
Hi there i want to create node to make the Half man Tree The code that i found to make node is here
<pre lang="c#">
public class Node
{
public char Symbol { get; set; }
public int Frequency { get; set; }
public Node Right { get; set; }
public Node Left { get; set; }
public List<bool> Traverse(char symbol, List<bool> data)
{
// Leaf
if (Right == null && Left == null)
{
if (symbol.Equals(this.Symbol))
{
return data;
}
else
{
return null;
}
}
else
{
List<bool> left = null;
List<bool> right = null;
if (Left != null)
{
List<bool> leftPath = new List<bool>();
leftPath.AddRange(data);
leftPath.Add(false);
left = Left.Traverse(symbol, leftPath);
}
if (Right != null)
{
List<bool> rightPath = new List<bool>();
rightPath.AddRange(data);
rightPath.Add(true);
right = Right.Traverse(symbol, rightPath);
}
if (left != null)
{
return left;
}
else
{
return right;
}
}
}
}
</pre>


I know that the Symbol and Frequency Are the data and the Left and Right is to store the position of the another Node. but i dont understand the Traverse Method. what is for!!!
The Rest of the halff man code is here:
<pre lang="c#">
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace HuffmanTest
{
public class HuffmanTree
{
private List<Node> nodes = new List<Node>();
public Node Root { get; set; }
public Dictionary<char, int> Frequencies = new Dictionary<char, int>();
public void Build(string source)
{
for (int i = 0; i < source.Length; i++)
{
if (!Frequencies.ContainsKey(source[i]))
{
Frequencies.Add(source[i], 0);
}
Frequencies[source[i]]++;
}
foreach (KeyValuePair<char, int> symbol in Frequencies)
{
nodes.Add(new Node() { Symbol = symbol.Key, Frequency = symbol.Value });
}
while (nodes.Count > 1)
{
List<Node> orderedNodes = nodes.OrderBy(node => node.Frequency).ToList<Node>();
if (orderedNodes.Count >= 2)
{
// Take first two items
List<Node> taken = orderedNodes.Take(2).ToList<Node>();
// Create a parent node by combining the frequencies
Node parent = new Node()
{
Symbol = '*',
Frequency = taken[0].Frequency + taken[1].Frequency,
Left = taken[0],
Right = taken[1]
};
nodes.Remove(taken[0]);
nodes.Remove(taken[1]);
nodes.Add(parent);
}
this.Root = nodes.FirstOrDefault();
}
}
public BitArray Encode(string source)
{
List<bool> encodedSource = new List<bool>();
for (int i = 0; i < source.Length; i++)
{
List<bool> encodedSymbol = this.Root.Traverse(source[i], new List<bool>());
encodedSource.AddRange(encodedSymbol);
}
BitArray bits = new BitArray(encodedSource.ToArray());
return bits;
}
public string Decode(BitArray bits)
{
Node current = this.Root;
string decoded = "";
foreach (bool bit in bits)
{
if (bit)
{
if (current.Right != null)
{
current = current.Right;
}
}
else
{
if (current.Left != null)
{
current = current.Left;
}
}
if (IsLeaf(current))
{
decoded += current.Symbol;
current = this.Root;
}
}
return decoded;
}
public bool IsLeaf(Node node)
{
return (node.Left == null && node.Right == null);
}
}
}
</pre>
AnswerRe: Node In C# And The Halff Man Node Method Pin
Paul Conrad2-Jun-12 7:11
professionalPaul Conrad2-Jun-12 7:11 
QuestionC# linq to sql Pin
sc steinhayse30-May-12 9:11
sc steinhayse30-May-12 9:11 
AnswerRe: C# linq to sql Pin
Pete O'Hanlon30-May-12 10:45
mvePete O'Hanlon30-May-12 10:45 
AnswerRe: C# linq to sql Pin
Keith Barrow30-May-12 22:58
professionalKeith Barrow30-May-12 22:58 
AnswerRe: C# linq to sql Pin
AmitGajjar31-May-12 0:06
professionalAmitGajjar31-May-12 0:06 
GeneralRe: C# linq to sql Pin
Pete O'Hanlon31-May-12 0:11
mvePete O'Hanlon31-May-12 0:11 
GeneralRe: C# linq to sql Pin
AmitGajjar31-May-12 0:18
professionalAmitGajjar31-May-12 0:18 
GeneralRe: C# linq to sql Pin
Pete O'Hanlon31-May-12 0:43
mvePete O'Hanlon31-May-12 0:43 
GeneralRe: C# linq to sql Pin
AmitGajjar31-May-12 0:55
professionalAmitGajjar31-May-12 0:55 
GeneralRe: C# linq to sql Pin
Pete O'Hanlon31-May-12 1:02
mvePete O'Hanlon31-May-12 1:02 
Questionwcf service Pin
heba abu ghaleih22 30-May-12 7:58
heba abu ghaleih22 30-May-12 7:58 
AnswerRe: wcf service Pin
Dave Kreskowiak30-May-12 15:21
mveDave Kreskowiak30-May-12 15:21 
Questionhow to call the values from the databases Pin
S. Karthik - Hosur30-May-12 4:21
S. Karthik - Hosur30-May-12 4:21 
AnswerRe: how to call the values from the databases PinPopular
Dave Kreskowiak30-May-12 4:24
mveDave Kreskowiak30-May-12 4:24 
GeneralRe: how to call the values from the databases Pin
Paul Conrad1-Jun-12 14:48
professionalPaul Conrad1-Jun-12 14:48 
AnswerCross posted from QA PinPopular
Pete O'Hanlon30-May-12 4:40
mvePete O'Hanlon30-May-12 4:40 
QuestionMessage Removed Pin
30-May-12 4:05
S. Karthik - Hosur30-May-12 4:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.