@@ -16,21 +16,21 @@ public static void main(String[] args) {
1616 tree .inorder ();
1717 }
1818
19- public boolean insert (int val ) {
20- if (root == null ) {
21- root = new Node (val );
22- return true ;
23- }
24- else
25- return root .insert (val );
26- }
27-
28- public boolean find (int val ) {
29- if (root == null )
30- return false ;
31- else
32- return root .find (val );
33- }
19+ public boolean insert (int val ) {
20+ if (root == null ) {
21+ root = new Node (val );
22+ return true ;
23+ }
24+ else
25+ return root .insert (val );
26+ }
27+
28+ public boolean find (int val ) {
29+ if (root == null )
30+ return false ;
31+ else
32+ return root .find (val );
33+ }
3434
3535 public int getHeight () {
3636 if (root != null )
@@ -46,80 +46,80 @@ public int getSize() {
4646 return 0 ;
4747 }
4848
49- public void preorder () {
50- if (root != null ) {
51- System .out .println ("Preorder:" );
52- root .preorder ();
53- }
54- }
55-
56- public void postorder () {
57- if (root != null ) {
58- System .out .println ("Postorder:" );
59- root .postorder ();
60- }
61- }
62-
63- public void inorder () {
64- if (root != null ) {
65- System .out .println ("Inorder:" );
66- root .inorder ();
67- }
68- }
49+ public void preorder () {
50+ if (root != null ) {
51+ System .out .println ("Preorder:" );
52+ root .preorder ();
53+ }
54+ }
55+
56+ public void postorder () {
57+ if (root != null ) {
58+ System .out .println ("Postorder:" );
59+ root .postorder ();
60+ }
61+ }
62+
63+ public void inorder () {
64+ if (root != null ) {
65+ System .out .println ("Inorder:" );
66+ root .inorder ();
67+ }
68+ }
6969
70- private class Node {
71- private Node leftChild ;
72- private Node rightChild ;
73- private int data ;
70+ private class Node {
71+ private Node leftChild ;
72+ private Node rightChild ;
73+ private int data ;
7474
75- private Node (int val ) {
76- data = val ;
75+ private Node (int val ) {
76+ data = val ;
7777 leftChild = null ;
7878 rightChild = null ;
79- }
79+ }
8080
81- private boolean insert (int val ) {
82- boolean added = false ;
83- if (this == null ) {
84- this .data = val ;
85- return true ;
86- }
87- else {
88- if (val < this .data ) {
89- if (this .leftChild == null ) {
90- this .leftChild = new Node (val );
91- return true ;
92- }
93- else
94- added = this .leftChild .insert (val );
95- }
96- else if (val > this .data ) {
97- if (this .rightChild == null ) {
98- this .rightChild = new Node (val );
99- return true ;
100- }
101- else
102- added = this .rightChild .insert (val );
103- }
104- }
105- return added ;
106- }
107-
108- private boolean find (int val ) {
109- boolean found = false ;
110- if (this == null )
111- return false ;
112- else {
113- if (val == this .data )
114- return true ;
115- else if (val < this .data && this .leftChild != null )
116- found = this .leftChild .find (val );
117- else if (val > this .data && this .rightChild != null )
118- found = this .rightChild .find (val );
119- }
120- return found ;
121- }
122-
81+ private boolean insert (int val ) {
82+ boolean added = false ;
83+ if (this == null ) {
84+ this .data = val ;
85+ return true ;
86+ }
87+ else {
88+ if (val < this .data ) {
89+ if (this .leftChild == null ) {
90+ this .leftChild = new Node (val );
91+ return true ;
92+ }
93+ else
94+ added = this .leftChild .insert (val );
95+ }
96+ else if (val > this .data ) {
97+ if (this .rightChild == null ) {
98+ this .rightChild = new Node (val );
99+ return true ;
100+ }
101+ else
102+ added = this .rightChild .insert (val );
103+ }
104+ }
105+ return added ;
106+ }
107+
108+ private boolean find (int val ) {
109+ boolean found = false ;
110+ if (this == null )
111+ return false ;
112+ else {
113+ if (val == this .data )
114+ return true ;
115+ else if (val < this .data && this .leftChild != null )
116+ found = this .leftChild .find (val );
117+ else if (val > this .data && this .rightChild != null )
118+ found = this .rightChild .find (val );
119+ }
120+ return found ;
121+ }
122+
123123 private int getHeight () {
124124 int leftHeight = 0 , rightHeight = 0 ;
125125
@@ -144,34 +144,34 @@ private int getSize() {
144144 return 1 + leftSize + rightSize ;
145145 }
146146
147- private void preorder () {
148- if (this != null ) {
149- System .out .println (this .data );
150- if (this .leftChild != null )
151- this .leftChild .preorder ();
152- if (this .rightChild != null )
153- this .rightChild .preorder ();
154- }
155- }
156-
157- private void postorder () {
158- if (this != null ) {
159- if (this .leftChild != null )
160- this .leftChild .postorder ();
161- if (this .rightChild != null )
162- this .rightChild .postorder ();
163- System .out .println (this .data );
164- }
165- }
166-
167- private void inorder () {
168- if (this != null ) {
169- if (this .leftChild != null )
170- this .leftChild .inorder ();
171- System .out .println (this .data );
172- if (this .rightChild != null )
173- this .rightChild .inorder ();
174- }
175- }
176- }
147+ private void preorder () {
148+ if (this != null ) {
149+ System .out .println (this .data );
150+ if (this .leftChild != null )
151+ this .leftChild .preorder ();
152+ if (this .rightChild != null )
153+ this .rightChild .preorder ();
154+ }
155+ }
156+
157+ private void postorder () {
158+ if (this != null ) {
159+ if (this .leftChild != null )
160+ this .leftChild .postorder ();
161+ if (this .rightChild != null )
162+ this .rightChild .postorder ();
163+ System .out .println (this .data );
164+ }
165+ }
166+
167+ private void inorder () {
168+ if (this != null ) {
169+ if (this .leftChild != null )
170+ this .leftChild .inorder ();
171+ System .out .println (this .data );
172+ if (this .rightChild != null )
173+ this .rightChild .inorder ();
174+ }
175+ }
176+ }
177177}
0 commit comments