0

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

我正在学习二叉树遍历我已经获得了一些代码,我真的很难理解使用 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();
    }

}

}
4

2 回答 2

1

使用递归,卢克。(那么这只是您访问(和处理)左子树、右子树或当前节点本身的顺序的问题。)

于 2013-11-11T23:18:17.807 回答
0

在这里,也许一些不错的动画可以选择以您喜欢的方式遍历它,并添加和删除以便您可以拥有自己的示例可能会有所帮助:看看这个网站

或者也许是带有画外音的 CS 动画……看这里

于 2013-11-11T23:20:41.203 回答