二叉排序数
package com.weeks.tree.binarysorttree;
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
BinarySortTree binarySortTree = new BinarySortTree();
for (int i = 0; i < arr.length; i++){
binarySortTree.add(new Node(arr[i]));
}
System.out.println("中序遍历二叉排序树~~~");
binarySortTree.infixOrder(binarySortTree.getRoot());
binarySortTree.delNode(10);
System.out.println("\n中序遍历删除结点后的二叉排序树~~~");
binarySortTree.infixOrder(binarySortTree.getRoot());
}
}
class BinarySortTree{
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public void add(Node node){
if(root == null){
root = node;
}else{
root.add(node);
}
}
public void infixOrder(Node root){
if(root == null){
System.out.println("空树不能遍历");
return;
}
root.infixOrder();
}
public Node search(int value){
if(root == null){
return null;
}
return root.search(value);
}
public Node searchParent(int value){
if(root == null){
return null;
}
return root.searchParent(value);
}
public void delNode(int value){
if(root == null){
System.out.println("空树,没有结点可删除!!!");
return;
}
Node targetNode = root.search(value);
if(targetNode == null){
return;
}
if(root.getLeft() == null && root.getRight() == null && root.getData() == value){
root = null;
return;
}
Node parent = root.searchParent(value);
if(targetNode.getLeft() == null && targetNode.getRight() == null){
if(parent.getLeft() != null && parent.getLeft().getData() == value){
parent.setLeft(null);
}else if(parent.getRight() != null && parent.getRight().getData() == value){
parent.setRight(null);
}
}else if(targetNode.getLeft() != null && targetNode.getRight() != null){
int max = searchLeftMax(targetNode.getLeft());
targetNode.setData(max);
}else{
if(targetNode.getLeft() != null){
if(parent.getLeft().getData() == value){
parent.setLeft(targetNode.getLeft());
}else{
parent.setRight(targetNode.getLeft());
}
}else{
if(parent.getLeft().getData() == value){
parent.setLeft(targetNode.getRight());
}else{
parent.setRight(targetNode.getRight());
}
}
}
}
public int searchRightMin(Node node){
Node minTarget = node;
while(minTarget.getLeft() != null){
minTarget = minTarget.getLeft();
}
delNode(minTarget.getData());
return minTarget.getData();
}
public int searchLeftMax(Node node){
Node maxTarget = node;
while(maxTarget.getRight() != null){
maxTarget = maxTarget.getRight();
}
delNode(maxTarget.getData());
return maxTarget.getData();
}
}
class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public void add(Node node){
if(node == null){
return;
}
if(node.getData() < this.getData()){
if(this.getLeft() == null){
this.setLeft(node);
}else{
this.left.add(node);
}
}else{
if(this.getRight() == null){
this.setRight(node);
}else{
this.right.add(node);
}
}
}
public void infixOrder(){
if(this.left != null){
this.left.infixOrder();
}
System.out.print(this.data + " ");
if(this.right != null){
this.right.infixOrder();
}
}
public Node search(int value){
if(this.data == value){
return this;
}
if(this.data > value){
if(this.left == null){
return null;
}
return this.left.search(value);
}else {
if(this.right == null){
return null;
}
return this.right.search(value);
}
}
public Node searchParent(int value){
if((this.left != null && this.left.data == value) ||
(this.right != null && this.right.data == value)){
return this;
}else{
if(value < this.data && this.left != null){
return this.left.searchParent(value);
}else if(value >= this.data && this.right != null){
return this.right.searchParent(value);
}else{
return null;
}
}
}
}
AVL树
package com.weeks.tree.avltree;
public class AVLTreeDemo {
public static void main(String[] args) {
int[] arr = {10, 11, 7, 6, 8, 9};
AVLTree avlTree = new AVLTree();
for(int i : arr){
avlTree.add(new Node(i));
}
avlTree.infixOrder(avlTree.getRoot());
System.out.println("\navlTree的高度:" + avlTree.getRoot().height());
System.out.println("左子树的高度:" + avlTree.getRoot().leftHeight());
System.out.println("右子树的高度:" + avlTree.getRoot().rightHeight());
System.out.println("当前的根结点:" + avlTree.getRoot().getData());
}
}
class AVLTree{
private Node root;
public Node getRoot() {
return root;
}
public void setRoot(Node root) {
this.root = root;
}
public void add(Node node){
if(root == null){
root = node;
}else{
root.add(node);
}
}
public void infixOrder(Node root){
if(root == null){
System.out.println("空树不能遍历");
return;
}
root.infixOrder();
}
public Node search(int value){
if(root == null){
return null;
}
return root.search(value);
}
public Node searchParent(int value){
if(root == null){
return null;
}
return root.searchParent(value);
}
public void delNode(int value){
if(root == null){
System.out.println("空树,没有结点可删除!!!");
return;
}
Node targetNode = root.search(value);
if(targetNode == null){
return;
}
if(root.getLeft() == null && root.getRight() == null && root.getData() == value){
root = null;
return;
}
Node parent = root.searchParent(value);
if(targetNode.getLeft() == null && targetNode.getRight() == null){
if(parent.getLeft() != null && parent.getLeft().getData() == value){
parent.setLeft(null);
}else if(parent.getRight() != null && parent.getRight().getData() == value){
parent.setRight(null);
}
}else if(targetNode.getLeft() != null && targetNode.getRight() != null){
int max = searchLeftMax(targetNode.getLeft());
targetNode.setData(max);
}else{
if(parent != null){
if(targetNode.getLeft() != null) {
if (parent.getLeft().getData() == value) {
parent.setLeft(targetNode.getLeft());
} else {
parent.setRight(targetNode.getLeft());
}
}else{
root = targetNode.getLeft();
}
}else{
if(parent != null) {
if (parent.getLeft().getData() == value) {
parent.setLeft(targetNode.getRight());
} else {
parent.setRight(targetNode.getRight());
}
}else{
root = targetNode.getRight();
}
}
}
}
public int searchRightMin(Node node){
Node minTarget = node;
while(minTarget.getLeft() != null){
minTarget = minTarget.getLeft();
}
delNode(minTarget.getData());
return minTarget.getData();
}
public int searchLeftMax(Node node){
Node maxTarget = node;
while(maxTarget.getRight() != null){
maxTarget = maxTarget.getRight();
}
delNode(maxTarget.getData());
return maxTarget.getData();
}
}
class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.data = data;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
public void add(Node node){
if(node == null){
return;
}
if(node.getData() < this.getData()){
if(this.getLeft() == null){
this.setLeft(node);
}else{
this.left.add(node);
}
}else{
if(this.getRight() == null){
this.setRight(node);
}else{
this.right.add(node);
}
}
if(this.rightHeight() - this.leftHeight() > 1){
if(this.left != null && this.right.leftHeight() > this.right.rightHeight()){
this.right.rightRotate();
leftRotate();
}else{
leftRotate();
}
return;
}
if(this.leftHeight() - this.rightHeight() > 1){
if(this.left != null && this.left.rightHeight() > this.left.leftHeight()){
this.left.leftRotate();
rightRotate();
}else{
rightRotate();
}
}
}
public void infixOrder(){
if(this.left != null){
this.left.infixOrder();
}
System.out.print(this.data + " ");
if(this.right != null){
this.right.infixOrder();
}
}
public Node search(int value){
if(this.data == value){
return this;
}
if(this.data > value){
if(this.left == null){
return null;
}
return this.left.search(value);
}else {
if(this.right == null){
return null;
}
return this.right.search(value);
}
}
public Node searchParent(int value){
if((this.left != null && this.left.data == value) ||
(this.right != null && this.right.data == value)){
return this;
}else{
if(value < this.data && this.left != null){
return this.left.searchParent(value);
}else if(value >= this.data && this.right != null){
return this.right.searchParent(value);
}else{
return null;
}
}
}
public int leftHeight(){
if(this.left == null){
return 0;
}
return this.left.height();
}
public int rightHeight(){
if(this.right == null){
return 0;
}
return this.right.height();
}
public int height(){
return (Math.max(this.left == null ? 0 : this.left.height(), this.right == null ? 0 : this.right.height()) + 1);
}
public void leftRotate(){
Node newNode = new Node(this.data);
newNode.left = this.left;
newNode.right = this.right.left;
this.data = this.right.data;
this.left = newNode;
this.right = this.right.right;
}
public void rightRotate(){
Node newNode = new Node(this.data);
newNode.right = this.right;
newNode.left = this.left.right;
this.data = this.left.data;
this.right = newNode;
this.left = this.left.left;
}
}