<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>
节点:
class
Node
<
T
>
{
public T Value { get ; set ; }
public Node < T > Left { get ; set ; }
public Node < T > Right { get ; set ; }
public Node(T value, Node < T > left, Node < T > right)
{
Value = value;
Left = left;
Right = right;
}
public Node(T value) : this (value, null , null ) { }
}
{
public T Value { get ; set ; }
public Node < T > Left { get ; set ; }
public Node < T > Right { get ; set ; }
public Node(T value, Node < T > left, Node < T > right)
{
Value = value;
Left = left;
Right = right;
}
public Node(T value) : this (value, null , null ) { }
}
二叉树:
前中后深度优先遍历非递归,上到下(下到上)宽度优先遍历非递归,输出某层,获取深度
class
BinaryTree
<
T
>
{
protected IComparer < T > comparer = Comparer < T > .Default;
public Node < T > Root { get ; set ; }
public void Clear()
{
Root = null ;
}
public void PreOrder()
{
if (Root == null )
return ;
Stack < Node < T >> stack = new Stack < 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);
}
}
public void MidOrder()
{
if (Root == null )
return ;
Stack < Node < T >> stack = new Stack < 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 + " " );
}
}
public void AfterOrder()
{
if (Root == null )
return ;
Stack < Node < T >> toVisit = new Stack < Node < T >> ();
Stack < bool > hasBeenProcessed = new Stack < 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
{
bool processed = hasBeenProcessed.Pop();
Node < T > node = toVisit.Pop();
if ( ! processed)
{
toVisit.Push(node);
hasBeenProcessed.Push( true );
current = node.Right;
}
else
Console.WriteLine(node.Value + " " );
}
}
}
public void UpDownOrder()
{
if (Root == null )
return ;
Queue < Node < T >> queue = new Queue < 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);
}
}
public void DownUpOrder()
{
if (Root == null )
return ;
Queue < Node < T >> queue = new Queue < Node < T >> ();
queue.Enqueue(Root);
Stack < Node < T >> stack = new Stack < 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);
}
int c = stack.Count;
for ( int i = 0 ; i < c; i ++ )
{
Console.Write(stack.Pop().Value + " " );
}
}
public void PrintNodeAtLevel( int level)
{
if (level == 0 ) Console.Write(Root.Value + " " );
PrintNodeAtLevel(Root, level);
}
private void PrintNodeAtLevel(Node < T > node, int level)
{
if (node == null || level < 0 )
return ;
if (level == 0 )
{
Console.Write(node.Value + " " );
return ;
}
PrintNodeAtLevel(node.Left, level - 1 );
PrintNodeAtLevel(node.Right, level - 1 );
}
public int GetDepth()
{
if (Root == null )
return 0 ;
return GetDepth(Root);
}
private int GetDepth(Node < T > node)
{
if (node == null ) return 0 ;
int l = GetDepth(node.Left);
int r = GetDepth(node.Right);
return Math.Max(l,r) + 1 ;
}
}
{
protected IComparer < T > comparer = Comparer < T > .Default;
public Node < T > Root { get ; set ; }
public void Clear()
{
Root = null ;
}
public void PreOrder()
{
if (Root == null )
return ;
Stack < Node < T >> stack = new Stack < 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);
}
}
public void MidOrder()
{
if (Root == null )
return ;
Stack < Node < T >> stack = new Stack < 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 + " " );
}
}
public void AfterOrder()
{
if (Root == null )
return ;
Stack < Node < T >> toVisit = new Stack < Node < T >> ();
Stack < bool > hasBeenProcessed = new Stack < 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
{
bool processed = hasBeenProcessed.Pop();
Node < T > node = toVisit.Pop();
if ( ! processed)
{
toVisit.Push(node);
hasBeenProcessed.Push( true );
current = node.Right;
}
else
Console.WriteLine(node.Value + " " );
}
}
}
public void UpDownOrder()
{
if (Root == null )
return ;
Queue < Node < T >> queue = new Queue < 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);
}
}
public void DownUpOrder()
{
if (Root == null )
return ;
Queue < Node < T >> queue = new Queue < Node < T >> ();
queue.Enqueue(Root);
Stack < Node < T >> stack = new Stack < 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);
}
int c = stack.Count;
for ( int i = 0 ; i < c; i ++ )
{
Console.Write(stack.Pop().Value + " " );
}
}
public void PrintNodeAtLevel( int level)
{
if (level == 0 ) Console.Write(Root.Value + " " );
PrintNodeAtLevel(Root, level);
}
private void PrintNodeAtLevel(Node < T > node, int level)
{
if (node == null || level < 0 )
return ;
if (level == 0 )
{
Console.Write(node.Value + " " );
return ;
}
PrintNodeAtLevel(node.Left, level - 1 );
PrintNodeAtLevel(node.Right, level - 1 );
}
public int GetDepth()
{
if (Root == null )
return 0 ;
return GetDepth(Root);
}
private int GetDepth(Node < T > node)
{
if (node == null ) return 0 ;
int l = GetDepth(node.Left);
int r = GetDepth(node.Right);
return Math.Max(l,r) + 1 ;
}
}
搜索二叉树:
搜索、包含、增加删除、获取父节点
class
BinarySearchTree
<
T
>
: BinaryTree
<
T
>
{
public Node < T > Search(T data)
{
Node < T > current = Root;
while (current != null )
{
int result = comparer.Compare(current.Value, data);
if (result == 0 )
return current;
else if (result > 0 )
current = current.Left;
else if (result < 0 )
current = current.Right;
}
return current;
}
public bool Contains(T data)
{
Node < T > current = Root;
int result;
while (current != null )
{
result = comparer.Compare(current.Value, data);
if (result == 0 )
return true ;
else if (result > 0 )
current = current.Left;
else if (result < 0 )
current = current.Right;
}
return false ;
}
public Node < T > GetParent(Node < T > node)
{
Node < T > current = base .Root, parent = null ;
while (current != null )
{
int result = comparer.Compare(current.Value, node.Value);
if (result == 0 )
return parent;
else if (result > 0 )
{
parent = current;
current = current.Left;
}
else if (result < 0 )
{
parent = current;
current = current.Right;
}
}
return parent;
}
public void Add(T data)
{
Node < T > n = new Node < T > (data);
int result;
Node < T > current = base .Root, parent = null ;
while (current != null )
{
result = comparer.Compare(current.Value, data);
if (result == 0 )
return ;
else if (result > 0 )
{
parent = current;
current = current.Left;
}
else if (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;
}
}
public bool Remove(T data)
{
if ( base .Root == null )
return false ;
Node < T > current = base .Root, parent = null ;
int result = comparer.Compare(current.Value, data);
while (result != 0 )
{
if (result > 0 )
{
parent = current;
current = current.Left;
}
else if (result < 0 )
{
parent = current;
current = current.Right;
}
if (current == null )
return false ;
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;
else if (result < 0 )
parent.Right = current.Left;
}
}
else if (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;
else if (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;
else if (result < 0 )
parent.Right = leftmost;
}
}
current.Left = current.Right = null ;
current = null ;
return true ;
}
}
{
public Node < T > Search(T data)
{
Node < T > current = Root;
while (current != null )
{
int result = comparer.Compare(current.Value, data);
if (result == 0 )
return current;
else if (result > 0 )
current = current.Left;
else if (result < 0 )
current = current.Right;
}
return current;
}
public bool Contains(T data)
{
Node < T > current = Root;
int result;
while (current != null )
{
result = comparer.Compare(current.Value, data);
if (result == 0 )
return true ;
else if (result > 0 )
current = current.Left;
else if (result < 0 )
current = current.Right;
}
return false ;
}
public Node < T > GetParent(Node < T > node)
{
Node < T > current = base .Root, parent = null ;
while (current != null )
{
int result = comparer.Compare(current.Value, node.Value);
if (result == 0 )
return parent;
else if (result > 0 )
{
parent = current;
current = current.Left;
}
else if (result < 0 )
{
parent = current;
current = current.Right;
}
}
return parent;
}
public void Add(T data)
{
Node < T > n = new Node < T > (data);
int result;
Node < T > current = base .Root, parent = null ;
while (current != null )
{
result = comparer.Compare(current.Value, data);
if (result == 0 )
return ;
else if (result > 0 )
{
parent = current;
current = current.Left;
}
else if (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;
}
}
public bool Remove(T data)
{
if ( base .Root == null )
return false ;
Node < T > current = base .Root, parent = null ;
int result = comparer.Compare(current.Value, data);
while (result != 0 )
{
if (result > 0 )
{
parent = current;
current = current.Left;
}
else if (result < 0 )
{
parent = current;
current = current.Right;
}
if (current == null )
return false ;
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;
else if (result < 0 )
parent.Right = current.Left;
}
}
else if (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;
else if (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;
else if (result < 0 )
parent.Right = leftmost;
}
}
current.Left = current.Right = null ;
current = null ;
return true ;
}
}
<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"></script>