I have learnt binary tree in Mathematics Computing last two semester in universities. And after I have completed the double module of Java, I know a little knowledge of Object-oriented Programming, so as I have tried to build a Java program which using object-oriented concept to construct a binary tree in Java.
This is my source code which written by me, fyhao.
- class Node {
- public Node parentElement; // parent element of the node
- public Node leftElement; // left child element of the node
- public Node rightElement; // right child element of the node
- public String value; // the value of the node
- }
- public class TestNode {
- public static void main(String args[]) {
- //String sentence = “The fox is jump over that fence”;
- String sentence = “The three fox are jump over that fence”;
- // Break each word into array
- String word[] = sentence.split(” “);
- // Initialize the main tree
- Node node = new Node();
- // SetTree
- for(int i=0; i<WORD.LENGTH; {<br i++) />
- if(i == 0) {
- node.value = word[i];
- node.parentElement = null;
- } else
- setNode(node, word[i]);
- }
- // GetTree
- System.out.println(“Demonstration of Mathematic for Computing: Tree”);
- System.out.println(“Sentence: ” + sentence);
- System.out.println(“Height of tree: ” + getNodeHeight(node));
- System.out.println(“Leaf: ” + getLeaf(node));
- System.out.println(“Parent of jump: ” + getParent(node, getNode(node, “jump”)).value);
- System.out.println(“Parent of fence: ” + getParent(node, getNode(node, “fence”)).value);
- System.out.println(“Height of sub-tree on fox: ” + getNodeHeight(getNode(node, “fox”)));
- }
- // Set the structure of the tree on each node
- // @param Tree of node, word (value)
- public static void setNode(Node node, String word) {
- Node childNode;
- if(node.value != null) {
- childNode = new Node();
- childNode.parentElement = node;
- childNode.value = word;
- // compare ignore case
- word = word.toLowerCase();
- node.value = node.value.toLowerCase();
- if(word.compareTo(node.value) == 0)
- return; // if equal, break out of method
- else if(word.compareTo(node.value) < 0) {
- if(node.leftElement != null)
- setNode(node.leftElement, word);
- else
- node.leftElement = childNode;
- } else {
- if(node.rightElement != null)
- setNode(node.rightElement, word);
- else
- node.rightElement = childNode;
- }
- }
- }
- // Get node by value
- // @param Tree of node, value
- // @return the node want to get
- public static Node getNode(Node node, String value) {
- Node targetNode = null;
- if(value.equals(node.value))
- targetNode = node;
- else {
- // check if left or right got target, then only one target found only, strange problem, getParent also encountered, maybe not very good
- if(node.leftElement != null)
- targetNode = targetNode == null ? getNode(node.leftElement, value) : targetNode;
- if(node.rightElement != null)
- targetNode = targetNode == null ? getNode(node.rightElement, value) : targetNode;
- }
- return targetNode;
- }
- // Get height of the tree
- // @param Tree node
- // @return height of tree
- public static int getNodeHeight(Node node) {
- int height = 1;
- int leftHeight = 0, rightHeight = 0;
- if(node.leftElement == null && node.rightElement == null)
- return height;
- else {
- if(node.leftElement != null)
- leftHeight += getNodeHeight(node.leftElement);
- if(node.rightElement != null)
- rightHeight += getNodeHeight(node.rightElement);
- }
- return height + Math.max(leftHeight, rightHeight);
- }
- // Get number of leafs in the tree
- // @param Tree node
- // @return number of leaf
- public static int getLeaf(Node node) {
- int leaf = 0;
- if(node.leftElement == null && node.rightElement == null)
- return 1;
- else {
- if(node.leftElement != null)
- leaf += getLeaf(node.leftElement);
- if(node.rightElement != null)
- &
nbsp; leaf += getLeaf(node.rightElement); - }
- return leaf;
- }
- // Get parent of node by value
- // @param Tree of node, child of node
- // @return the parent of node
- public static Node getParent(Node node, Node childNode) {
- // found that node
- Node parentNode = null;
- if(node.value.equals(childNode.value))
- return node.parentElement;
- else {
- if(node.leftElement != null)
- parentNode = parentNode == null ? getParent(node.leftElement, childNode) : parentNode;
- if(node.rightElement != null)
- parentNode = parentNode == null ? getParent(node.rightElement, childNode) : parentNode;
- }
- return parentNode;
- }
- }