二叉树的创建、遍历和左右交换

本文详细介绍了如何使用C#实现二叉树的先序、中序、后序和层次遍历,以及节点交换算法。通过实例演示了每个遍历过程的具体步骤和代码实现,为读者提供了全面的理解和实践指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如题,代码如下:

    class BinaryTree
    {
        /// <summary>
        /// 创建二叉树,并返回根结点
        /// </summary>
        /// <returns></returns>
        public static nodes<string> BinTree()
        {
            nodes<string>[] list = new nodes<string>[8];
            //创建结点
            list[0] = new nodes<string>("A");
            list[1] = new nodes<string>("B");
            list[2] = new nodes<string>("C");
            list[3] = new nodes<string>("D");
            list[4] = new nodes<string>("E");
            list[5] = new nodes<string>("F");
            list[6] = new nodes<string>("G");
            list[7] = new nodes<string>("H");

            //利用层次方式创建二叉树
            list[0].LNode = list[1];
            list[0].RNode = list[2];
            list[1].RNode = list[3];
            list[2].LNode = list[4];
            list[2].RNode = list[5];
            list[3].LNode = list[6];
            list[3].RNode = list[7];
            return list[0];
        }


        /// <summary>
        /// 先序遍历
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rootNode"></param>
        public static void PreOrder<T>(nodes<T> rootNode)
        {
            if (rootNode != null)
            {
                Console.Write(rootNode.Data);
                PreOrder<T>(rootNode.LNode);
                PreOrder<T>(rootNode.RNode);
            }
        }

        /// <summary>
        /// 中序遍历
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rootNode"></param>
        public static void MidOrder<T>(nodes<T> rootNode)
        {
            if (rootNode != null)
            {
                MidOrder<T>(rootNode.LNode);
                Console.Write(rootNode.Data);
                MidOrder<T>(rootNode.RNode);
            }
        }

        /// <summary>
        /// 后序遍历
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rootNode"></param>
        public static void EndOrder<T>(nodes<T> rootNode)
        {
            if (rootNode != null)
            {
                EndOrder<T>(rootNode.LNode);
                EndOrder<T>(rootNode.RNode);
                Console.Write(rootNode.Data);
            }
        }

        /// <summary>
        /// 层次遍历
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rootNode"></param>
        public static void LayOrder<T>(nodes<T> rootNode)
        {
            nodes<T>[] list = new nodes<T>[20];
            int front = -1;
            int rear = -1;
            if (rootNode != null)
            {
                rear++;
                list[rear] = rootNode;
            }

            while (front != rear)
            {
                front++;
                rootNode = list[front];
                Console.Write(rootNode.Data);
                if (rootNode.LNode != null)
                {
                    rear++;
                    list[rear] = rootNode.LNode;
                }
                if (rootNode.RNode != null)
                {
                    rear++;
                    list[rear] = rootNode.RNode;
                }
            }
        }


        /// <summary>
        /// 交换
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rootNode"></param>
        public static void SortTree<T>(nodes<T> rootNode)
        {
            if (rootNode.LNode != null)
                SortTree<T>(rootNode.LNode);
            if (rootNode != null)
            {
                nodes<T> node = new nodes<T>();
                if (rootNode.LNode != null)
                {
                    node = rootNode.LNode;
                }
                if (rootNode.RNode != null)
                {
                    rootNode.LNode = rootNode.RNode;
                }
                if (node != null)
                {
                    rootNode.RNode = node;
                }
            }
            if (rootNode.LNode != null)
                SortTree<T>(rootNode.LNode);
        }
    }

    class nodes<T>
    {
        public nodes() { }
        public nodes(T d)
        {
            this.data = d;
        }
        T data;
        nodes<T> lNode, rNode, pNode;

        internal nodes<T> PNode
        {
            get { return pNode; }
            set { pNode = value; }
        }

        internal nodes<T> RNode
        {
            get { return rNode; }
            set { rNode = value; }
        }

        internal nodes<T> LNode
        {
            get { return lNode; }
            set { lNode = value; }
        }

        public T Data
        {
            get { return data; }
            set { data = value; }
        }

    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值