0401 연결트리로 구현한 이진트리 복습

2021. 4. 1. 10:52C#/자료구조

public class BinaryTreeNode
    {
        public string data;
        public BinaryTreeNode left;
        public BinaryTreeNode right;

        public BinaryTreeNode(string data) {
            this.data = data;
        }
    }
public class BinaryTree
    {
        public BinaryTreeNode root;
        public BinaryTree(string data) 
        {
            this.root = new BinaryTreeNode(data);
        }
        public void PrintPreorderTraversal() 
        {
            this.PreorderTraversalImple(this.root);
        }
        private void PreorderTraversalImple(BinaryTreeNode node) 
        {
            if (node == null) return;
            Console.WriteLine("{0}", node.data);
            this.PreorderTraversalImple(node.left);
            this.PreorderTraversalImple(node.right);
            
        }
    }
public class App
    {
        public 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.PrintPreorderTraversal();
        }
    }