본문 바로가기

자료구조

LCRS트리

목표

namespace HelloWorld
{
    class App
    {
        public App()
        {
            BinaryTree bt = new BinaryTree();
            Node root = bt.AddChild(bt.Root, "A");
            bt.AddChild(root, "B");
            bt.AddChild(root, "C");
        }
    }
}

추가

public Node AddChild(Node node, string data)
        {
            Node child = new Node(data);

            if (this.Root == null)
            {
                this.Root = child;
                return this.Root;
            }
            else if (node != this.Root)
            {
                if (node.Left == null)
                {
                    node.Left = child;
                    return node.Left;
                }
                else
                {
                    if (node.Left.Right == null)
                    {
                        node.Left.Right = child;
                        return node.Left.Right;
                    }
                    else
                    {
                        throw new Exception("더이상 추가할수 없습니다.");
                    }
                }
            }
            else
            {
                if (this.Root.Left == null)
                {
                    this.Root.Left = child;
                    return this.Root.Left;
                }
                else
                {
                    if (this.Root.Left.Right == null)
                    {
                        this.Root.Left.Right = child;
                        return this.Root.Left.Right;
                    }
                    else
                    {
                        throw new Exception("더이상 추가할수 없습니다.");
                    }
                }
            }
        }

'자료구조' 카테고리의 다른 글

LCRS트리 LevelOrder  (0) 2021.12.28
일반트리  (0) 2021.12.28
트리 LCRS Expression  (0) 2021.12.28
Queue 복습  (0) 2021.12.28
Queue Count  (0) 2021.12.28