Advertisement
makispaiktis

Trees and Nodes

Jul 15th, 2020 (edited)
1,158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.20 KB | None | 0 0
  1. public class Node {
  2.    
  3.     // Variables
  4.     double value;
  5.     String nodeName;
  6.     Node parent;
  7.     Node leftChild;
  8.     Node rightChild;
  9.    
  10.     // Constructors (6 different constructors depending on the arguments)
  11.     Node(){
  12.         value = 0;
  13.         nodeName = "";
  14.         parent = null;
  15.         leftChild = null;
  16.         rightChild = null;
  17.     }
  18.    
  19.     Node(double value){
  20.         this.value = value;
  21.         this.nodeName = "";
  22.         this.parent = null;
  23.         this.leftChild = null;
  24.         this.rightChild = null;
  25.     }
  26.    
  27.     Node(double value, String nodeName){
  28.         this.value = value;
  29.         this.nodeName = nodeName;
  30.         this.parent = null;
  31.         this.leftChild = null;
  32.         this.rightChild = null;
  33.     }
  34.    
  35.     Node(double value, String nodeName, Node parent){
  36.         this.value = value;
  37.         this.nodeName = nodeName;
  38.         this.parent = parent;
  39.         this.leftChild = null;
  40.         this.rightChild = null;
  41.     }
  42.    
  43.     Node(double value, String nodeName, Node parent, Node leftChild){
  44.         this.value = value;
  45.         this.nodeName = nodeName;
  46.         this.parent = parent;
  47.         this.leftChild = leftChild;
  48.         this.rightChild = null;
  49.     }
  50.    
  51.     Node(double value, String nodeName, Node parent, Node leftChild, Node rightChild){
  52.         this.value = value;
  53.         this.nodeName = nodeName;
  54.         this.parent = parent;
  55.         this.leftChild = leftChild;
  56.         this.rightChild = rightChild;
  57.     }
  58.    
  59.     // Mehods
  60.     public void showSatus() {
  61.         System.out.println("Node's name:  " + nodeName);
  62.         System.out.println("Node's value: " + value);
  63.         if(this.parent != null) {
  64.             System.out.println("Parent: (" + parent.nodeName + ", " + parent.value + ")");
  65.         }
  66.         if(this.leftChild != null) {
  67.             System.out.println("LeftChild: (" + leftChild.nodeName + ", " + leftChild.value + ")");
  68.         }
  69.         if(this.rightChild != null) {
  70.             System.out.println("RightChild: (" + rightChild.nodeName + ", " + rightChild.value + ")");
  71.         }
  72.     }
  73.    
  74.     public static Node makeRightChild(Node node) {
  75.         Node right = new Node();
  76.         node.rightChild = right;
  77.         right.parent = node;
  78.         return right;
  79.     }
  80.    
  81.     public static Node makeLeftChild(Node node) {
  82.         Node left = new Node();
  83.         node.leftChild = left;
  84.         left.parent = node;
  85.         return left;
  86.     }
  87.    
  88.     public static Node makeRightChild(Node node, double value, String childName) {
  89.         Node right = new Node(value, childName);
  90.         node.rightChild = right;
  91.         right.parent = node;
  92.         return right;
  93.     }
  94.    
  95.     public static Node makeLeftChild(Node node, double value, String childName) {
  96.         Node left = new Node(value, childName);
  97.         node.leftChild = left;
  98.         left.parent = node;
  99.         return left;
  100.     }
  101.    
  102.     public static Node createRoot(double rootValue, String rootName) {
  103.         return new Node(rootValue, rootName);
  104.     }
  105.    
  106.    
  107.     // ***********************************************************************************************
  108.     // ***********************************************************************************************
  109.     // ***********************************************************************************************
  110.     // ***********************************************************************************************
  111.    
  112.     // Main Function
  113.     public static void main(String[] args) {
  114.         Node root = createRoot(10, "root");
  115.         root.showSatus();
  116.         Node left = makeLeftChild(root, 5, "LEFT");
  117.         left.showSatus();
  118.         Node right = makeRightChild(root);
  119.         right.showSatus();
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement