Run Code  | API  | Code Wall  | Misc  | Feedback  | Login  | Theme  | Privacy  | Patreon 

BST final

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{

public class Node
{
    public int data;
    public Node left;
    public Node right;
    
    public Node(int value)
    {
        data = value;
        left = null;
        right = null;
    }
}

public class BST
{
    public void insert (ref Node focus, int value)
    {
        if (focus == null)
        {
            focus = new Node(value);
        }
        else if (value < focus.data)
        {
            insert(ref focus.left, value);
        }
        else
        {
            insert(ref focus.right, value);
        }
    }
    
    public void traverse (ref Node focus)
    {
        if (focus == null)
        {
            return;
        }
        
        Console.WriteLine(focus.data);
        
        traverse (ref focus.left);
        traverse (ref focus.right);
    }
}
    
public class Program
{
        public static void Main(string[] args)
        {
            Node root = null;
            BST tree = new BST();
            tree.insert(ref root, 5);
            tree.insert(ref root, 15);
            tree.insert(ref root, 3);
            tree.insert(ref root, 17);
            tree.insert(ref root, 16);
            tree.traverse(ref root);
        }
}

}
 run  | edit  | history  | help 0