示例#1
0
        //层次遍历
        //引入队列
        public void LevelOrder(MLNode <T> root)
        {
            Console.WriteLine("遍历开始:");
            if (root == null)
            {
                Console.WriteLine("没有结点!");
                return;
            }

            MLNode <T> temp = root;

            CSeqQueue <MLNode <T> > queue = new CSeqQueue <MLNode <T> >(50);

            queue.EnQueue(temp);
            while (!queue.IsEmpty())
            {
                temp = (MLNode <T>)queue.DeQueue();
                Console.WriteLine(temp.Data + " ");
                for (int i = 0; i < temp.Childs.Length; i++)
                {
                    if (temp.Childs[i] != null)
                    {
                        queue.EnQueue(temp.Childs[i]);
                    }
                }
            }
            Console.WriteLine("遍历结束!");
        }
示例#2
0
        //求结点t的双亲结点,如果t的双亲结点存在,返回双亲结点,否则返回空
        //按层序遍历的算法进行查找
        public MLNode <T> Parent(MLNode <T> t)
        {
            MLNode <T> temp = head;

            if (IsEmpty() || t == null)
            {
                return(null);
            }
            if (temp.Data.Equals(t.Data))
            {
                return(null);
            }
            CSeqQueue <MLNode <T> > queue = new CSeqQueue <MLNode <T> >(50);

            queue.EnQueue(temp);
            while (!queue.IsEmpty())
            {
                temp = (MLNode <T>)queue.DeQueue();
                for (int i = 0; i < temp.Childs.Length; i++)
                {
                    if (temp.Childs[i] != null)
                    {
                        if (temp.Childs[i].Data.Equals(t.Data))
                        {
                            return(temp);
                        }
                        else
                        {
                            queue.EnQueue(temp.Childs[i]);
                        }
                    }
                }
            }
            return(null);
        }