[数据结构与算法]二叉树与二叉树遍历

一、构造二叉树
二叉树查找树由节点组成,所以需要有个Node类,这个类类似于链表实现中用到的Node类。首先一起来看看Node类的代码。

    public class Node
    {
        public int Data;    
        public Node Left;
        public Node Right;
        public void DisplayNode() 
        {
            Console.Write(Data+" ");
        }
    }

这里为存储在节点内的数据和每一个子节点宝航了public数据成员。displayNode方法允许显示存储在节点内的数据。这种特殊的Node类存储整数,但是人们可以很容易地把它调整为存储各种类型的数据,或者若需要,甚至可以声明为Object类型的data.
下面就准备来构造BST类,这个类只由一个数据成员构成。即一个表示BST根节点的Node对象。针对此类的默认构造器方法把根节点设置为NULL,同时创建一个空节点。
接下来需要Insert方法来向树内添加新的节点。这个方法有些复杂,而且会需要一些解释说明。这个方法中的第一步是创建一个Node对象,并且把Node存储的数据赋值给Data变量。这个数值会作为唯一的参数传递到此方法内。
插入的第二步是查看BST是否有根节点。如果没有,那么说明这是一个新的BST,并且要插入的节点就是根节点。如果是这种情况,那么就结束这个方法。否则,这个方法进入到下一步。
如果要添加的节点不是一个根节点,那么为了找到合适的插入点需遍历BST.这个过程类似链表的遍历。当一层层移动的时候,需要一个Node对象能赋值为当前节点。还需要把自身定位在BST内的根节点。
在BST内部,下一步就是确定放置新节点的位置。这个操作在一个while循环内执行,一旦为新节点找到正确的位置就跳出循环。确定节点正确位置的算法如下所示:
(1)把父节点设置为当前节点,根节点
(2)如果新节点内的数值小于当前节点内的数据值,那么吧当前节点设置为当前节点的左子节点。如果新节点内的数据值大于当前节点的数据值,那么跳到步骤4
(3)如果当前节点的左子节点的数值为空,就把心节点插入在这里并且退出循环。否则,跳到while循环的下一次循环操作中。
(4)把当前节点设置为当前节点的右子节点
(5)如果当前节点的右子节点的数值为空,就把心节点插入在这里并且退出循环。否则,跳到while循环的下一次循环操作中。

    public class Node
    {
        public int Data;    
        public Node Left;
        public Node Right;
        public void DisplayNode() 
        {
            Console.Write(Data+" ");
        }
    }
    public class BinarySearchTree 
    {
        public Node root;
        public BinarySearchTree() 
        {
            root = null;
        }

        public void Insert(int i) 
        {
            Node newNode = new Node();
            newNode.Data = i;
            if (root == null)
            {
                root = newNode;
            }
            else 
            {
                Node current = root;
                Node parent;
                while (true) 
                {
                    parent = current;
                    if (i < current.Data)
                    {
                        current = current.Left;
                        if (current == null)
                        {
                            parent.Left = newNode;
                            break;
                        }
                    }
                    else 
                    {
                        current = current.Right;
                        if (current == null) 
                        {
                            parent.Right = newNode;
                            break;
                        }
                    }
                }
            }

        }
        public void InOrder(Node theRoot) 
        {
            if (!(theRoot == null)) 
            {
                InOrder(theRoot.Left);
                theRoot.DisplayNode();
                InOrder(theRoot.Right);
            }
        }
        public void PreOrder(Node theRoot)
        {
            if (!(theRoot == null))
            {
                theRoot.DisplayNode();
                InOrder(theRoot.Left);
                InOrder(theRoot.Right);
            }
        }
        public void PostOrder(Node theRoot)
        {
            if (!(theRoot == null))
            {

                InOrder(theRoot.Left);
                InOrder(theRoot.Right);
                theRoot.DisplayNode();
            }
        }
    }

测试:

          BinarySearchTree nums = new BinarySearchTree();
            nums.Insert(23);
            nums.Insert(45);
            nums.Insert(16);
            nums.Insert(37);
            nums.Insert(3);
            nums.Insert(99);
            nums.Insert(22);
            Console.WriteLine("Inorder traversal:");
            nums.InOrder(nums.root);


            Console.Read();

相关文章

【啊哈!算法】算法3:最常用的排序——快速排序       ...
匿名组 这里可能用到几个不同的分组构造。通过括号内围绕的正...
选择排序:从数组的起始位置处开始,把第一个元素与数组中其...
public struct Pqitem { public int priority; ...
在编写正则表达式的时候,经常会向要向正则表达式添加数量型...
来自:http://blog.csdn.net/morewindows/article/details/6...