C# 实现最短二叉树路径

来源:互联网 发布:2017程序员看的书 编辑:程序博客网 时间:2024/06/07 05:15

条件:一颗普通的二叉树,给定二叉树的两个节点,nodeA、nodeB

输出:找到nodeA和nodeB的最短路径,并输出:如的d节点到h节点的最短路径为红线所示:



using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApplication2{    class Program    {        static void Main(string[] args)        {            #region 构造一棵已知的二叉树              //*                A            //*              B    C            //*            D  E     F            Node<string>[] binTree = new Node<string>[8];            //创建结点              binTree[0] = new Node<string>("A");            binTree[1] = new Node<string>("B");            binTree[2] = new Node<string>("C");            binTree[3] = new Node<string>("D");            binTree[4] = new Node<string>("E");            binTree[5] = new Node<string>("F");            //使用层次遍历二叉树的思想,构造一个已知的二叉树              binTree[0].LNode = binTree[1];            binTree[0].RNode = binTree[2];            binTree[0].PNode = null;            binTree[1].LNode = binTree[3];            binTree[1].RNode = binTree[4];            binTree[1].PNode = binTree[0];            binTree[2].LNode = null;            binTree[2].RNode = binTree[5];            binTree[2].PNode = binTree[0];            binTree[3].LNode = null;            binTree[3].RNode = null;            binTree[3].PNode = binTree[1];            binTree[4].LNode = null;            binTree[4].RNode = null;            binTree[4].PNode = binTree[1];            binTree[5].LNode = null;            binTree[5].RNode = null;            binTree[5].PNode = binTree[2];            #endregion            string minPath = outputMinPath(binTree, binTree[0], binTree[3], binTree[5]);            Console.WriteLine(minPath);        }        //输出最短路径        public static string outputMinPath(Node<string>[] binTree, Node<string> nodeRoot, Node<string> node1, Node<string> node2)        {            //获取node1到根节点的路径            string[] node1ToRoot = getRootPath(binTree, node1);            //获取node1到根节点的路径            string[] node2ToRoot = getRootPath(binTree, node2);            //下面比较node1ToRoot 与 node2ToRoot 数组,求最短路径            string minPath = getMinPath(node1ToRoot, node2ToRoot);            //获取node2到根节点的路径            return node1.Data + "-->" + minPath + "-->" + node2.Data;        }        public static string getMinPath(string[] node1ToRoot, string[] node2ToRoot)        {            List<string> node1ToRootList = node1ToRoot.ToList();            List<string> node2ToRootList = node2ToRoot.ToList();            string minPath = "";            for (int i = 0; i < node1ToRoot.Length; i++)            {                //第一个重复的路径,相连接即为最短路径                int cfindex = node2ToRootList.IndexOf(node1ToRoot[i]);                if (cfindex > 0)                {                    //第一个节点的路径                    for (int j = 0; j < i; j++)                    {                        minPath = minPath + node1ToRoot[j].ToString() + "-->";                    }                    //加上重复节点                    minPath = minPath + node1ToRoot[i].ToString() + "-->";                    //第二个节点路径                    for (int k = cfindex - 1; k > -1; k--)                    {                        minPath = minPath + node2ToRoot[k].ToString() + "-->";                    }                    //去除后面的"-->"                    minPath = minPath.Substring(0, minPath.Length - 3);                }            }            return minPath;        }        //获取某节点到根节点的路径        public static string[] getRootPath(Node<string>[] binTree, Node<string> node)        {            List<string> list = new List<string>();            while (node.PNode != null)            {                list.Add(node.PNode.Data);                node = node.PNode;            }            return list.ToArray();        }        #region 二叉树结点数据结构的定义           //二叉树结点数据结构包括数据域,左右结点以及父结点成员;          public class Node<T>        {            public T Data;            Node<T> Lnode, Rnode, Pnode;            public Node(T Data)            {                this.Data = Data;            }            public Node<T> LNode { get; set; }            public Node<T> RNode { get; set; }            public Node<T> PNode { get; set; }        }        #endregion    }}

最后结果如下:


0 0