All pastes #2086363 Raw Edit

Someone

public text v1 · immutable
#2086363 ·published 2011-10-03 01:20 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MyBST {
    class Tree {
        private Node root;

        public Tree() {
            this.root = null;
        }



        public void add(int value) {

            if (root == null) {
                root = new Node(value, null, null); ;
                return;
            }
            
            Node current = root;

            while (current != null){
                if (value == current.getValue()) {
                    return;
                }else if (value < current.getValue()){
                    if (current.getLeft() == null) {
                        current.setLeft(new Node(value, null, null));
                        return;
                    } else {
                        current = current.getLeft();
                    }

                }else{
                    if (current.getRight() == null) {
                        current.setRight(new Node(value, null, null));
                        return;
                    } else {
                        current = current.getRight();
                    }  
     
                }
            }
            
        
        }



        public void print(int choice){
            if(this.root == null){
                Console.WriteLine("Tree is empty");
                return;
            }else{
                //switch choice, or use enum...

                if (choice == 0){
                    Console.WriteLine("Printing Tree:");
                    printInOrder(root);
                }
            }
        }


        private void printInOrder(Node n) {
            if (n == null) {
                return;
            } else {
                Console.WriteLine(root.getValue());
                printInOrder(n.getLeft());
                printInOrder(n.getRight());
                //return;
            }
        }
            

    }
}