03-树3 Tree Traversals Again (25 分)

03-树3 Tree Traversals Again (25 分)

An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.


Figure 1

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.

Output Specification:

For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.

Sample Input:

6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop

Sample Output:

3 4 2 6 5 1



代码是通过上以题的代码改的,很多没用的,自己删删吧.
因为是之前写的代码,当时没想着记录下来.

里面没注释
以后的改吧.


using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Net;
using System.Text;
using System.Xml;

struct Point
{
    public string C;
    public string X;
    public string Y;
    public Point(string v1, string v2, string v3) : this()
    {
        this.C = v1;
        this.X = v2;
        this.Y = v3;
    }
}
public class Link
{
    public Link()
    {

    }
    public string v;
    public Link left;
    public Link right;
    public Link Father => this;
}
class P
{


    public XmlDocument doc = new XmlDocument();
    public P(string v2) : this(int.Parse(v2))
    {

    }
    List<Point> tempList = new List<Point>();
    public P(int v1)
    {
        if (v1 > 0)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < v1; i++)
            {
                var temp = Console.ReadLine().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);


                tempList.Add(new Point(i.ToString(), temp[0], temp[1]));
                sb.Append(temp[0]);
                sb.Append(temp[1]);
            }
            var item = new Point();
            string sss = sb.ToString();
            for (int i = 0; i < v1; i++)
            {
                if (!sss.Contains(i + ""))
                {
                    item = tempList[i];
                }
            }



            var xNode = doc.CreateNode(XmlNodeType.Element, "item", "");

            var attr1 = doc.CreateAttribute("x");
            attr1.Value = item.X;
            var attr2 = doc.CreateAttribute("y");
            attr2.Value = item.Y;
            var attr3 = doc.CreateAttribute("n");
            attr3.Value = item.C;

            xNode.Attributes.Append(attr1);
            xNode.Attributes.Append(attr2);
            xNode.Attributes.Append(attr3);
            GetNodes(xNode);

            doc.AppendChild(xNode);

        }
    }

    private void GetNodes(XmlNode xNode)
    {
        int x = -1;
        int y = -1;
        if (int.TryParse(xNode.Attributes["x"].Value, out x))
        {

            var item = tempList[x];

            var node = doc.CreateNode(XmlNodeType.Element, "item", "");
            var attr1 = doc.CreateAttribute("x");
            attr1.Value = item.X;
            var attr2 = doc.CreateAttribute("y");
            attr2.Value = item.Y;
            var attr3 = doc.CreateAttribute("n");
            attr3.Value = item.C;

            node.Attributes.Append(attr1);
            node.Attributes.Append(attr2);
            node.Attributes.Append(attr3);
            GetNodes(node);

            xNode.AppendChild(node);
        }
        if (int.TryParse(xNode.Attributes["y"].Value, out y))
        {

            var item = tempList[y];

            var node = doc.CreateNode(XmlNodeType.Element, "item", "");
            var attr1 = doc.CreateAttribute("x");
            attr1.Value = item.X;
            var attr2 = doc.CreateAttribute("y");
            attr2.Value = item.Y;
            var attr3 = doc.CreateAttribute("n");
            attr3.Value = item.C;

            node.Attributes.Append(attr1);
            node.Attributes.Append(attr2);
            node.Attributes.Append(attr3);
            GetNodes(node);

            xNode.AppendChild(node);
        }
    }


    List<XmlNode> xnList = new List<XmlNode>();
    public string 获取子叶上为先(XmlNode node)
    {
        xnList.Add(node);
        string s = "";
        for (int i = 0; i < xnList.Count; i++)
        {
            if (xnList[i].HasChildNodes)
            {
                foreach (XmlNode item in xnList[i].ChildNodes)
                {
                    xnList.Add(item);
                }
            }
            else
            {
                s += xnList[i].Attributes["n"].Value + " ";
            }
        }

        return s;

    }
    public string 获取子叶左为先(XmlNode node)
    {
        string s = "";

        if (node.HasChildNodes)
        {
            foreach (XmlNode item in node.ChildNodes)
            {
                s += 获取子叶左为先(item);
            }


        }
        else
        {
            s += node.Attributes["n"].Value + " ";
        }



        return s;

    }
}


