C#/수업 내용

[C#] Tree

JSH1 2021. 9. 24. 12:37

나무구조, 계층적 자료구조



        public App()
        {
            BinaryTree bt = new BinaryTree();
            Node root = bt.AddChild(bt.Root, "A");
            bt.AddChild(root, "B");
            bt.AddChild(root, "C");
            bt.AddChild(root, "D");
        }
    class BinaryTree
    {
        public class Node
        {
            public string Data { get; private set; }
            public Node Left { get; set; }
            public Node Right { get; set; }

            public Node(string data)
            {
                this.Data = data;
            }
        }

        public Node Root { get; private set; }

        public BinaryTree()
        {
        }

        public Node AddChild(Node root, string data)
        {
            if (root == null)
            {
                Console.WriteLine("Root Node: {0}", data);
                return Root = new Node(data);
            }

            else if (root.Left == null)
            {
                Console.WriteLine("{0} Child Node: {1}", root.Data, data);
                return root.Left = new Node(data);
            }

            else if (root.Left.Right == null)
            {
                Console.WriteLine("{0} Sibling Node: {1}", root.Left.Data, data);
                return root.Left.Right = new Node(data);
            }

            else
            {
                throw new InvalidOperationException();
            }
        }
    }


    class App
    {
        public App()
        {
            Tree bt = new Tree();
            Node root = bt.AddChild(bt.Root, "A");
            bt.AddChild(root, "B");
            bt.AddChild(root, "C");
            bt.AddChild(root, "D");

            Console.WriteLine("bt.Root.Next.Count: {0}", bt.Root.Next.Count);
            Console.WriteLine("root.Next.Count: {0}", root.Next.Count);

            bt.AddChild(root, "E");
            bt.AddChild(root, "F");
            bt.AddChild(root, "G");
            bt.AddChild(root, "H");
            bt.AddChild(root, "I");

            Console.WriteLine("bt.Root.Next[2].Next.Count: {0}", bt.Root.Next[2].Next.Count);
            Console.WriteLine("root.Next[2].Next.Count: {0}", root.Next[2].Next.Count);
        }
    }
    class Tree
    {
        public class Node
        {
            public string Data { get; private set; }
            public List<Node> Next = new List<Node>();

            public Node(string data)
            {
                Data = data;
            }
        }

        public Node Root { get; private set; }

        public Tree() 
        { 

        }

        public Node AddChild(Node root, string data)
        {
            if (root == null)
            {
                return Root = new Node(data);
            }
            else
            {
                if (root.Next.Count >= 3)
                {
                    root.Next[2].Next.Add(new Node(data));
                    return root;
                }

                else
                {
                    root.Next.Add(new Node(data));
                    return root;
                }
            }
        }
    }


 

'C# > 수업 내용' 카테고리의 다른 글

[C#] 이진 트리 level order traversal  (0) 2021.09.27
[C#] 일반 트리 level order traversal  (0) 2021.09.26
[C#] Queue 복습  (0) 2021.09.24
[C#] Queue  (0) 2021.09.23
[C#] Stack 복습  (0) 2021.09.23