In any case, here is the Java code for implementation of the tree class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tree { | |
private static class Node { | |
int data; | |
Node left; | |
Node right; | |
/** Node constructor that adds the data | |
* | |
* @param data to be inserted | |
*/ | |
Node (int data) { | |
data = data; | |
left = null; | |
right = null; | |
} | |
} | |
private Node root; | |
Tree () { | |
root = null; | |
} | |
/** insert a new data into the tree incapsulation method | |
* | |
* @param data to be inserted | |
*/ | |
public void insert (int data) { | |
root = insert(root, data); | |
} | |
/** insert data actual implementation method | |
* in this version of binary tree, the data will be appended | |
* to the right tree if it is equal | |
* | |
* @param node is the current node | |
* @param data to be inserted | |
* @return the node in which data is inserted | |
*/ | |
private Node insert (Node node, int data) { | |
if (node == null) | |
node = new Node (data); | |
else if (data < node.data) | |
node.left = insert (node.left, data); | |
else // if (data >= node.data) | |
node.right = insert (node.right, data); | |
return node; | |
} | |
/** lookup data incapsulation method | |
* | |
* @param data to be searched within the tree | |
* @return the total number of nodes that contain the data | |
*/ | |
public int lookup (int data) { | |
return lookup (root, data); | |
} | |
/** lookup data actual implementation method | |
* | |
* @param node is the current node | |
* @param data is the data to be searched | |
* @return the number of nodes containing the data in the | |
* subtree starting from node | |
*/ | |
private int lookup (Node node, int data) { | |
if (node == null) | |
return 0; | |
else if (node.data == data) | |
return lookup (node.right, data) + 1; | |
else if (data < node.data) | |
return lookup (node.left, data); | |
else | |
return lookup (node.right, data); | |
} | |
} |
Any experienced C++ programmers should immediately understand what the code is doing and what each method is doing. For actual implementation of the binary tree, below is the Main class that builds a tree from Tree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Main.java | |
public class Main { | |
/** main method | |
* | |
* @param args is the argument strings passed from the user | |
*/ | |
public static void main(String[] args) { | |
Tree binaryTree = new Tree(); | |
binaryTree.insert(2); | |
binaryTree.insert(1); | |
binaryTree.insert(-2); | |
binaryTree.insert(1); | |
System.out.println("lookup(1) = " + binaryTree.lookup(1)); | |
} | |
} |
No comments:
Post a Comment