Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApp1
- {
- class Node
- {
- public int val { get; set; }
- public Node left;
- public Node right;
- public Node(int d)
- {
- this.val = d;
- left = null;
- right = null;
- }
- public static void Insert(ref Node root, int val)
- {
- if (root == null) { root = new Node(val); return; }
- if (root.val == val) { return; }
- else if (val < root.val) { Insert(ref root.left, val); }
- else { Insert(ref root.right, val); }
- }
- public static void Traverse(Node root)
- {
- if (root is null) { return; }
- Traverse(root.left);
- Console.Write($"{root.val} ");
- Traverse(root.right);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Random random = new Random(Guid.NewGuid().GetHashCode());
- Node root = null;
- for (int i = 0; i < 15; i++)
- {
- Node.Insert(ref root, random.Next(0, 55));
- }
- Node.Traverse(root);
- Console.WriteLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment