LoveCherry

技术无极限

博客园 首页 新随笔 联系 订阅 管理
  192 Posts :: 0 Stories :: 3240 Comments :: 656 Trackbacks

节点:

 

 class Node<T>
    {
        
public T Value { getset; }

        
public Node<T> Left { getset; }

        
public Node<T> Right { getset; }

        
public Node(T value, Node<T> left, Node<T> right)
        {
            Value 
= value;
            Left 
= left;
            Right 
= right;
        }

        
public Node(T value) : this(value, nullnull) { }
    }

 

 

二叉树:

 

前中后深度优先遍历非递归,上到下(下到上)宽度优先遍历非递归,输出某层,获取深度

   class BinaryTree<T>
    {
        
protected IComparer<T> comparer = Comparer<T>.Default; 

        
public Node<T> Root { getset; }

        
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 == nullreturn 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;
        }
    }

 

 

posted on 2008-08-17 17:47 lovecherry 阅读(4750) 评论(6) 编辑 收藏

Feedback

咩来噶?
 回复 引用   

#2楼 2009-12-09 16:58 rtgryt [未注册用户]
freshwater pearl necklace
freshwater pearl bracelet
freshwater pearl pendant
freshwater pearl earrings
freshwater pearl strands
wholesale coral jewelry
freshwater pearl
freshwater pearl jewelry
pearl jewelry wholesale
wholesale pearl jewelry
pearl necklace

freshwater pearl necklace
akoya pearl necklace
pearl earrings
freshwater pearl earrings
akoya pearl earrings
pearl pendant
freshwater pearl pendant
akoya pearl pendant
pearl bracelet
freshwater pearl bracelet
akoya pearl bracelet
pearl ring

 回复 引用   

#3楼 2009-12-09 17:01 rtgryt [未注册用户]
freshwater pearl ring
akoya pearl ring
shell pearl
shell pearl jewelry
shell jewelry
coral jewelry
turquoise jewelry
silver pearl jewelry
cultured pearl jewelry
pearl strand
freshwater pearl strand
akoya pearl strand
pearl strands
pearl beads
freshwater pearl beads
akoya pearl beads
loose pearl
loose pearls
loose freshwater pearl
loose akoya pearl
freshwater loose pearl
akoya loose pearl
pearl necklace style
twisted pearl necklace
multi strand necklace
multi strand pearl necklace
pearl jewelry
akoya pearl jewelry
wish pearl
wish pearl oyster
wish pearl gift set
pearl jewelry

 回复 引用   

#4楼 2010-06-23 09:56 cactus123456      
我全文引用了
http://cactusprogram.appspot.com/?p=313005

 回复 引用 查看   

#5楼 2010-06-23 10:00 cactus123456      
我全文引用了
 回复 引用 查看   

#6楼 2010-11-12 23:52 xvhfeng      
正在研究二叉树,搜到了你的算法!!!留下脚印!
 回复 引用 查看