Last active
December 19, 2021 10:31
-
-
Save sunmeat/b95ff0ad755e37d2f5a88d58e13dfe5e to your computer and use it in GitHub Desktop.
java binary tree
This file contains hidden or 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
package tree; // and change class name at line 237! | |
class Tree { | |
class Node { // inner class! | |
private int value; | |
private Node parent; | |
private Node right; | |
private Node left; | |
public int getValue() { | |
return value; | |
} | |
} | |
private Node root; | |
public boolean isEmpty() { | |
return root == null; | |
} | |
public void showTree() { | |
System.out.print("\n"); | |
showTree(root); | |
System.out.print("\n\n"); | |
} | |
private void showTree(Node elem) { | |
if (elem != null) { | |
showTree(elem.left); | |
System.out.print(elem.getValue() + ", "); | |
showTree(elem.right); | |
} | |
} | |
public Node getRoot() { | |
return root; | |
} | |
public int getCount() { | |
int count = 0; | |
count = getCount(root, count); | |
return count; | |
} | |
private int getCount(Node elem, int count) { | |
if (elem != null) { | |
count = getCount(elem.left, count); | |
count++; | |
count = getCount(elem.right, count); | |
} | |
return count; | |
} | |
public Node findNode(int value) { | |
if (isEmpty()) { | |
return null; | |
} else { | |
Node f = root; | |
while (true) { | |
if (value < f.value) { | |
if (f.left != null) { | |
f = f.left; | |
} else { | |
break; | |
} | |
} else if (value > f.value) { | |
if (f.right != null) { | |
f = f.right; | |
} else { | |
break; | |
} | |
} else { | |
return f; | |
} | |
} | |
return null; | |
} | |
} | |
public void addNode(int value) { | |
if (findNode(value) != null) { | |
return; | |
} | |
Node n = new Node(); | |
n.right = n.left = null; | |
n.value = value; | |
Node parent = null; | |
if (isEmpty()) { | |
root = n; | |
root.parent = parent; | |
} else { | |
Node p = root; | |
while (p != null) { | |
parent = p; | |
if (n.value > p.value) { | |
p = p.right; | |
} else { | |
p = p.left; | |
} | |
} | |
if (n.value < parent.value) { | |
parent.left = n; | |
} else { | |
parent.right = n; | |
} | |
n.parent = parent; | |
} | |
} | |
public void clear() { | |
if (!isEmpty()) { | |
clear(root); | |
} | |
} | |
private void clear(Node elem) { | |
if (elem != null) { | |
clear(elem.left); | |
clear(elem.right); | |
elem = null; | |
} | |
} | |
public Tree() { | |
// nothing to do | |
} | |
public Tree(Tree bt) { | |
root = null; | |
addNode(bt.root.value); | |
clone(bt.root); | |
} | |
private void clone(Node elem) { | |
if (elem.left != null) { | |
addNode(elem.left.value); | |
} | |
if (elem.right != null) { | |
addNode(elem.right.value); | |
} | |
if (elem.left != null) { | |
clone(elem.left); | |
} | |
if (elem.right != null) { | |
clone(elem.right); | |
} | |
} | |
public boolean deleteNode(int value) { | |
Node d = findNode(value); | |
if (d == null) { | |
return false; | |
} | |
if (d == root && getCount() == 1) { | |
clear(); | |
return false; | |
} | |
Node parent = d.parent; | |
// нет потомков | |
if (d.left == null && d.right == null) { | |
if (parent.left == d) { | |
parent.left = null; | |
} else { | |
parent.right = null; | |
} | |
return true; | |
} | |
// есть правый потомок, но нет левого | |
if (d.left == null && d.right != null) { | |
if (d == root) { | |
root = d.right; | |
d.right.parent = null; | |
} else { | |
if (parent.left == d) { | |
parent.left = d.right; | |
} else { | |
parent.right = d.right; | |
} | |
d.right.parent = parent; | |
} | |
return true; | |
} | |
// есть левый, но нет правого потомка | |
if (d.left != null && d.right == null) { | |
if (d == root) { | |
root = d.left; | |
d.left.parent = null; | |
} else { | |
if (parent.left == d) { | |
parent.left = d.left; | |
} else { | |
parent.right = d.left; | |
} | |
d.left.parent = parent; | |
} | |
return true; | |
} | |
// есть оба потомка | |
if (d.left != null && d.right != null) { | |
Node r = d.right; | |
if (r.right == null && r.left == null) { | |
d.value = r.value; | |
d.right = null; | |
} else if (r.left != null) { | |
Node p = r.left; | |
while (p.left != null) { | |
p = p.left; | |
} | |
d.value = p.value; | |
if (p.right == null) { | |
p.parent.left = null; | |
} else { | |
p.parent.left = p.right; | |
} | |
} else { | |
d.value = r.value; | |
d.right = r.right; | |
} | |
} | |
return true; | |
} | |
} | |
class Program { | |
public static void main(String[] args) { | |
int size = 20; | |
int mass[] = new int[size]; | |
System.out.println("Elements of array:"); | |
for (int i = 0; i < size; i++) { | |
mass[i] = (int) (Math.random() * 200); | |
System.out.print(mass[i] + ", "); | |
} | |
Tree bt = new Tree(); | |
for (int i = 0; i < size; i++) { | |
bt.addNode(mass[i]); | |
} | |
System.out.print("\n\nElements of tree: "); | |
bt.showTree(); | |
Tree.Node f = bt.findNode(127); | |
if (f != null) { | |
System.out.println("BINGO! " + f.getValue() + "\n\n"); | |
} else { | |
System.out.println("В дереве нет значения 127..."); | |
} | |
for (int i = 0; i < 150; i++) { | |
bt.deleteNode(i); | |
} | |
bt.showTree(); | |
System.out.print("Осталось элементов: " + bt.getCount() + "\n\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment