如何在树状结构中组织数据?

时间:2015-06-22 14:11:29

标签: c# tree

我从未使用过任何树状结构,但现在我需要制作一个。这是我想要做的:

PowerPoint中有五个子弹点级别。这就像一个树结构,但我在powerpoint类中找不到任何返回以这种方式组织的数据的东西。但是,我能够识别特定行是1级还是2级还是....级别5,并且希望根据此信息填充树。总的来说,我试图将这些数据设置为Oragnized,然后进一步使用它将动画效果分成单独的幻灯片。实际上只是在代码中看到我想要做的事情,并纠正我做错的事情:

//directory to temporary save .png files
            var dir = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\slides_png\";
            Directory.CreateDirectory(dir);


            //Add each slide in the presentation to the array

            Microsoft.Office.Interop.PowerPoint.Slide[] aSlides = new Microsoft.Office.Interop.PowerPoint.Slide[presentation.Slides.Count];

            foreach (Microsoft.Office.Interop.PowerPoint.Slide oSl in presentation.Slides)
                aSlides[oSl.SlideIndex - 1] = oSl;


            //check if an object in slide is a text box and if it is animated

            if (Convert.ToBoolean(aSlides[0].Shapes[1].AnimationSettings.Animate) && (aSlides[0].Shapes[1].Type.ToString() == "msoAutoShape" || aSlides[0].Shapes[1].Type.ToString() == "msoTextBox"))
            { 

            int all_lines_count = aSlides[0].Shapes[1].TextFrame.TextRange.Paragraphs().Count; //counts number of bullet lines and sub-bullet lines
            Console.WriteLine("Total Number of Lines is: "+ Convert.ToString(all_lines_count));

            int xx = 0;

            int num_1st_levels = 0;
            int num_2nd_levels = 0;
            int num_3rd_levels = 0;
            int num_4th_levels = 0;
            int num_5th_levels = 0;
            string line_of_text = null;



            TreeView my_tree = new TreeView();
            my_tree.Nodes.Clear();

            for(int i = 1; i<=all_lines_count;i++) //get the number of 1st 2nd 3rd 4th and 5th level lines in entire textbox
            {


                line_of_text = aSlides[0].Shapes[1].TextFrame.TextRange.Paragraphs(i).Text;


                xx = aSlides[0].Shapes[1].TextFrame.TextRange.Paragraphs(i).IndentLevel; //returns the bullet level of particular line


                if(xx == 1 && line_of_text !=String.Empty)
                {
                    num_1st_levels++;
                    //now add the line to the root node of a tree
                    TreeNode node_level1 = my_tree.Nodes[i].Nodes.Add(line_of_text);

                }

                else if (xx == 2 && line_of_text != String.Empty)
                {
                    num_2nd_levels++;
                    //now add the line as child to the 1st_level 
                    TreeNode node_level2 = my_tree.Nodes[i].Nodes[i].Nodes.Add(line_of_text);
                }

                else if (xx == 3 && line_of_text != String.Empty)
                {
                    num_3rd_levels++;
                    //now add the line as child to 2nd_level
                    TreeNode node_level3 = my_tree.Nodes[i].Nodes[i].Nodes[i].Nodes.Add(line_of_text);
                }

                else if (xx == 4 && line_of_text != String.Empty)
                {
                    num_4th_levels++;
                    //now add the line as child to 3rd_level
                    TreeNode node_level4 = my_tree.Nodes[i].Nodes[i].Nodes[i].Nodes[i].Nodes.Add(line_of_text);
                }

                else if (xx == 5 && line_of_text != String.Empty)
                {
                    num_5th_levels++;
                    //now add the line as child to 4th_level
                    TreeNode node_level5 = my_tree.Nodes[i].Nodes[i].Nodes[i].Nodes[i].Nodes[i].Nodes.Add(line_of_text);
                }

            }

1 个答案:

答案 0 :(得分:1)

可能是一个像这样的基本树结构......(这只是基本的)......

void Main()
{
    Tree t = new Tree<string>();  //replace string with your data type in this generic list

    //individually
    Node<string> nd1=t.AddNode("level 1");
    Node<string> nd2=nd1.AddNode("level 2");
    Node<string> nd3=nd2.AddNode("level 3");

    //in a loop
    var lst = new List<string>{"Level 1", "Level 2", "Level 3"};
    Node<string> parent=null;
    foreach (var itm in lst)
    {
        Node<string> nd=null;
        if (parent==null)
            nd=t.AddNode(itm);  //add as a root node in the tree
        else 
            nd=parent.AddNode(itm); //add as a child node to the parent node
        parent=nd;
    }



    //process all nodes
    foreach (var node in t.Nodes)
    {
        ProcessNode(node);
    }
}

//processes each node and it children in using recursive call
void ProcessNode(Node<string> node)
{
    System.Console.WriteLine(node.Value); //process current node (print)
    if (node.Children.Count>0)  //process child nodes
    {
        foreach (var nd in node.Children)
        {
            ProcessNode(nd); // recursive call to process child node
        }
    }
}


//tree node class
public class Node<T>  // where T: interface/abstract class your object derives from or the object itself
{
    public Node ()
    {
        this.Children=new List<Node<T>>();
    }
    public Node (T val) : this()
    {
        this.Value=val;
    }

    public T Value { get; set; }
    public List<Node<T>> Children { get; set; }
    public Node<T> this[int i] {
        get{ return this.Children[i]; }
        set{ this.Children[i]=value;}
    }

    //add a child node to a parent node and return the child
    public Node<T> AddNode(Node<T> parent, T val)
    {
        Node<T> child= new Node<T>(val);
        parent.Children.Add(child);
        return child;
    }


}





//Tree class
public class Tree<T>  // where T: interface/abstract class your object derives from or the object itself
{
    public Tree ()
    {
        this.Nodes = new List<Node<T>>();   
    }

    public List<Node<T>> Nodes { get; set; }

    public Node<T> AddNode(T val)
    {
        Node<T> child= new Node<T>(val);
        this.Nodes.Add(child);
        return child;
    }   
}