节点:
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->classNode<T>
{
publicTValue{get;set;}
publicNode<T>Left{get;set;}
publicNode<T>Right{get;set;}
publicNode(Tvalue,Node<T>left,Node<T>right)
{
Value=value;
Left=left;
Right=right;
}
publicNode(Tvalue):this(value,null,null){}
}
{
publicTValue{get;set;}
publicNode<T>Left{get;set;}
publicNode<T>Right{get;set;}
publicNode(Tvalue,Node<T>left,Node<T>right)
{
Value=value;
Left=left;
Right=right;
}
publicNode(Tvalue):this(value,null,null){}
}
二叉树:
前中后深度优先遍历非递归,上到下(下到上)宽度优先遍历非递归,输出某层,获取深度
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->classBinaryTree<T>
{
protectedIComparer<T>comparer=Comparer<T>.Default;
publicNode<T>Root{get;set;}
publicvoidClear()
{
Root=null;
}
publicvoidPreOrder()
{
if(Root==null)
return;
Stack<Node<T>>stack=newStack<Node<T>>();
stack.Push(Root);
while(stack.Count>0)
{
Node<T>node=stack.Pop();
Console.Write(node.Value+"");
if(node.Right!=null)
stack.Push(node.Right);
if(node.Left!=null)
stack.Push(node.Left);
}
}
publicvoidMidOrder()
{
if(Root==null)
return;
Stack<Node<T>>stack=newStack<Node<T>>();
for(Node<T>current=Root;current!=null||stack.Count>0;current=current.Right)
{
while(current!=null)
{
stack.Push(current);
current=current.Left;
}
current=stack.Pop();
Console.Write(current.Value+"");
}
}
publicvoidAfterOrder()
{
if(Root==null)
return;
Stack<Node<T>>toVisit=newStack<Node<T>>();
Stack<bool>hasBeenProcessed=newStack<bool>();
Node<T>current=Root;
if(current!=null)
{
toVisit.Push(current);
hasBeenProcessed.Push(false);
current=current.Left;
}
while(toVisit.Count!=0)
{
if(current!=null)
{
toVisit.Push(current);
hasBeenProcessed.Push(false);
current=current.Left;
}
else
{
boolprocessed=hasBeenProcessed.Pop();
Node<T>node=toVisit.Pop();
if(!processed)
{
toVisit.Push(node);
hasBeenProcessed.Push(true);
current=node.Right;
}
else
Console.WriteLine(node.Value+"");
}
}
}
publicvoidUpDownOrder()
{
if(Root==null)
return;
Queue<Node<T>>queue=newQueue<Node<T>>();
queue.Enqueue(Root);
while(queue.Count>0)
{
Node<T>node=queue.Dequeue();
Console.Write(node.Value+"");
if(node.Left!=null)
queue.Enqueue(node.Left);
if(node.Right!=null)
queue.Enqueue(node.Right);
}
}
publicvoidDownUpOrder()
{
if(Root==null)
return;
Queue<Node<T>>queue=newQueue<Node<T>>();
queue.Enqueue(Root);
Stack<Node<T>>stack=newStack<Node<T>>();
while(queue.Count>0)
{
Node<T>node=queue.Dequeue();
stack.Push(node);
if(node.Left!=null)
queue.Enqueue(node.Left);
if(node.Right!=null)
queue.Enqueue(node.Right);
}
intc=stack.Count;
for(inti=0;i<c;i++)
{
Console.Write(stack.Pop().Value+"");
}
}
publicvoidPrintNodeAtLevel(intlevel)
{
if(level==0)Console.Write(Root.Value+"");
PrintNodeAtLevel(Root,level);
}
privatevoidPrintNodeAtLevel(Node<T>node,intlevel)
{
if(node==null||level<0)
return;
if(level==0)
{
Console.Write(node.Value+"");
return;
}
PrintNodeAtLevel(node.Left,level-1);
PrintNodeAtLevel(node.Right,level-1);
}
publicintGetDepth()
{
if(Root==null)
return0;
returnGetDepth(Root);
}
privateintGetDepth(Node<T>node)
{
if(node==null)return0;
intl=GetDepth(node.Left);
intr=GetDepth(node.Right);
returnMath.Max(l,r)+1;
}
}
{
protectedIComparer<T>comparer=Comparer<T>.Default;
publicNode<T>Root{get;set;}
publicvoidClear()
{
Root=null;
}
publicvoidPreOrder()
{
if(Root==null)
return;
Stack<Node<T>>stack=newStack<Node<T>>();
stack.Push(Root);
while(stack.Count>0)
{
Node<T>node=stack.Pop();
Console.Write(node.Value+"");
if(node.Right!=null)
stack.Push(node.Right);
if(node.Left!=null)
stack.Push(node.Left);
}
}
publicvoidMidOrder()
{
if(Root==null)
return;
Stack<Node<T>>stack=newStack<Node<T>>();
for(Node<T>current=Root;current!=null||stack.Count>0;current=current.Right)
{
while(current!=null)
{
stack.Push(current);
current=current.Left;
}
current=stack.Pop();
Console.Write(current.Value+"");
}
}
publicvoidAfterOrder()
{
if(Root==null)
return;
Stack<Node<T>>toVisit=newStack<Node<T>>();
Stack<bool>hasBeenProcessed=newStack<bool>();
Node<T>current=Root;
if(current!=null)
{
toVisit.Push(current);
hasBeenProcessed.Push(false);
current=current.Left;
}
while(toVisit.Count!=0)
{
if(current!=null)
{
toVisit.Push(current);
hasBeenProcessed.Push(false);
current=current.Left;
}
else
{
boolprocessed=hasBeenProcessed.Pop();
Node<T>node=toVisit.Pop();
if(!processed)
{
toVisit.Push(node);
hasBeenProcessed.Push(true);
current=node.Right;
}
else
Console.WriteLine(node.Value+"");
}
}
}
publicvoidUpDownOrder()
{
if(Root==null)
return;
Queue<Node<T>>queue=newQueue<Node<T>>();
queue.Enqueue(Root);
while(queue.Count>0)
{
Node<T>node=queue.Dequeue();
Console.Write(node.Value+"");
if(node.Left!=null)
queue.Enqueue(node.Left);
if(node.Right!=null)
queue.Enqueue(node.Right);
}
}
publicvoidDownUpOrder()
{
if(Root==null)
return;
Queue<Node<T>>queue=newQueue<Node<T>>();
queue.Enqueue(Root);
Stack<Node<T>>stack=newStack<Node<T>>();
while(queue.Count>0)
{
Node<T>node=queue.Dequeue();
stack.Push(node);
if(node.Left!=null)
queue.Enqueue(node.Left);
if(node.Right!=null)
queue.Enqueue(node.Right);
}
intc=stack.Count;
for(inti=0;i<c;i++)
{
Console.Write(stack.Pop().Value+"");
}
}
publicvoidPrintNodeAtLevel(intlevel)
{
if(level==0)Console.Write(Root.Value+"");
PrintNodeAtLevel(Root,level);
}
privatevoidPrintNodeAtLevel(Node<T>node,intlevel)
{
if(node==null||level<0)
return;
if(level==0)
{
Console.Write(node.Value+"");
return;
}
PrintNodeAtLevel(node.Left,level-1);
PrintNodeAtLevel(node.Right,level-1);
}
publicintGetDepth()
{
if(Root==null)
return0;
returnGetDepth(Root);
}
privateintGetDepth(Node<T>node)
{
if(node==null)return0;
intl=GetDepth(node.Left);
intr=GetDepth(node.Right);
returnMath.Max(l,r)+1;
}
}
搜索二叉树:
搜索、包含、增加删除、获取父节点
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->classBinarySearchTree<T>:BinaryTree<T>
{
publicNode<T>Search(Tdata)
{
Node<T>current=Root;
while(current!=null)
{
intresult=comparer.Compare(current.Value,data);
if(result==0)
returncurrent;
elseif(result>0)
current=current.Left;
elseif(result<0)
current=current.Right;
}
returncurrent;
}
publicboolContains(Tdata)
{
Node<T>current=Root;
intresult;
while(current!=null)
{
result=comparer.Compare(current.Value,data);
if(result==0)
returntrue;
elseif(result>0)
current=current.Left;
elseif(result<0)
current=current.Right;
}
returnfalse;
}
publicNode<T>GetParent(Node<T>node)
{
Node<T>current=base.Root,parent=null;
while(current!=null)
{
intresult=comparer.Compare(current.Value,node.Value);
if(result==0)
returnparent;
elseif(result>0)
{
parent=current;
current=current.Left;
}
elseif(result<0)
{
parent=current;
current=current.Right;
}
}
returnparent;
}
publicvoidAdd(Tdata)
{
Node<T>n=newNode<T>(data);
intresult;
Node<T>current=base.Root,parent=null;
while(current!=null)
{
result=comparer.Compare(current.Value,data);
if(result==0)
return;
elseif(result>0)
{
parent=current;
current=current.Left;
}
elseif(result<0)
{
parent=current;
current=current.Right;
}
}
if(parent==null)
base.Root=n;
else
{
result=comparer.Compare(parent.Value,data);
if(result>0)
parent.Left=n;
else
parent.Right=n;
}
}
publicboolRemove(Tdata)
{
if(base.Root==null)
returnfalse;
Node<T>current=base.Root,parent=null;
intresult=comparer.Compare(current.Value,data);
while(result!=0)
{
if(result>0)
{
parent=current;
current=current.Left;
}
elseif(result<0)
{
parent=current;
current=current.Right;
}
if(current==null)
returnfalse;
else
result=comparer.Compare(current.Value,data);
}
if(current.Right==null)
{
if(parent==null)
base.Root=current.Left;
else
{
result=comparer.Compare(parent.Value,current.Value);
if(result>0)
parent.Left=current.Left;
elseif(result<0)
parent.Right=current.Left;
}
}
elseif(current.Right.Left==null)
{
current.Right.Left=current.Left;
if(parent==null)
base.Root=current.Right;
else
{
result=comparer.Compare(parent.Value,current.Value);
if(result>0)
parent.Left=current.Right;
elseif(result<0)
parent.Right=current.Right;
}
}
else
{
Node<T>leftmost=current.Right.Left,lmParent=current.Right;
while(leftmost.Left!=null)
{
lmParent=leftmost;
leftmost=leftmost.Left;
}
lmParent.Left=leftmost.Right;
leftmost.Left=current.Left;
leftmost.Right=current.Right;
if(parent==null)
base.Root=leftmost;
else
{
result=comparer.Compare(parent.Value,current.Value);
if(result>0)
parent.Left=leftmost;
elseif(result<0)
parent.Right=leftmost;
}
}
current.Left=current.Right=null;
current=null;
returntrue;
}
}
{
publicNode<T>Search(Tdata)
{
Node<T>current=Root;
while(current!=null)
{
intresult=comparer.Compare(current.Value,data);
if(result==0)
returncurrent;
elseif(result>0)
current=current.Left;
elseif(result<0)
current=current.Right;
}
returncurrent;
}
publicboolContains(Tdata)
{
Node<T>current=Root;
intresult;
while(current!=null)
{
result=comparer.Compare(current.Value,data);
if(result==0)
returntrue;
elseif(result>0)
current=current.Left;
elseif(result<0)
current=current.Right;
}
returnfalse;
}
publicNode<T>GetParent(Node<T>node)
{
Node<T>current=base.Root,parent=null;
while(current!=null)
{
intresult=comparer.Compare(current.Value,node.Value);
if(result==0)
returnparent;
elseif(result>0)
{
parent=current;
current=current.Left;
}
elseif(result<0)
{
parent=current;
current=current.Right;
}
}
returnparent;
}
publicvoidAdd(Tdata)
{
Node<T>n=newNode<T>(data);
intresult;
Node<T>current=base.Root,parent=null;
while(current!=null)
{
result=comparer.Compare(current.Value,data);
if(result==0)
return;
elseif(result>0)
{
parent=current;
current=current.Left;
}
elseif(result<0)
{
parent=current;
current=current.Right;
}
}
if(parent==null)
base.Root=n;
else
{
result=comparer.Compare(parent.Value,data);
if(result>0)
parent.Left=n;
else
parent.Right=n;
}
}
publicboolRemove(Tdata)
{
if(base.Root==null)
returnfalse;
Node<T>current=base.Root,parent=null;
intresult=comparer.Compare(current.Value,data);
while(result!=0)
{
if(result>0)
{
parent=current;
current=current.Left;
}
elseif(result<0)
{
parent=current;
current=current.Right;
}
if(current==null)
returnfalse;
else
result=comparer.Compare(current.Value,data);
}
if(current.Right==null)
{
if(parent==null)
base.Root=current.Left;
else
{
result=comparer.Compare(parent.Value,current.Value);
if(result>0)
parent.Left=current.Left;
elseif(result<0)
parent.Right=current.Left;
}
}
elseif(current.Right.Left==null)
{
current.Right.Left=current.Left;
if(parent==null)
base.Root=current.Right;
else
{
result=comparer.Compare(parent.Value,current.Value);
if(result>0)
parent.Left=current.Right;
elseif(result<0)
parent.Right=current.Right;
}
}
else
{
Node<T>leftmost=current.Right.Left,lmParent=current.Right;
while(leftmost.Left!=null)
{
lmParent=leftmost;
leftmost=leftmost.Left;
}
lmParent.Left=leftmost.Right;
leftmost.Left=current.Left;
leftmost.Right=current.Right;
if(parent==null)
base.Root=leftmost;
else
{
result=comparer.Compare(parent.Value,current.Value);
if(result>0)
parent.Left=leftmost;
elseif(result<0)
parent.Right=leftmost;
}
}
current.Left=current.Right=null;
current=null;
returntrue;
}
}
1435

被折叠的 条评论
为什么被折叠?



