package com.java.ds.bst; import java.util.Comparator; public class BinarySearchTree> { Node root; Comparator compr; public BinarySearchTree() { } public BinarySearchTree(Comparator compr) { this.compr = compr; } public void add(T t) { if (root == null) { root = new Node(t); } else { add(root, t); } } private void add(Node root, T t) { int compare; if (compr != null) { compare = compr.compare(root.value, t); } else { compare = t.compareTo(root.value); } if (compare > 0) {// right if (root.getRight() == null) { Node node = new Node(t); root.setRight(node); } else { add(root.getRight(), t); } } else if (compare < 0) { // left if (root.getLeft() == null) { Node node = new Node(t); root.setLeft(node); } else { add(root.getLeft(), t); } } } public void iterateAsc(){ travelTreeAsc(root); } private void travelTreeAsc(Node root) { if (root != null) { travelTreeAsc(root.getLeft()); System.out.print(root.getValue() + "-"); travelTreeAsc(root.getRight()); } } private static class Node> { private T value; private Node left; private Node right; public Node(T t) { this.value = t; } public void setLeft(Node left) { this.left = left; } public void setRight(Node right) { this.right = right; } public T getValue() { return value; } public Node getLeft() { return left; } public Node getRight() { return right; } } }