本文整理汇总了C#中Tree.Node类的典型用法代码示例。如果您正苦于以下问题:C# Node类的具体用法?C# Node怎么用?C# Node使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Node类属于Tree命名空间,在下文中一共展示了Node类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: print
public override void print(Node t, int n, bool p)
{
if (!p)
{
var spaces = "";
if (n >= 0) spaces = new string(' ', n);
Console.Write(spaces + "(if ");
if (t.getCdr().getCar().isPair())
{
if((t.getCdr().getCar() as Cons).form is If ||
(t.getCdr().getCar() as Cons).form is Lambda ||
(t.getCdr().getCar() as Cons).form is Define)
t.getCdr().getCar().print(n+4, true);
else
{
t.getCdr().getCar().print(0, false);
}
}
else
t.getCdr().getCar().print(0, false);
t = t.getCdr().getCdr();
do
{
Console.WriteLine("");
t.getCar().print(n + 4, false);
t = t.getCdr();
} while (t.getCdr() != null);
Console.WriteLine("");
t.print(n, true);
}
else
{
var spaces = "";
if (n >= 0) spaces = new string(' ', n);
Console.Write("(if ");
if (t.getCdr().getCar().isPair())
{
if ((t.getCdr().getCar() as Cons).form is If ||
(t.getCdr().getCar() as Cons).form is Lambda ||
(t.getCdr().getCar() as Cons).form is Define)
t.getCdr().getCar().print(n+4, true);
else
{
t.getCdr().getCar().print(0, false);
}
}
t.getCdr().getCar().print(0, false);
t = t.getCdr().getCdr();
do
{
Console.WriteLine("");
t.getCar().print(n + 4, false);
t = t.getCdr();
} while (t.getCdr() != null);
Console.WriteLine("");
t.print(n, true);
}
}
示例2: eval
// REMEMBER: If not explictly false, then true.
public Node eval(Node t, Environment e)
{
Node cond = t.getCdr().getCar(); // condition -> cadr(exp)
if (cond == null)
{
Console.Error.WriteLine("Error: Null in Condition");
}
Node ifTrue = t.getCdr().getCdr().getCar(); // true -> caddr(exp)
if (ifTrue == null)
{
Console.Error.WriteLine("Error: Null in True/If");
}
Node ifFalse = t.getCdr().getCdr().getCdr().getCar(); // false -> cadddr(exp)
if (ifFalse == null)
{
Console.Error.WriteLine("Error: Null in False/Else");
}
bool checkTrueResult = checkTrue(t, e);
if (checkTrueResult == true)
{
return ifTrue.eval(ifTrue, e);
}
else
{
return ifFalse.eval(ifFalse, e);
}
}
示例3: print
public override void print(Node t, int n, bool p)
{
// print leading spaces and '
if (n < 0)
{
n = -(n + 1);
}
Console.Write(new string(' ', n) + "'");
// if quote a list
if (t.getCdr().getCar().isPair())
{
// this while is to print following quote as ' to get the sample output
while (t.getCdr().getCar().getCar().isSymbol()
&& ((Ident) t.getCdr().getCar().getCar()).getName().Equals("quote"))
{
Console.Write("'");
t = t.getCdr().getCar();
}
// using the Regular form to print quoted list
Special v = new Regular();
v.print(t.getCdr().getCar(), 0, false);
// if the caller asks for change line
if (n > 0)
{
Console.WriteLine();
}
}
// if quote a single element
else
{
t.getCdr().getCar().print(0);
}
}
示例4: eval
public override Node eval(Node exp, Environment env)
{
int length = Util.expLength(exp);
if (length < 3)
{
Console.Error.WriteLine("Error: invalid expression");
return Nil.getInstance();
}
Node second = exp.getCdr().getCar();
Node name, value;
if (length == 3 && second.isSymbol())
{
name = second;
value = exp.getCdr().getCdr().getCar().eval(env);
env.define(name, value);
return null;
}
if (!second.isPair())
{
Console.Error.WriteLine("Error: invalid expression");
return Nil.getInstance();
}
name = second.getCar();
Node lambda = exp.getCdr();
lambda.setCar(second.getCdr());
lambda = new Cons(new Ident("lambda"), lambda);
value = lambda.eval(env);
env.define(name, value);
return null;
}
示例5: print
public override void print(Node t, int n, bool p)
{
Console.Write("".PadLeft(n));
if ( p != true)
{
Console.Write("(");
}
if(t.getCar().isPair())
{
t.getCar().print(0);
}
else
{
t.getCar().print(0, true);
}
Console.Write(" ");
if(t.getCdr() is Nil)
{
Console.WriteLine(")");
}
else
{
t.getCdr().print(0, true);
}
// Console.WriteLine();
}
示例6: eval
public override Node eval(Node exp, Environment env)
{
int length = Util.expLength(exp);
if (length < 3)
{
Console.Error.WriteLine("Error: invalid expression");
return Nil.getInstance();
}
Node second = exp.getCdr().getCar();
while (second.isPair())
{
if (Util.expLength(second.getCar()) != 2)
{
Console.Error.WriteLine("Error: invalid expression");
return Nil.getInstance();
}
second = second.getCdr();
}
second = exp.getCdr().getCar();
Environment env1 = new Environment(env);
while (second.isPair())
{
Node id = second.getCar().getCar();
Node val = second.getCar().getCdr().getCar();
env1.define(id, val);
second = second.getCdr();
}
Node exp1 = exp.getCdr().getCdr();
return Begin.evalBody(exp1, env1);
}
示例7: ConstuctTreeFromPreOrder
/* A O(n) iterative program for construction of BST from preorder traversal */
public Node ConstuctTreeFromPreOrder(int[] preOrder)
{
Node root = new Node();
root.data = preOrder[0];
Stack<Node> _stack = new Stack<Node>();
Node temp;
for (int i = 1; i < preOrder.Length; i++)
{
temp = null;
while (_stack.Count > 0 && preOrder[i] > _stack.Peek().data)
{
temp = _stack.Pop();
}
if (temp != null)
{
temp.right.data = preOrder[i];
_stack.Push(temp.right);
}
else
{
temp.left.data = preOrder[i];
_stack.Push(temp.left);
}
}
return root;
}
示例8: ParseTree
static Tree<int> ParseTree()
{
var nodes = new Dictionary<int, Node<int>>();
Console.Write("Number of nodes:");
int n = int.Parse(Console.ReadLine());
Console.WriteLine("Start typing the node pairs separated by a single space");
for (int i = 0; i < n-1; i++)
{
var pair = Console.ReadLine().Split();
int first = int.Parse(pair[0]);
int second = int.Parse(pair[1]);
if (!nodes.ContainsKey(first))
{
Node<int> parent = new Node<int>(first);
nodes.Add(first, parent);
}
if (!nodes.ContainsKey(second))
{
Node<int> newNode = new Node<int>(second);
newNode.HasParent = true;
nodes.Add(second,newNode);
nodes[first].Children.Add(newNode);
}
nodes[first].Children.Add(nodes[second]);
nodes[second].HasParent=true;
}
var root = nodes.First(x => x.Value.HasParent == false);
var tree = new Tree<int>(root.Value);
return tree;
}
示例9: apply
// TODO: The method apply() should be defined in class Node
// to report an error. It should be overridden only in classes
// BuiltIn and Closure.
public override Node apply (Node args)
{
Node param = fun.getCdr().getCar();
int numArgs = Util.expLength(args);
int numParam = Util.expLength(param);
if (numArgs != numParam)
{
Console.Error.WriteLine("Error: wrong number of arguments");
}
Environment env1 = new Environment(env);
while (param.isPair() && args.isPair())
{
Node id = param.getCar();
Node val = args.getCar();
env1.define(id, val);
param = param.getCdr();
args = args.getCdr();
}
while (param.isPair())
{
Node id = param.getCar();
Node val = Nil.getInstance();
env1.define(id, val);
param = param.getCdr();
}
Node exp1 = fun.getCdr().getCdr();
return Begin.evalBody(exp1, env1);
}
示例10: print
public override void print(Node t, int n, bool p)
{
// TODO: Implement this function.
// Console.Write("".PadLeft(n));
if (!p)
{
Console.Write("".PadLeft(n) + '(');
}
t.getCar().print(0, !p);
// if(!t.getCdr().isNull())
// {
// Console.Write(" ");
// }
if(t.getCdr().isNull())
{
t.getCdr().print(0, true);
// Console.WriteLine();
}
else {
Console.Write(" ");
t.getCdr().print(n, true);
}
// if(t.getCdr().isNull())
// {
// Console.WriteLine();
// }
}
示例11: eval
public override Node eval(Node t, Environment env)
{
if (t.getCdr() == Nil.getInstance())
{
Console.Error.WriteLine("Error: invalid argument");
throw new InvalidOperationException();
}
if (t.getCdr().getCar().eval(env) != BoolLit.getInstance(false))
{
if (t.getCdr().getCdr() == Nil.getInstance())
{
Console.Error.WriteLine("Error: invalid argument");
throw new InvalidOperationException();
}
return t.getCdr().getCdr().getCar().eval(env);
}
if (t.getCdr().getCdr() == Nil.getInstance())
{
Console.Error.WriteLine("Error: invalid argument");
throw new InvalidOperationException();
}
if (t.getCdr().getCdr().getCdr() == Nil.getInstance())
{
return Nil.getInstance();
}
return t.getCdr().getCdr().getCdr().getCar().eval(env);
}
示例12: eval
public override Node eval(Node t, Environment e)
{
if(t.getCdr() is Closure)
return e.lookup(t.getCar()).apply(t.getCdr());
else
{
Node args = t.getCdr();
Node param;
if (args.getCar().isSymbol())
param = new Cons(e.lookup((Node)args.GetCar()), Nil.getInstance());
else
param = new Cons((Node)args.GetCar(), Nil.getInstance());
Node currentNode = param;
args = args.getCdr();
while (args != Nil.getInstance())
{
if (args.getCar().isSymbol())
currentNode.setCdr(new Cons(e.lookup(args.getCar()), Nil.getInstance()));
else
currentNode.setCdr(new Cons(args.getCar(), Nil.getInstance()));
currentNode = currentNode.getCdr();
args = args.getCdr();
}
return e.lookup(t.getCar()).apply(param);
}
}
示例13: eval
public override Node eval(Node t, Environment e)
{
while (t.getCdr() != Nil.getInstance())
{
t = t.getCdr();
if (t.getCar().GetType() != typeof(Cons))
{
Console.Error.WriteLine("Error: invalid argument");
throw new InvalidOperationException();
}
if (t.getCar().getCar().eval(e) == BoolLit.getInstance(true))
{
t = t.getCar();
while(t.getCdr() != Nil.getInstance())
{
t = t.getCdr();
if (t.getCdr() == Nil.getInstance()) break;
t.getCar().eval(e);
}
return (Node)t.getCar().eval(e);
}
else if (t.getCar().getCar().eval(e) != BoolLit.getInstance(false))
{
Console.Error.WriteLine("Error: invalid argument");
throw new InvalidOperationException();
}
}
return Nil.getInstance();
}
示例14: print
public override void print(Node t, int n, bool p)
{
// TODO: Implement this function.
Console.Write("".PadLeft(n));
Console.Write("(define ");
Node name = t.getCdr().getCar();
name.print(n);
Console.Write(" ");
Node definition = t.getCdr().getCdr().getCar();
if (definition.isNull())
{
Console.Error.Write("Syntax error with DEFINE-definition");
}
else if (definition.isPair())
{
Console.WriteLine();
// Console.Write("".PadLeft(n+4));
definition.print(n+4);
}
else
{
definition.print(n);
}
// made n to 0
// Console.WriteLine();
Console.WriteLine("".PadLeft(n) + ")");
}
示例15: print
public override void print(Node t, int n, bool p)
{
for (int i = Console.CursorLeft; i < n; i++)
Console.Write(" ");
if (!p)
{
Console.Write("(");
}
//print "let"
t.getCar().print(n);
Console.WriteLine();
Node rest = t.getCdr();
if (rest.isPair())
{
rest.getCar().print(n + 4, false);
Console.WriteLine();
while ((rest = rest.getCdr()) != Nil.getNil())
{
rest.getCar().print(n + 4, false);
Console.WriteLine();
}
Console.Write(")");
}
else t.getCdr().print(n, true);
}