1+ package dataStructures ;
2+
3+ /**
4+ * Binary tree for general value type, without redundancy
5+ * @author RICARDO
6+ * @param <T> root data
7+ */
8+ public class BinaryTree <T extends Comparable > {
9+ private final T data ;
10+ private BinaryTree right , // the upper binary tree
11+ left ; // the lower binary tree
12+
13+ public BinaryTree (T data ) {
14+ this .data = data ;
15+ }
16+
17+ @ Override
18+ public String toString (){
19+ return this .data .toString ();
20+ }
21+
22+ /**
23+ * inserts a new value in it's correspondant place
24+ * @param newDataValue value of the new banary tree to add on this tree
25+ */
26+ public void insert (T newDataValue ){
27+ this .insert (new BinaryTree (newDataValue ));
28+ }
29+
30+ /**
31+ * inserts a new binary tree in it's correspondant place
32+ * @param newData new value to add on this tree
33+ */
34+ public void insert (BinaryTree newData ){
35+
36+ int cpr = newData .data .compareTo (this .data ); //new value comparission respect to actual value
37+
38+ if (cpr < 0 )
39+ if (this .left == null )
40+ this .setLeft (newData );
41+ else
42+ this .left .insert (newData );
43+ else if (cpr > 0 )
44+ if (this .right == null )
45+ this .setRight (newData );
46+ else
47+ this .right .insert (newData );
48+ else
49+ System .out .println ("Redundant value, not added" );
50+ }
51+
52+ /**
53+ * search and specific value on the tree
54+ * @param data Searched value
55+ * @return Binary tree wich contains the value, null if it doesn't exist
56+ */
57+ public BinaryTree search (T data ){
58+ int cpr = data .compareTo (this .data ); //new value comparission respect to actual value
59+
60+ if (cpr < 0 ) {
61+ if (this .left == null )
62+ return null ; //the value doesn't exist
63+ return this .left .search (data );
64+ }
65+ if (cpr > 0 ) {
66+ if (this .right == null )
67+ return null ; //the value doesn't exist
68+ return this .right .search (data );
69+ }
70+ return this ;
71+ }
72+
73+ /**
74+ * Checks if the data value exist in the tree
75+ * @param data data to be searched
76+ * @return true if this tree contains the data value, false if not.
77+ */
78+ public boolean contains (T data ){
79+ return !(this .search (data ) == null );
80+ }
81+
82+ /**
83+ * uses recursive black magic to print this tree in console
84+ * @param tabCounter prev tabs
85+ */
86+ private void print (int tabCounter ){
87+ for (int i = 0 ; i < tabCounter ; i ++)
88+ System .out .print ("\t " );
89+
90+ System .out .println (this );
91+
92+ if (this .left !=null )
93+ this .left .print (tabCounter +1 ); //it can't be ++ , pls don't change it
94+ if (this .right !=null )
95+ this .right .print (tabCounter +1 ); //it can't be ++ , pls don't change it
96+ }
97+
98+ /**
99+ * uses black magic to print this tree in console
100+ */
101+ public void print (){
102+ this .print (0 );
103+ }
104+
105+ //getters and setters
106+ public T getData () {
107+ return data ;
108+ }
109+
110+ public BinaryTree getRight () {
111+ return right ;
112+ }
113+
114+ public void setRight (BinaryTree right ) {
115+ this .right = right ;
116+ }
117+
118+ public BinaryTree getLeft () {
119+ return left ;
120+ }
121+
122+ public void setLeft (BinaryTree left ) {
123+ this .left = left ;
124+ }
125+ }
0 commit comments