public class BinaryTree {
public static void main(String[] args) {
Tree tree = new Tree();
tree.insert(15);
tree.insert(10);
tree.insert(20);
tree.insert(32);
tree.insert(18);
tree.insert(27);
tree.inOrder(tree.getRoot());
System.out.println();
tree.preOrder(tree.getRoot());
System.out.println();
System.out.println(tree.delete(20));
tree.inOrder(tree.getRoot());
System.out.println();
tree.preOrder(tree.getRoot());
}
}
class Node {
int data;
Node leftChild;
Node rightChild;
public Node(int data) {
this.data = data;
}
}
class Tree {
private Node root;
public Node getRoot() {
return root;
}
public void insert(int data) {
Node newNode = new Node(data);
if(root == null) {
root = newNode;
}else {
Node current = root;
Node parent;
while(true) {
parent = current;
if(data < current.data) {
current = current.leftChild;
if(current == null) {
parent.leftChild = newNode;
return;
}
}else if(data > current.data) {
parent = current;
current = current.rightChild;
if(current == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
public Node find(int data) {
Node current = root;
while(current.data != data) {
if(data < current.data) {
current = current.leftChild;
}else {
current = current.rightChild;
}
if(current == null) {
return null;
}
}
return current;
}
public boolean delete(int data) {
if(root == null) {
return false;
}else if(root.data == data) {
root = null;
return true;
}else {
Node current = root;
Node parent = root;
boolean isLeftChild = false;
while(true) {
if(data < current.data) {
parent = current;
current = current.leftChild;
isLeftChild = true;
if(current == null) {
return false;
}
}else if(data > current.data) {
parent = current;
current = current.rightChild;
isLeftChild = false;
if(current == null) {
return false;
}
}else {
if(current.leftChild == null && current.rightChild == null) {
if(isLeftChild) {
parent.leftChild = null;
return true;
}else {
parent.rightChild = null;
return true;
}
}else if(current.leftChild != null && current.rightChild == null) {
if(isLeftChild) {
parent.leftChild = current.leftChild;
return true;
}else {
parent.rightChild = current.leftChild;
return true;
}
}else if(current.leftChild == null && current.rightChild != null) {
if(isLeftChild) {
parent.leftChild = current.rightChild;
return true;
}else {
parent.rightChild = current.rightChild;
return true;
}
}else {
Node rightFlag = current.rightChild;
Node parentTemp = current.rightChild;
Node temp = rightFlag.leftChild;
if(temp == null) {
if(isLeftChild) {
parent.leftChild = parentTemp;
parentTemp.leftChild = current.leftChild;
return true;
}else {
parent.rightChild = parentTemp;
parentTemp.leftChild = current.leftChild;
return true;
}
}else {
while(temp.leftChild != null) {
parentTemp = temp;
temp = temp.leftChild;
}
if(isLeftChild) {
parent.leftChild = temp;
temp.leftChild = current.leftChild;
temp.rightChild = current.rightChild;
parentTemp.leftChild = null;
return true;
}else {
parent.rightChild = temp;
temp.leftChild = current.leftChild;
temp.rightChild = current.rightChild;
parentTemp.leftChild = null;
return true;
}
}
}
}
}
}
}
public void preOrder(Node root) {
if(root != null) {
System.out.print(root.data+" ");
preOrder(root.leftChild);
preOrder(root.rightChild);
}
}
public void inOrder(Node root) {
if(root != null) {
inOrder(root.leftChild);
System.out.print(root.data+" ");
inOrder(root.rightChild);
}
}
public void postOrder(Node root) {
if(root != null) {
postOrder(root.leftChild);
postOrder(root.rightChild);
System.out.print(root.data+" ");
}
}
}
二叉搜索树的实现(Java语言)
最新推荐文章于 2024-08-19 16:01:20 发布