ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 04/01 연결리스트로 구현한 BinaryTree & Preorder Traversal 복습
    자료구조/수업내용 2021. 4. 1. 10:21
    728x90

     

    Program.cs

    using System;
    
    namespace Study09
    {
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("Main");
                new App();
            }
        }
    }
    

     

    App.cs

    using System;
    
    namespace Study09
    {
    
        public class App
        {
    
            //생성자 
            public App()
            {
                Console.WriteLine("App");
                BinaryTree tree = new BinaryTree("A");
                tree.root.left = new BinaryTreeNode("B");
                tree.root.right = new BinaryTreeNode("C");
                tree.root.left.left = new BinaryTreeNode("D");
                tree.Print();
            }
    
        }
    }
    

     

    BinaryTreeNode.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study09
    {
        public class BinaryTreeNode
        {
            public string data;
            public BinaryTreeNode left;
            public BinaryTreeNode right;
    
            public BinaryTreeNode(string data)
            {
                this.data = data;
            }
        }
    }
    

     

    BinaryTree.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Study09
    {
        public class BinaryTree
        {
            public BinaryTreeNode root;
            public BinaryTree(string data)
            {
                this.root = new BinaryTreeNode(data);
            }
            public void Print()
            {
                PrintPreorderTraversal(this.root);
            }
            public void PrintPreorderTraversal(BinaryTreeNode node)
            {
                if (node == null) return;
                Console.Write("{0} ", node.data);
                this.PrintPreorderTraversal(node.left);
                this.PrintPreorderTraversal(node.right);
            }
        }
    }
    
    728x90
Designed by Tistory.