各类树的构造
基本的二叉树
package 数据结构与算法;
public class 树结构 {
public static void main(String[] args) {
BinaryTree binTree = new BinaryTree();
TreeNode root = new TreeNode(1);
binTree.setRoot(root);
TreeNode rootl = new TreeNode(2);
root.setLeftNode(rootl);
TreeNode rootr = new TreeNode(3);
root.setRightNode(rootr);
rootl.setLeftNode(new TreeNode(4));
rootl.setRightNode(new TreeNode(5));
rootr.setLeftNode(new TreeNode(6));
rootr.setRightNode(new TreeNode(7));
binTree.frontShow();
}
}
class BinaryTree {
TreeNode root;
public void setRoot(TreeNode root) {
this.root = root;
}
public TreeNode getRoot() {
return root;
}
public void frontShow() {
if (root != null) {
root.frontShow();
}
}
public void midShow() {
root.midShow();
}
public void afterShow() {
root.afterShow();
}
public TreeNode frontSearch(int i) {
return root.frontSearch(i);
}
public void delete(int i) {
root.delete(i);
}
}
class TreeNode {
int value;
TreeNode leftNode, rightNode;
public TreeNode(int value) {
this.value = value;
}
public void setLeftNode(TreeNode lNode) {
this.leftNode = lNode;
}
public void setRightNode(TreeNode rNode) {
this.rightNode = rNode;
}
public void frontShow() {
System.out.println(this.value);
if (leftNode != null) {
leftNode.frontShow();
}
if (rightNode != null) {
rightNode.frontShow();
}
}
public void midShow() {
if (leftNode != null) {
leftNode.midShow();
}
System.out.println(value);
if (rightNode != null) {
rightNode.midShow();
}
}
public void afterShow() {
if (leftNode != null) {
leftNode.afterShow();
}
if (rightNode != null) {
rightNode.afterShow();
}
System.out.println(value);
}
public TreeNode frontSearch(int i) {
TreeNode target = null;
if (this.value == i) {
return this;
} else {
if (leftNode != null) {
target = leftNode.frontSearch(i);
}
if (target != null) {
return target;
}
if (rightNode != null) {
target = rightNode.frontSearch(i);
}
}
return target;
}
public void delete(int i) {
TreeNode parent = this;
if (parent.leftNode != null && parent.leftNode.value == i) {
parent.leftNode = null;
return;
}
if (parent.rightNode != null && parent.rightNode.value == i) {
parent.rightNode = null;
return;
}
parent = leftNode;
if (parent != null) {
parent.delete(i);
return;
}
parent = rightNode;
if (parent != null) {
parent.delete(i);
return;
}
}
}
二叉排序树
package 数据结构与算法;
public class 树_二叉排序树 {
public static void main(String[] args) {
int[] arr = {7, 3, 10, 12, 5, 1, 9};
BinarySortTree bst = new BinarySortTree();
for (int i : arr) {
bst.add(new kNode(i));
}
bst.midShow();
System.out.println("----------");
System.out.println(bst.search(10).value);
System.out.println(bst.search(20));
System.out.println("----------");
kNode p1 = bst.searchParent(10);
System.out.println(p1.value);
System.out.println("----------");
bst.delete(9);
bst.midShow();
}
}
class BinarySortTree {
kNode root;
public void add(kNode node) {
if (root == null) {
root = node;
} else {
root.add(node);
}
}
public void midShow() {
if (root != null) {
root.midShow(root);
}
}
public kNode search(int value) {
if (root == null) return null;
else return root.search(value);
}
public void delete(int value) {
if (root == null) {
return;
} else {
kNode target = search(value);
if (target == null) {
return;
}
kNode parent = searchParent(value);
if (target.leftNode==null&&target.rightNode==null) {
if (parent.leftNode.value == value) {
parent.leftNode = null;
}
else {
parent.rightNode = null;
}
}
}
}
public kNode searchParent(int value) {
if (root==null) {
return null;
} else {
return root.searchParent(value);
}
}
}
class kNode {
int value;
kNode leftNode;
kNode rightNode;
public kNode(int value) {
this.value = value;
}
public void add(kNode node) {
if (node == null) return;
if (node.value < this.value) {
if (this.leftNode == null) this.leftNode = node;
else this.leftNode.add(node);
} else {
if (this.rightNode == null) this.rightNode = node;
else this.rightNode.add(node);
}
}
public void midShow(kNode node) {
if (node == null) return;
midShow(node.leftNode);
System.out.println(node.value);
midShow(node.rightNode);
}
public kNode search(int value) {
if(this.value == value) {
return this;
} else if (this.value > value) {
if (leftNode == null) return null;
return leftNode.search(value);
} else {
if (rightNode == null) return null;
return rightNode.search(value);
}
}
public kNode searchParent(int value) {
if ((this.leftNode != null && this.leftNode.value==value) || (this.rightNode != null && this.rightNode.value==value)) {
return this;
} else {
if (this.value > value && this.leftNode != null) {
return this.leftNode.searchParent(value);
} else if (this.value < value && this.rightNode != null) {
return this.rightNode.searchParent(value);
}
}
return null;
}
}
赫夫曼树
package 数据结构与算法;
import java.util.ArrayList;
import java.util.Collections;
public class 树_赫夫曼树 {
public static void main(String[] args) {
int[] arr = { 3, 7, 8, 29, 5, 11, 23, 14 };
myNode node = createHuffmanTree(arr);
System.out.println(node);
}
public static myNode createHuffmanTree(int[] arr) {
ArrayList<myNode> nodes = new ArrayList<>();
for (int value : arr) {
nodes.add(new myNode(value));
}
while (nodes.size() > 1) {
Collections.sort(nodes);
myNode left = nodes.get(nodes.size() - 1);
myNode right = nodes.get(nodes.size() - 2);
myNode parent = new myNode(left.value + right.value);
nodes.remove(left);
nodes.remove(right);
nodes.add(parent);
}
return nodes.get(0);
}
}
class myNode implements Comparable<myNode> {
int value;
myNode left;
myNode right;
public myNode(int value) {
this.value = value;
}
@Override
public int compareTo(myNode o) {
return -this.value - o.value;
}
@Override
public String toString() {
return "Node [value=" + value + "]";
}
}
完全二叉树(树的顺序存储)
package 数据结构与算法;
public class 树的顺序存储 {
public static void main(String[] args) {
int[] data = {1, 2, 3, 4, 5, 6, 7};
ArrayBinaryTree tree = new ArrayBinaryTree(data);
tree.frontShow(0);
}
}
class ArrayBinaryTree {
int [] data;
public ArrayBinaryTree(int[] data) {
this.data = data;
}
public void frontShow(int index) {
if (data == null || data.length == 0) {
return;
}
System.out.println(data[index]);
if (2*index+1 < data.length) frontShow(2*index+1);
if (2*index+2 < data.length) frontShow(2*index+2);
}
}
TODO