본문 바로가기

자료구조

이진트리 배열로 구현하기

더보기
        public App()
        {
            BinaryTree bt = new BinaryTree(15);
            bt.AddRoot("F");
            bt.AddLeft(0, "B");
            bt.AddRight(0, "G");
            bt.AddLeft(1, "A");
            bt.AddRight(1, "D");
            bt.AddRight(2, "I");
            bt.AddLeft(4, "C");
            bt.AddRight(4, "E");
            bt.AddLeft(6, "H");

            Console.WriteLine("루트: {0}", bt.GetRoot());
            Console.WriteLine("7의 부모: {0}", bt.GetParent(7));
            Console.WriteLine("4의 왼쪽: {0}", bt.GetLeft(4));
            Console.WriteLine("6의 오른쪽: {0}", bt.GetRight(6));
            bt.Print();
        }

        public void AddRoot(string data)
        {
            if (this.arr[0] == null)
            {
                this.arr[0] = data;
            }
            else
            {
                Console.WriteLine("루트가 이미 있습니다.");
            }
        }

        public void AddLeft(int parentIndex, string data)
        {
            if (arr[parentIndex] == null)
            {
                Console.WriteLine("부모 노드가 없습니다.");
            }
            else
            {
                arr[parentIndex * 2 + 1] = data;
            }
        }

        public void AddRight(int parentIndex, string data)
        {
            if (arr[parentIndex] == null)
            {
                Console.WriteLine("부모 노드가 없습니다.");
            }
            else
            {
                arr[parentIndex * 2 + 2] = data;
            }
        }

        public string GetRoot()
        {
            if (this.arr[0] == null)
            {
                return "없습니다.";
            }
            else
            {
                return this.arr[0];
            }
        }

        public string GetParent(int childIndex)
        {
            if (arr[(childIndex - 1) / 2] == null)
            {
                return "없습니다.";
            }
            else
            {
                return arr[(childIndex - 1) / 2];
            }
        }

        public string GetLeft(int parentIndex)
        {
            if (arr[parentIndex * 2 + 1] == null)
            {
                return "없습니다.";
            }
            else
            {
                return arr[parentIndex * 2 + 1];
            }
        }

        public string GetRight(int parentIndex)
        {
            if (arr[parentIndex * 2 + 2] == null)
            {
                return "없습니다.";
            }
            else
            {
                return arr[parentIndex * 2 + 2];
            }
        }

        public void Print()
        {
            for (int i = 0; i < this.arr.Length; i++)
            {
                if (this.arr[i] != null)
                {
                    Console.Write("{0} ", this.arr[i]);
                }
                else
                {
                    Console.Write("x ");
                }
            }
            Console.WriteLine();
        }

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

이진탐색트리 Add  (0) 2021.12.28
이진트리 PostOrder with Stack  (0) 2021.12.28
이진트리 InOrder with Stack  (0) 2021.12.28
이진트리 PostOrder 재귀구현  (0) 2021.12.28
이진트리 InOrder 재귀구현  (0) 2021.12.28