Beispiel #1
0
        static void Main(string[] args)
        {
            MyTreeNode node1 = new MyTreeNode()
            {
                Data = 1
            };
            MyTreeNode node2 = new MyTreeNode()
            {
                Data = 2
            };
            MyTreeNode node3 = new MyTreeNode()
            {
                Data = 3
            };
            MyTreeNode node4 = new MyTreeNode()
            {
                Data = 4
            };
            MyTreeNode node5 = new MyTreeNode()
            {
                Data = 5
            };
            MyTreeNode node6 = new MyTreeNode()
            {
                Data = 6
            };

            node1.Left  = node2;
            node1.Right = node3;

            node2.Left  = node4;
            node2.Right = node5;

            node3.Right = node6;

            MyTreeNode node7 = new MyTreeNode()
            {
                Data = 7
            };


            MyTreeNode parent = node1.FindNearestParent2(node1, node1);

            Console.WriteLine(parent.Data);


            //var paths = node1.FindAllPath();

            //foreach(var p in paths)
            //{
            //    foreach(var node in p)
            //    {
            //        Console.Write(node.Data + "-->");
            //    }

            //    Console.WriteLine(  );
            //}
        }
Beispiel #2
0
        /// <summary>
        /// 判断当前树当中,是否包含目标节点
        /// </summary>
        /// <param name="root">树的根节点</param>
        /// <param name="target">目标节点</param>
        /// <returns>True - root 这棵树中包含target节点, false不包含</returns>
        private static bool Contains(MyTreeNode root, MyTreeNode target)
        {
            if (root != null && target != null)
            {
                if (root == target)
                {
                    return(true);
                }

                return(Contains(root.Left, target) || Contains(root.Right, target));
            }

            return(false);
        }
Beispiel #3
0
        public MyTreeNode FindNearestParent2(MyTreeNode first, MyTreeNode second)
        {
            List <List <MyTreeNode> > allPaths = this.FindAllPath();

            List <MyTreeNode> firstPath  = null;
            List <MyTreeNode> secondPath = null;

            for (int index = 0; index < allPaths.Count; index++)
            {
                for (int i = 0; i < allPaths[index].Count; i++)
                {
                    if (allPaths[index][i] == first)
                    {
                        firstPath = allPaths[index];
                    }

                    if (allPaths[index][i] == second)
                    {
                        secondPath = allPaths[index];
                    }
                }
            }

            MyTreeNode parent = null;

            if (firstPath != null && firstPath.Count > 0 && secondPath != null && secondPath.Count > 0)
            {
                for (int index = 0; index < firstPath.Count && index < secondPath.Count; index++)
                {
                    if (firstPath[index] != first && secondPath[index] != second)
                    {
                        parent = firstPath[index];
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(parent);
        }
Beispiel #4
0
        public MyTreeNode FindNearestParent(MyTreeNode first, MyTreeNode second)
        {
            var leftContainsFirst   = Contains(this.Left, first);
            var rightContainsSecond = Contains(this.Right, second);

            var leftContainsSecond = Contains(this.Left, second);
            var rightContainsFirst = Contains(this.Right, first);

            if ((leftContainsFirst && rightContainsSecond) || (leftContainsSecond && rightContainsFirst))
            {
                return(this);
            }
            else
            {
                if (leftContainsFirst && leftContainsSecond)
                {
                    if (this.Left != null)
                    {
                        if (this.Left == first || this.Left == second)
                        {
                            return(this);
                        }

                        return(this.Left.FindNearestParent(first, second));
                    }
                }

                if (rightContainsFirst && rightContainsSecond)
                {
                    if (this.Right != null)
                    {
                        if (this.Right == first || this.Right == second)
                        {
                            return(this);
                        }
                        return(this.Right.FindNearestParent(first, second));
                    }
                }
            }

            return(null);
        }