class PP
{
    public XmlDocument doc = new XmlDocument();
    public PP(string v2) : this(int.Parse(v2))
    {

    }
    public XmlElement ele;
    public PP(int v1)
    {
        var first = true;
        if (v1 > 0)
        {
            ele = doc.CreateElement("item");

            var link = ele;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < v1 * 2; i++)
            {
                var temp = Console.ReadLine().Split(new[] { " " }, StringSplitOptions.RemoveEmptyEntries);

                switch (temp[0])
                {
                    case "Push":
                        if (link.Attributes["value"] != null && link.Attributes["value"].Value != "")
                        {
                            var node = doc.CreateElement("item");
                            node.SetAttribute("value", temp[1]);
                            node.SetAttribute("poped", "false");
                            link.AppendChild(node);
                            link = node;
                        }
                        else
                        {
                            link.SetAttribute("value", temp[1]);
                            link.SetAttribute("poped", "false");
                        }

                        break;
                    case "Pop":
                        DDD:
                        if (link != null)
                        {
                            if (link.GetAttribute("poped") == "false")
                            {
                                link.SetAttribute("poped", "true");
                            }
                            else
                            {
                                link = link.ParentNode as XmlElement;
                                goto DDD;
                            }
                        }
                        break;

                }


            }

        }
    }

}
class T
{


    static bool 对比(XmlNode p1, XmlNode p2)
    {
        if (p1.Attributes["n"].Value == p2.Attributes["n"].Value)
        {

            if (p1.ChildNodes.Count == 0 &&
                        p2.ChildNodes.Count == 0)
            {

                return true;
            }
            if (p1.ChildNodes.Count == 1 &&
                p2.ChildNodes.Count == 1)
            {
                return 对比(p1.ChildNodes[0], p2.ChildNodes[0]);
            }
            if (p1.ChildNodes.Count == 2 &&
                      p2.ChildNodes.Count == 2)
            {
                var b00 = 对比(p1.ChildNodes[0], p2.ChildNodes[0]);
                var b11 = 对比(p1.ChildNodes[1], p2.ChildNodes[1]);
                var b10 = 对比(p1.ChildNodes[1], p2.ChildNodes[0]);
                var b01 = 对比(p1.ChildNodes[0], p2.ChildNodes[1]);
                return (b00 && b11) || (b10 && b01);
            }
        }
        return false;
    }

    static string digui(XmlElement x)
    {
        string s = "";
        if (x.HasChildNodes)
        {
            s += digui(x.FirstChild as XmlElement);
        }
        else
        {
            s += x.Attributes["value"].Value + " ";
            if (x.NextSibling != null)
            {
                x = x.NextSibling as XmlElement;
                x.ParentNode.RemoveChild(x.ParentNode.FirstChild);

                s += digui(x);
            }
            else
            {
                if (x.ParentNode == null)
                {
                    return s;
                }
                else
                {
                    x = x.ParentNode as XmlElement;
                    while (x.HasChildNodes)
                    {
                        x.RemoveChild(x.FirstChild);
                    }
                    s += digui(x);
                }
            }
        }

        return s;
    }
    static void Main(string[] args)
    {
        PP p1 = new PP(Console.ReadLine());

        XmlElement x = p1.ele;
       

        gotoa(x);
        return;


    }

   static  void gotoa(XmlElement x)
    {
        StringBuilder sb = new StringBuilder();
        BBB:
        while (x.HasChildNodes)
        {
            x = x.FirstChild as XmlElement;
        }


        AAA:

        sb.Append(x.Attributes["value"].Value + " ");
        if (x.NextSibling != null)
        {
            x = x.NextSibling as XmlElement;
            x.ParentNode.RemoveChild(x.ParentNode.FirstChild);

            goto BBB;
        }

        if (x.ParentNode == null)
        {
            goto CCC;
        }
        x = x.ParentNode as XmlElement;
        while (x.HasChildNodes)
        {
            x.RemoveChild(x.FirstChild);
        }
        goto AAA;
        CCC:

        Console.WriteLine(sb.ToString().Trim());
    }

}
 

猜你喜欢

转载自www.cnblogs.com/interim/p/9723215.html