fork download
  1. // The classes are basically the same as the BinaryTree
  2. // file except the visitor classes and the accept method
  3. // in the Tree class
  4.  
  5. class TreeVisitor{
  6. public static void main(String[] a){
  7. System.out.println(new TV().Start());
  8. }
  9. }
  10.  
  11. class TV {
  12.  
  13. public int Start(){
  14. Tree root ;
  15. boolean ntb ;
  16. int nti ;
  17. MyVisitor v ;
  18.  
  19. root = new Tree();
  20. ntb = root.Init(16);
  21. ntb = root.Print();
  22. System.out.println(100000000);
  23. ntb = root.Insert(8) ;
  24. ntb = root.Insert(24) ;
  25. ntb = root.Insert(4) ;
  26. ntb = root.Insert(12) ;
  27. ntb = root.Insert(20) ;
  28. ntb = root.Insert(28) ;
  29. ntb = root.Insert(14) ;
  30. ntb = root.Print();
  31. System.out.println(100000000);
  32. v = new MyVisitor();
  33. System.out.println(50000000);
  34. nti = root.accept(v);
  35. System.out.println(100000000);
  36. System.out.println(root.Search(24));
  37. System.out.println(root.Search(12));
  38. System.out.println(root.Search(16));
  39. System.out.println(root.Search(50));
  40. System.out.println(root.Search(12));
  41. ntb = root.Delete(12);
  42. ntb = root.Print();
  43. System.out.println(root.Search(12));
  44.  
  45. return 0 ;
  46. }
  47.  
  48. }
  49.  
  50.  
  51. class Tree{
  52. Tree left ;
  53. Tree right;
  54. int key ;
  55. boolean has_left ;
  56. boolean has_right ;
  57. Tree my_null ;
  58.  
  59.  
  60.  
  61. //Tree new_node ;
  62. //Tree current_node ;
  63. //Tree parent_node ;
  64.  
  65. // boolean ntb ;
  66. //boolean cont ;
  67. //boolean found ;
  68. //int ifound ;
  69. // boolean is_root ;
  70. // int nti ;
  71. // int key_aux ;
  72. // int auxkey1 ;
  73. // int auxkey2 ;
  74.  
  75. public boolean Init(int v_key){
  76. key = v_key ;
  77. has_left = false ;
  78. has_right = false ;
  79. return true ;
  80. }
  81.  
  82. public boolean SetRight(Tree rn){
  83. right = rn ;
  84. return true ;
  85. }
  86.  
  87. public boolean SetLeft(Tree ln){
  88. left = ln ;
  89. return true ;
  90. }
  91.  
  92. public Tree GetRight(){
  93. return right ;
  94. }
  95.  
  96. public Tree GetLeft(){
  97. return left;
  98. }
  99.  
  100. public int GetKey(){
  101. return key ;
  102. }
  103.  
  104. public boolean SetKey(int v_key){
  105. key = v_key ;
  106. return true ;
  107. }
  108.  
  109. public boolean GetHas_Right(){
  110. return has_right ;
  111. }
  112.  
  113. public boolean GetHas_Left(){
  114. return has_left ;
  115. }
  116.  
  117. public boolean SetHas_Left(boolean val){
  118. has_left = val ;
  119. return true ;
  120. }
  121.  
  122. public boolean SetHas_Right(boolean val){
  123. has_right = val ;
  124. return true ;
  125. }
  126.  
  127. public boolean Compare(int num1 , int num2){
  128. boolean ntb ;
  129. int nti ;
  130.  
  131. ntb = false ;
  132. nti = num2 + 1 ;
  133. if (num1 < num2) ntb = false ;
  134. else if (!(num1 < nti)) ntb = false ;
  135. else ntb = true ;
  136. return ntb ;
  137. }
  138.  
  139. public boolean Insert(int v_key){
  140. Tree new_node ;
  141. boolean ntb ;
  142. Tree current_node ;
  143. boolean cont ;
  144. int key_aux ;
  145.  
  146. new_node = new Tree();
  147. ntb = new_node.Init(v_key) ;
  148. current_node = this ;
  149. cont = true ;
  150. while (cont){
  151. key_aux = current_node.GetKey();
  152. if (v_key < key_aux){
  153. if (current_node.GetHas_Left())
  154. current_node = current_node.GetLeft() ;
  155. else {
  156. cont = false ;
  157. ntb = current_node.SetHas_Left(true);
  158. ntb = current_node.SetLeft(new_node);
  159. }
  160. }
  161. else{
  162. if (current_node.GetHas_Right())
  163. current_node = current_node.GetRight() ;
  164. else {
  165. cont = false ;
  166. ntb = current_node.SetHas_Right(true);
  167. ntb = current_node.SetRight(new_node);
  168. }
  169. }
  170. }
  171. return true ;
  172. }
  173.  
  174. public boolean Delete(int v_key){
  175. Tree current_node ;
  176. Tree parent_node ;
  177. boolean cont ;
  178. boolean found ;
  179. boolean ntb ;
  180. boolean is_root ;
  181. int key_aux ;
  182.  
  183. current_node = this ;
  184. parent_node = this ;
  185. cont = true ;
  186. found = false ;
  187. is_root = true ;
  188. while (cont){
  189. key_aux = current_node.GetKey();
  190. if (v_key < key_aux)
  191. if (current_node.GetHas_Left()){
  192. parent_node = current_node ;
  193. current_node = current_node.GetLeft() ;
  194. }
  195. else cont = false ;
  196. else
  197. if (key_aux < v_key)
  198. if (current_node.GetHas_Right()){
  199. parent_node = current_node ;
  200. current_node = current_node.GetRight() ;
  201. }
  202. else cont = false ;
  203. else {
  204. if (is_root)
  205. if (!(current_node.GetHas_Right()) &&
  206. !(current_node.GetHas_Left()) )
  207. ntb = true ;
  208. else
  209. ntb = this.Remove(parent_node,current_node);
  210. else ntb = this.Remove(parent_node,current_node);
  211. found = true ;
  212. cont = false ;
  213. }
  214. is_root = false ;
  215. }
  216. return found ;
  217. }
  218.  
  219. public boolean Remove(Tree p_node, Tree c_node){
  220. boolean ntb ;
  221. int auxkey1 ;
  222. int auxkey2 ;
  223.  
  224. if (c_node.GetHas_Left())
  225. ntb = this.RemoveLeft(p_node,c_node) ;
  226. else
  227. if (c_node.GetHas_Right())
  228. ntb = this.RemoveRight(p_node,c_node) ;
  229. else {
  230. auxkey1 = c_node.GetKey();
  231. auxkey2 = (p_node.GetLeft()).GetKey() ;
  232. if (this.Compare(auxkey1,auxkey2)) {
  233. ntb = p_node.SetLeft(my_null);
  234. ntb = p_node.SetHas_Left(false);
  235. }
  236. else {
  237. ntb = p_node.SetRight(my_null);
  238. ntb = p_node.SetHas_Right(false);
  239. }
  240. }
  241. return true ;
  242. }
  243.  
  244. public boolean RemoveRight(Tree p_node, Tree c_node){
  245. boolean ntb ;
  246. while (c_node.GetHas_Right()){
  247. ntb = c_node.SetKey((c_node.GetRight()).GetKey());
  248. p_node = c_node ;
  249. c_node = c_node.GetRight() ;
  250. }
  251. ntb = p_node.SetRight(my_null);
  252. ntb = p_node.SetHas_Right(false);
  253. return true ;
  254. }
  255.  
  256. public boolean RemoveLeft(Tree p_node, Tree c_node){
  257. boolean ntb ;
  258. while (c_node.GetHas_Left()){
  259. ntb = c_node.SetKey((c_node.GetLeft()).GetKey());
  260. p_node = c_node ;
  261. c_node = c_node.GetLeft() ;
  262. }
  263. ntb = p_node.SetLeft(my_null);
  264. ntb = p_node.SetHas_Left(false);
  265. return true ;
  266. }
  267.  
  268.  
  269. public int Search(int v_key){
  270. Tree current_node ;
  271. int ifound ;
  272. boolean cont ;
  273. int key_aux ;
  274.  
  275. current_node = this ;
  276. cont = true ;
  277. ifound = 0 ;
  278. while (cont){
  279. key_aux = current_node.GetKey();
  280. if (v_key < key_aux)
  281. if (current_node.GetHas_Left())
  282. current_node = current_node.GetLeft() ;
  283. else cont = false ;
  284. else
  285. if (key_aux < v_key)
  286. if (current_node.GetHas_Right())
  287. current_node = current_node.GetRight() ;
  288. else cont = false ;
  289. else {
  290. ifound = 1 ;
  291. cont = false ;
  292. }
  293. }
  294. return ifound ;
  295. }
  296.  
  297. public boolean Print(){
  298. boolean ntb ;
  299. Tree current_node ;
  300.  
  301. current_node = this ;
  302. ntb = this.RecPrint(current_node);
  303. return true ;
  304. }
  305.  
  306. public boolean RecPrint(Tree node){
  307. boolean ntb ;
  308.  
  309. if (node.GetHas_Left()){
  310. ntb = this.RecPrint(node.GetLeft());
  311. } else ntb = true ;
  312. System.out.println(node.GetKey());
  313. if (node.GetHas_Right()){
  314. ntb = this.RecPrint(node.GetRight());
  315. } else ntb = true ;
  316. return true ;
  317. }
  318.  
  319. public int accept(Visitor v){
  320. int nti ;
  321.  
  322. System.out.println(333);
  323. nti = v.visit(this) ;
  324. return 0 ;
  325. }
  326.  
  327. }
  328.  
  329.  
  330.  
  331. class Visitor {
  332. Tree l ;
  333. Tree r ;
  334.  
  335. public int visit(Tree n){
  336. int nti ;
  337.  
  338. if (n.GetHas_Right()){
  339. r = n.GetRight() ;
  340. nti = r.accept(this) ; }
  341. else nti = 0 ;
  342.  
  343. if (n.GetHas_Left()) {
  344. l = n.GetLeft();
  345. nti = l.accept(this) ; }
  346. else nti = 0 ;
  347.  
  348. return 0;
  349. }
  350.  
  351. }
  352.  
  353.  
  354. class MyVisitor extends Visitor {
  355.  
  356. public int visit(Tree n){
  357. int nti ;
  358.  
  359. if (n.GetHas_Right()){
  360. r = n.GetRight() ;
  361. nti = r.accept(this) ; }
  362. else nti = 0 ;
  363.  
  364. System.out.println(n.GetKey());
  365.  
  366. if (n.GetHas_Left()) {
  367. l = n.GetLeft();
  368. nti =l.accept(this) ; }
  369. else nti = 0 ;
  370.  
  371. return 0;
  372. }
  373.  
  374. }
  375.  
Success #stdin #stdout 0.06s 32576KB
stdin
Standard input is empty
stdout
16
100000000
4
8
12
14
16
20
24
28
100000000
50000000
333
333
333
28
24
333
20
16
333
333
333
14
12
8
333
4
100000000
1
1
1
0
1
4
8
14
16
20
24
28
0
0