Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Node {
- // Variables
- double value;
- String nodeName;
- Node parent;
- Node leftChild;
- Node rightChild;
- // Constructors (6 different constructors depending on the arguments)
- Node(){
- value = 0;
- nodeName = "";
- parent = null;
- leftChild = null;
- rightChild = null;
- }
- Node(double value){
- this.value = value;
- this.nodeName = "";
- this.parent = null;
- this.leftChild = null;
- this.rightChild = null;
- }
- Node(double value, String nodeName){
- this.value = value;
- this.nodeName = nodeName;
- this.parent = null;
- this.leftChild = null;
- this.rightChild = null;
- }
- Node(double value, String nodeName, Node parent){
- this.value = value;
- this.nodeName = nodeName;
- this.parent = parent;
- this.leftChild = null;
- this.rightChild = null;
- }
- Node(double value, String nodeName, Node parent, Node leftChild){
- this.value = value;
- this.nodeName = nodeName;
- this.parent = parent;
- this.leftChild = leftChild;
- this.rightChild = null;
- }
- Node(double value, String nodeName, Node parent, Node leftChild, Node rightChild){
- this.value = value;
- this.nodeName = nodeName;
- this.parent = parent;
- this.leftChild = leftChild;
- this.rightChild = rightChild;
- }
- // Mehods
- public void showSatus() {
- System.out.println("Node's name: " + nodeName);
- System.out.println("Node's value: " + value);
- if(this.parent != null) {
- System.out.println("Parent: (" + parent.nodeName + ", " + parent.value + ")");
- }
- if(this.leftChild != null) {
- System.out.println("LeftChild: (" + leftChild.nodeName + ", " + leftChild.value + ")");
- }
- if(this.rightChild != null) {
- System.out.println("RightChild: (" + rightChild.nodeName + ", " + rightChild.value + ")");
- }
- }
- public static Node makeRightChild(Node node) {
- Node right = new Node();
- node.rightChild = right;
- right.parent = node;
- return right;
- }
- public static Node makeLeftChild(Node node) {
- Node left = new Node();
- node.leftChild = left;
- left.parent = node;
- return left;
- }
- public static Node makeRightChild(Node node, double value, String childName) {
- Node right = new Node(value, childName);
- node.rightChild = right;
- right.parent = node;
- return right;
- }
- public static Node makeLeftChild(Node node, double value, String childName) {
- Node left = new Node(value, childName);
- node.leftChild = left;
- left.parent = node;
- return left;
- }
- public static Node createRoot(double rootValue, String rootName) {
- return new Node(rootValue, rootName);
- }
- // ***********************************************************************************************
- // ***********************************************************************************************
- // ***********************************************************************************************
- // ***********************************************************************************************
- // Main Function
- public static void main(String[] args) {
- Node root = createRoot(10, "root");
- root.showSatus();
- Node left = makeLeftChild(root, 5, "LEFT");
- left.showSatus();
- Node right = makeRightChild(root);
- right.showSatus();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement