使用布尔逻辑遍历二叉树

时间:2013-11-11 23:04:12

标签: c# if-statement boolean

让我先说对不对,如果标题错了。

我正在学习二叉树遍历我已经获得了一些代码,我真的很难理解使用if / else语句和布尔逻辑的逻辑。

我必须使用postorder,preorder和inorder方法遍历树。

preorderTraversal方法已准备好并正常工作。

任何建议都将不胜感激。

守则:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BinaryTree
{
class BinaryTree<T>
{
   class BinaryTreeNode
    {
        public BinaryTreeNode Left;
        public BinaryTreeNode Right;
        public BinaryTreeNode Parent;
        public T Data;
        public Boolean processed;

        public BinaryTreeNode()
        {
            Left = null;
            Right = null;
            Parent = null;
            processed = false;
        }
    }

    BinaryTreeNode Root;
    Comparison<T> CompareFunction;

    public BinaryTree(Comparison<T> theCompareFunction)
    {
        Root = null;
        CompareFunction = theCompareFunction;
    }

    public static int CompareFunction_Int(int left, int right)
    {
        return left - right;
    }

    public void preorderTraversal()
    {
        if (Root != null)
        {
            BinaryTreeNode currentNode = Root;
            Boolean process = true;
            int nodesProcessed = 0;
            while (nodesProcessed != 11)
            {
                if (process)
                {
                    Console.Write(currentNode.Data.ToString());
                    currentNode.processed = true;
                    nodesProcessed = nodesProcessed +1;
                }
                process = true;

                if (currentNode.Left != null && currentNode.Left.processed == false)
                {
                    currentNode = currentNode.Left;
                }
                else if (currentNode.Right != null && currentNode.Right.processed ==            
                false)
                {
                    currentNode = currentNode.Right;
                }
                else if (currentNode.Parent != null)
                {
                    currentNode = currentNode.Parent;
                    process = false;
                }
            }
        }
        else
        {
            Console.WriteLine("There is no tree to process");
        }
    }

    public void postorderTraversal()
    {
        if (Root != null)
        {
        }
        else
        {
            Console.WriteLine("There is no tree to process");
        }
    }
    public void inorderTraversal()
    {
        if (Root != null)
        {
        }
        else
        {
            Console.WriteLine("There is no tree to process");
        }
    }        
     public static int CompareFunction_String(string left, string right)
    {
        return left.CompareTo(right);
    }

    public void Add(T Value)
    {
        BinaryTreeNode child = new BinaryTreeNode();
        child.Data = Value;

        if (Root == null)
        {
            Root = child;
        }
        else
        {

            BinaryTreeNode Iterator = Root;
            while (true)
            {

                int Compare = CompareFunction(Value, Iterator.Data);

                if (Compare <= 0)
                    if (Iterator.Left != null)
                    {

                        Iterator = Iterator.Left;
                        continue;
                    }
                    else
                    {

                        Iterator.Left = child;
                        child.Parent = Iterator;
                        break;
                    }

                if (Compare > 0)
                    if (Iterator.Right != null)
                    {

                        Iterator = Iterator.Right;
                        continue;
                    }
                    else
                    {

                        Iterator.Right = child;
                        child.Parent = Iterator;
                        break;
                    }
            }
        }
    }

    public bool Find(T Value)
    {
        BinaryTreeNode Iterator = Root;
        while (Iterator != null)
        {
            int Compare = CompareFunction(Value, Iterator.Data);

            if (Compare == 0) return true;
            if (Compare < 0)
            {

                Iterator = Iterator.Left;
                continue;
            }

            Iterator = Iterator.Right;
        }
        return false;
    }

    BinaryTreeNode FindMostLeft(BinaryTreeNode start)
    {
        BinaryTreeNode node = start;
        while (true)
        {
            if (node.Left != null)
            {
                node = node.Left;
                continue;
            }
            break;
        }
        return node;
    }


    public IEnumerator<T> GetEnumerator()
    {
        return new BinaryTreeEnumerator(this);
    }



    class BinaryTreeEnumerator : IEnumerator<T>
    {
        BinaryTreeNode current;
        BinaryTree<T> theTree;
        public BinaryTreeEnumerator(BinaryTree<T> tree)
        {
            theTree = tree;
            current = null;
        }


        public bool MoveNext()
        {

            if (current == null)
                current = theTree.FindMostLeft(theTree.Root);
            else
            {

                if (current.Right != null)
                    current = theTree.FindMostLeft(current.Right);
                else
                {

                    T CurrentValue = current.Data;

                    while (current != null)
                    {
                        current = current.Parent;
                        if (current != null)
                        {
                            int Compare = theTree.CompareFunction(current.Data,                    
CurrentValue);                            
                         if (Compare < 0) continue;
                        }
                        break;
                    }
                }
            }
            return (current != null);
        }

        public T Current
        {
            get
            {
                if (current == null)
                    throw new InvalidOperationException();
                return current.Data;
            }
        }

        object System.Collections.IEnumerator.Current
        {
            get
            {
                if (current == null)
                    throw new InvalidOperationException();
                return current.Data;
            }
        }

        public void Dispose() { }

        public void Reset() { current = null; }

    }
}

class TreeTest
{
    static BinaryTree<int> Test = new BinaryTree<int>                                                                
(BinaryTree<int>.CompareFunction_Int);
    static void Main(string[] args)
    {

        // Build the tree
        Test.Add(5);
        Test.Add(2);
        Test.Add(1);
        Test.Add(3);
        Test.Add(3); // Duplicates are OK
        Test.Add(4);
        Test.Add(6);
        Test.Add(10);
        Test.Add(7);
        Test.Add(8);
        Test.Add(9);
        // Test if we can find values in the tree

        for (int Lp = 1; Lp <= 10; Lp++)

            Console.WriteLine("Find ({0}) = {1}", Lp, Test.Find(Lp));

        // Test if we can find a non-existing value
        Console.WriteLine("Find (999) = {0}", Test.Find(999));

        // Iterate over all members in the tree -- values are returned in sorted order
        foreach (int value in Test)
        {
            Console.WriteLine("Value: {0}", value);
        }

        Console.WriteLine("Preorder traversal");
        Test.preorderTraversal();
        Console.ReadKey();
    }

}

}

2 个答案:

答案 0 :(得分:1)

使用Recursion,卢克。 (然后,这只是你访问(和处理)左子树,右子树或当前节点本身的顺序的问题。)

答案 1 :(得分:0)

在这里,也许有一些不错的动画,可以选择以你喜欢的方式遍历它,添加和删除,这样你就可以拥有自己的例子:看看this site

或者可能是带有语音的CS动画......看here