Advertisement
vencinachev

BinaryTree

Sep 8th, 2019
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.92 KB | None | 0 0
  1. class BinTree<T>
  2.     {
  3.         private BinTreeNode<T> root;
  4.  
  5.         public BinTreeNode<T> Root
  6.         {
  7.             get
  8.             {
  9.                 return this.root;
  10.             }
  11.         }
  12.  
  13.         public BinTree(T value, BinTree<T> leftChild, BinTree<T> rightChild)
  14.         {
  15.             BinTreeNode<T> leftChildNode = leftChild != null ? leftChild.root : null;
  16.             BinTreeNode<T> rightChildNode = rightChild != null ? rightChild.root : null;
  17.             this.root = new BinTreeNode<T>(value, leftChildNode, rightChildNode);
  18.         }
  19.  
  20.         public BinTree(T value) : this(value, null, null)
  21.         {
  22.         }
  23.  
  24.         private void PrintInorder(BinTreeNode<T> root)
  25.         {
  26.             if (root == null)
  27.             {
  28.                 return;
  29.             }
  30.  
  31.             PrintInorder(root.LeftChild);
  32.             Console.Write(root.Value + " ");
  33.             PrintInorder(root.RightChild);
  34.         }
  35.  
  36.         public void PrintInorder()
  37.         {
  38.             PrintInorder(this.Root);
  39.             Console.WriteLine();
  40.         }
  41.  
  42.         private void PrintPreorder(BinTreeNode<T> root)
  43.         {
  44.             if (root == null)
  45.             {
  46.                 return;
  47.             }
  48.             Console.Write(root.Value + " ");
  49.             PrintPreorder(root.LeftChild);
  50.             PrintPreorder(root.RightChild);
  51.         }
  52.  
  53.         public void PrintPreorder()
  54.         {
  55.             PrintPreorder(this.root);
  56.             Console.WriteLine();
  57.         }
  58.  
  59.         private void PrintPostorder(BinTreeNode<T> root)
  60.         {
  61.             if (root == null)
  62.             {
  63.                 return;
  64.             }
  65.             PrintPostorder(root.LeftChild);
  66.             PrintPostorder(root.RightChild);
  67.             Console.Write(root.Value + " ");
  68.         }
  69.  
  70.         public void PrintPostorder()
  71.         {
  72.             PrintPostorder(this.root);
  73.             Console.WriteLine();
  74.         }
  75.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement