Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- public class Tree {
- private int value;
- private Tree leftChild;
- private Tree rightChild;
- public static Tree rootNode;
- public static String result = "";
- public Tree getRootNode(){
- return rootNode;
- }
- public int getValue() {
- return this.value;
- }
- public void setValue(final int value) {
- this.value = value;
- }
- public Tree getLeftChild() {
- return this.leftChild;
- }
- public void setLeftChild(final Tree leftChild) {
- this.leftChild = leftChild;
- }
- public Tree getRightChild() {
- return this.rightChild;
- }
- public void setRightChild(final Tree rightChild) {
- this.rightChild = rightChild;
- }
- public void createTree() {
- rootNode = null;
- }
- public Tree findNode(int value) {
- Tree currentNode = rootNode;
- while (currentNode.getValue() != value) {
- if (value < currentNode.getValue()) {
- currentNode = currentNode.getLeftChild();
- } else {
- currentNode = currentNode.getRightChild();
- }
- if (currentNode == null) {
- return null;
- }
- }
- return currentNode;
- }
- public void addNode(int value) {
- Tree newNode = new Tree();
- newNode.setValue(value);
- if (rootNode == null) {
- rootNode = newNode;
- }
- else {
- Tree currentNode = rootNode;
- Tree parentNode;
- while (true)
- {
- parentNode = currentNode;
- if(value == currentNode.getValue()) {
- return;
- }
- else if (value < currentNode.getValue()) {
- currentNode = currentNode.getLeftChild();
- if (currentNode == null){
- parentNode.setLeftChild(newNode);
- return;
- }
- }
- else {
- currentNode = currentNode.getRightChild();
- if (currentNode == null) {
- parentNode.setRightChild(newNode);
- return;
- }
- }
- }
- }
- }
- public boolean deleteNode(int value)
- {
- Tree currentNode = rootNode;
- Tree parentNode = rootNode;
- boolean isLeftChild = true;
- while (currentNode.getValue() != value) {
- parentNode = currentNode;
- if (value < currentNode.getValue()) {
- isLeftChild = true;
- currentNode = currentNode.getLeftChild();
- }
- else {
- isLeftChild = false;
- currentNode = currentNode.getRightChild();
- }
- if (currentNode == null)
- return false;
- }
- if (currentNode.getLeftChild() == null && currentNode.getRightChild() == null) {
- if (currentNode == rootNode)
- rootNode = null;
- else if (isLeftChild)
- parentNode.setLeftChild(null);
- else
- parentNode.setRightChild(null);
- }
- else if (currentNode.getRightChild() == null) {
- if (currentNode == rootNode)
- rootNode = currentNode.getLeftChild();
- else if (isLeftChild)
- parentNode.setLeftChild(currentNode.getLeftChild());
- else
- parentNode.setRightChild(currentNode.getLeftChild());
- }
- else if (currentNode.getLeftChild() == null) {
- if (currentNode == rootNode)
- rootNode = currentNode.getRightChild();
- else if (isLeftChild)
- parentNode.setLeftChild(currentNode.getRightChild());
- else
- parentNode.setRightChild(currentNode.getRightChild());
- }
- else {
- Tree heir = receiveHeir(currentNode);
- if (currentNode == rootNode)
- rootNode = heir;
- else if (isLeftChild)
- parentNode.setLeftChild(heir);
- else
- parentNode.setRightChild(heir);
- }
- return true;
- }
- private Tree receiveHeir(Tree node) {
- Tree parentNode = node;
- Tree heirNode = node;
- Tree currentNode = node.getRightChild();
- while (currentNode != null) {
- parentNode = heirNode;
- heirNode = currentNode;
- currentNode = currentNode.getLeftChild();
- }
- if (heirNode != node.getRightChild()) {
- parentNode.setLeftChild(heirNode.getRightChild());
- heirNode.setRightChild(node.getRightChild());
- }
- return heirNode;
- }
- public static String getAllKeys(Tree node) {
- if (node != null) {
- getAllKeys(node.getLeftChild());
- result = result + node.getValue() + " ";
- getAllKeys(node.getRightChild());
- }
- return result;
- }
- public String mirrorTree() {
- Stack globalStack = new Stack();
- globalStack.push(rootNode);
- String printMirrorTree = "";
- int gaps = 32;
- boolean isRowEmpty = false;
- while (!isRowEmpty) {
- Stack localStack = new Stack();
- isRowEmpty = true;
- for (int j = 0; j < gaps; j++)
- printMirrorTree = printMirrorTree + " ";
- while (!globalStack.isEmpty()) {
- Tree temp = (Tree) globalStack.pop();
- if (temp != null) {
- printMirrorTree = printMirrorTree + temp.getValue();
- localStack.push(temp.getRightChild());
- localStack.push(temp.getLeftChild());
- if (temp.getLeftChild() != null || temp.getRightChild() != null)
- isRowEmpty = false;
- } else {
- printMirrorTree = printMirrorTree + "- ";
- localStack.push(null);
- localStack.push(null);
- }
- for (int j = 0; j < gaps * 2 - 2; j++)
- printMirrorTree = printMirrorTree + " ";
- }
- printMirrorTree = printMirrorTree + "\n";
- gaps /= 2;
- while (!localStack.isEmpty())
- globalStack.push(localStack.pop());
- }
- return printMirrorTree;
- }
- public String printTree() {
- Stack globalStack = new Stack();
- globalStack.push(rootNode);
- String treeStr = "";
- int gaps = 32;
- boolean isRowEmpty = false;
- while (!isRowEmpty) {
- Stack localStack = new Stack();
- isRowEmpty = true;
- for (int j = 0; j < gaps; j++)
- treeStr = treeStr + " ";
- while (!globalStack.isEmpty()) {
- Tree temp = (Tree) globalStack.pop();
- if (temp != null) {
- treeStr = treeStr + temp.getValue();
- localStack.push(temp.getLeftChild());
- localStack.push(temp.getRightChild());
- if (temp.getLeftChild() != null || temp.getRightChild() != null)
- isRowEmpty = false;
- } else {
- treeStr = treeStr + "- ";
- localStack.push(null);
- localStack.push(null);
- }
- for (int j = 0; j < gaps * 2 - 2; j++)
- treeStr = treeStr + " ";
- }
- treeStr = treeStr + "\n";
- gaps /= 2;
- while (!localStack.isEmpty())
- globalStack.push(localStack.pop());
- }
- return treeStr;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement