Advertisement
haquaa

Untitled

Jan 4th, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Tree
  7. {
  8.     class Node
  9.     {
  10.         public int info;
  11.         public Node left;
  12.         public Node right;
  13.  
  14.         public Node(int val)
  15.         {
  16.             info = val;
  17.             left = right = null;
  18.         }
  19.  
  20.         public void insertNode(int val)
  21.         {
  22.             if (val > info)
  23.             {
  24.                 if (right == null)
  25.                 {
  26.                     right = new Node(val);
  27.                 }
  28.                 else
  29.                 {
  30.                     right.insertNode(val);
  31.                 }
  32.             }
  33.             else
  34.             {
  35.                 if (left == null)
  36.                 {
  37.                     left = new Node(val);
  38.                 }
  39.                 else
  40.                 {
  41.                     left.insertNode(val);
  42.                 }
  43.             }
  44.         }
  45.  
  46.  
  47.     }
  48.  
  49.  
  50. class Tree
  51.     {
  52.         Node root = null;
  53.  
  54.         public void insert(int val)
  55.         {
  56.             Node tmp = new Node(val);
  57.  
  58.             if (root == null)
  59.             {
  60.                 root = tmp;
  61.                 return;
  62.             }
  63.  
  64.             root.insertNode(val);
  65.         }
  66.  
  67.         public void PreOrder(Node x)
  68.         {
  69.  
  70.             if (x == null) return;
  71.  
  72.             PreOrder(x.left);
  73.  
  74.             Console.WriteLine(x.info);
  75.  
  76.             PreOrder(x.right);
  77.  
  78.         }
  79.  
  80.         public void print()
  81.         {
  82.             PreOrder(root);
  83.         }
  84.  
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement