1 31 package org.objectweb.proactive.examples.binarytree; 32 33 import org.apache.log4j.Logger; 34 35 38 public class BinaryTree { 39 40 static Logger logger = Logger.getLogger(BinaryTree.class.getName()); 41 42 protected int key; protected Object value; 45 protected boolean isLeaf; 48 protected BinaryTree leftTree; 50 protected BinaryTree rightTree; 51 52 53 56 public BinaryTree() { 57 this.isLeaf = true; } 59 60 61 65 public void put(int key, Object value) { 66 if (this.isLeaf) { 68 this.key = key; 69 this.value = value; 70 this.isLeaf = false; 71 this.createChildren(); 72 } else if (key == this.key) { 73 this.value = value; 75 } else if (key < this.key) { 76 this.leftTree.put(key, value); 78 } else { 79 this.rightTree.put(key, value); 81 } 82 } 83 84 85 public ObjectWrapper get(int key) { 86 if (this.isLeaf) { 88 return new ObjectWrapper ("null"); 91 } 92 if (key == this.key) { 93 return new ObjectWrapper(this.value); 95 } 96 if (key < this.key) { 97 ObjectWrapper res = this.leftTree.get(key); 99 return res; 100 } 101 ObjectWrapper res = this.rightTree.get(key); 103 return res; 104 } 105 106 107 110 protected void createChildren() { 111 this.leftTree = new BinaryTree(); 112 this.rightTree = new BinaryTree(); 113 } 114 } 115 | Popular Tags |