Advertisement
Egor_Vakar

(Java) lab 5.2 Tree.java

Mar 23rd, 2022
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.83 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. public class Tree {
  4.  
  5.     private int value;
  6.     private Tree leftChild;
  7.     private Tree rightChild;
  8.  
  9.     public static Tree rootNode;
  10.     public static String result = "";
  11.  
  12.     public Tree getRootNode(){
  13.         return rootNode;
  14.     }
  15.  
  16.     public int getValue() {
  17.         return this.value;
  18.     }
  19.  
  20.     public void setValue(final int value) {
  21.         this.value = value;
  22.     }
  23.  
  24.     public Tree getLeftChild() {
  25.         return this.leftChild;
  26.     }
  27.  
  28.     public void setLeftChild(final Tree leftChild) {
  29.         this.leftChild = leftChild;
  30.     }
  31.  
  32.     public Tree getRightChild() {
  33.         return this.rightChild;
  34.     }
  35.  
  36.     public void setRightChild(final Tree rightChild) {
  37.         this.rightChild = rightChild;
  38.     }
  39.  
  40.     public void createTree() {
  41.         rootNode = null;
  42.     }
  43.  
  44.     public Tree findNode(int value) {
  45.         Tree currentNode = rootNode;
  46.         while (currentNode.getValue() != value) {
  47.             if (value < currentNode.getValue()) {
  48.                 currentNode = currentNode.getLeftChild();
  49.             } else {
  50.                 currentNode = currentNode.getRightChild();
  51.             }
  52.             if (currentNode == null) {
  53.                 return null;
  54.             }
  55.         }
  56.         return currentNode;
  57.     }
  58.  
  59.     public void addNode(int value) {
  60.         Tree newNode = new Tree();
  61.         newNode.setValue(value);
  62.         if (rootNode == null) {
  63.             rootNode = newNode;
  64.         }
  65.         else {
  66.             Tree currentNode = rootNode;
  67.             Tree parentNode;
  68.             while (true)
  69.             {
  70.                 parentNode = currentNode;
  71.                 if(value == currentNode.getValue()) {
  72.                     return;
  73.                 }
  74.                 else  if (value < currentNode.getValue()) {
  75.                     currentNode = currentNode.getLeftChild();
  76.                     if (currentNode == null){
  77.                         parentNode.setLeftChild(newNode);
  78.                         return;
  79.                     }
  80.                 }
  81.                 else {
  82.                     currentNode = currentNode.getRightChild();
  83.                     if (currentNode == null) {
  84.                         parentNode.setRightChild(newNode);
  85.                         return;
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.     }
  91.  
  92.     public boolean deleteNode(int value)
  93.     {
  94.         Tree currentNode = rootNode;
  95.         Tree parentNode = rootNode;
  96.         boolean isLeftChild = true;
  97.         while (currentNode.getValue() != value) {
  98.             parentNode = currentNode;
  99.             if (value < currentNode.getValue()) {
  100.                 isLeftChild = true;
  101.                 currentNode = currentNode.getLeftChild();
  102.             }
  103.             else {
  104.                 isLeftChild = false;
  105.                 currentNode = currentNode.getRightChild();
  106.             }
  107.             if (currentNode == null)
  108.                 return false;
  109.         }
  110.  
  111.         if (currentNode.getLeftChild() == null && currentNode.getRightChild() == null) {
  112.             if (currentNode == rootNode)
  113.                 rootNode = null;
  114.             else if (isLeftChild)
  115.                 parentNode.setLeftChild(null);
  116.             else
  117.                 parentNode.setRightChild(null);
  118.         }
  119.         else if (currentNode.getRightChild() == null) {
  120.             if (currentNode == rootNode)
  121.                 rootNode = currentNode.getLeftChild();
  122.             else if (isLeftChild)
  123.                 parentNode.setLeftChild(currentNode.getLeftChild());
  124.             else
  125.                 parentNode.setRightChild(currentNode.getLeftChild());
  126.         }
  127.         else if (currentNode.getLeftChild() == null) {
  128.             if (currentNode == rootNode)
  129.                 rootNode = currentNode.getRightChild();
  130.             else if (isLeftChild)
  131.                 parentNode.setLeftChild(currentNode.getRightChild());
  132.             else
  133.                 parentNode.setRightChild(currentNode.getRightChild());
  134.         }
  135.         else {
  136.             Tree heir = receiveHeir(currentNode);
  137.             if (currentNode == rootNode)
  138.                 rootNode = heir;
  139.             else if (isLeftChild)
  140.                 parentNode.setLeftChild(heir);
  141.             else
  142.                 parentNode.setRightChild(heir);
  143.         }
  144.         return true;
  145.     }
  146.  
  147.     private Tree receiveHeir(Tree node) {
  148.         Tree parentNode = node;
  149.         Tree heirNode = node;
  150.         Tree currentNode = node.getRightChild();
  151.         while (currentNode != null) {
  152.             parentNode = heirNode;
  153.             heirNode = currentNode;
  154.             currentNode = currentNode.getLeftChild();
  155.         }
  156.         if (heirNode != node.getRightChild()) {
  157.             parentNode.setLeftChild(heirNode.getRightChild());
  158.             heirNode.setRightChild(node.getRightChild());
  159.         }
  160.         return heirNode;
  161.     }
  162.  
  163.     public static String getAllKeys(Tree node) {
  164.  
  165.         if (node != null) {
  166.             getAllKeys(node.getLeftChild());
  167.             result = result + node.getValue() + " ";
  168.             getAllKeys(node.getRightChild());
  169.         }
  170.         return result;
  171.     }
  172.  
  173.     public String mirrorTree() {
  174.         Stack globalStack = new Stack();
  175.         globalStack.push(rootNode);
  176.         String printMirrorTree = "";
  177.         int gaps = 32;
  178.         boolean isRowEmpty = false;
  179.         while (!isRowEmpty) {
  180.             Stack localStack = new Stack();
  181.             isRowEmpty = true;
  182.             for (int j = 0; j < gaps; j++)
  183.                 printMirrorTree = printMirrorTree + " ";
  184.             while (!globalStack.isEmpty()) {
  185.                 Tree temp = (Tree) globalStack.pop();
  186.                 if (temp != null) {
  187.                     printMirrorTree = printMirrorTree + temp.getValue();
  188.                     localStack.push(temp.getRightChild());
  189.                     localStack.push(temp.getLeftChild());
  190.                     if (temp.getLeftChild() != null || temp.getRightChild() != null)
  191.                         isRowEmpty = false;
  192.                 } else {
  193.                     printMirrorTree = printMirrorTree + "- ";
  194.                     localStack.push(null);
  195.                     localStack.push(null);
  196.                 }
  197.                 for (int j = 0; j < gaps * 2 - 2; j++)
  198.                     printMirrorTree = printMirrorTree + " ";
  199.             }
  200.             printMirrorTree = printMirrorTree + "\n";
  201.             gaps /= 2;
  202.             while (!localStack.isEmpty())
  203.                 globalStack.push(localStack.pop());
  204.         }
  205.         return printMirrorTree;
  206.     }
  207.  
  208.     public String printTree() {
  209.         Stack globalStack = new Stack();
  210.         globalStack.push(rootNode);
  211.         String treeStr = "";
  212.         int gaps = 32;
  213.         boolean isRowEmpty = false;
  214.         while (!isRowEmpty) {
  215.             Stack localStack = new Stack();
  216.             isRowEmpty = true;
  217.  
  218.             for (int j = 0; j < gaps; j++)
  219.                 treeStr = treeStr + " ";
  220.             while (!globalStack.isEmpty()) {
  221.                 Tree temp = (Tree) globalStack.pop();
  222.                 if (temp != null) {
  223.                     treeStr = treeStr + temp.getValue();
  224.                     localStack.push(temp.getLeftChild());
  225.                     localStack.push(temp.getRightChild());
  226.                     if (temp.getLeftChild() != null || temp.getRightChild() != null)
  227.                         isRowEmpty = false;
  228.                 } else {
  229.                     treeStr = treeStr + "- ";
  230.                     localStack.push(null);
  231.                     localStack.push(null);
  232.                 }
  233.                 for (int j = 0; j < gaps * 2 - 2; j++)
  234.                     treeStr = treeStr + " ";
  235.             }
  236.             treeStr = treeStr + "\n";
  237.             gaps /= 2;
  238.             while (!localStack.isEmpty())
  239.                 globalStack.push(localStack.pop());
  240.         }
  241.         return treeStr;
  242.     }
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement