import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
class PTree<AnyType> {// 双亲链表表示法
ArrayList<PTNode<AnyType>>nodes = new ArrayList<PTNode<AnyType>>();
static Scanner scan = new Scanner(System.in);
class PTNode<AnyType> {// 双亲节点定义
AnyType Data;
int parent;
public PTNode(AnyType Data, int parent) {
this.Data = Data;
this.parent = parent;
}
}
public void build() {// 建立双亲链表
int n, m;
AnyType s;
System.out.print("请输入结点个数:");
n = scan.nextInt();
System.out.println("请输入节点组合(data parent):");
for (int i = 0; i < n; i++) {
s = (AnyType) scan.next();
m = scan.nextInt();
nodes.add(new PTNode<AnyType>(s, m));
System.out.println();
System.out.println("建立节点:" + s);
}
}
public int depth(PTNode<AnyType> leaf) {// 节点深度
int depth = 0;
if (leaf != null) {
depth++;
while (leaf.parent != -1) {
leaf = nodes.get(leaf.parent);
depth++;
}
}
return depth;
}
public void depth() {// 节点深度
int depth = depth(nodes.get(0));
for (int i = 1; i < this.nodes.size(); i++) {
depth = Math.max(depth, depth(nodes.get(i)));
}
System.out.println("树的深度为:" + depth);
}
}
class ChildTree<AnyType> {// 孩子链表
CNode<AnyType> root;
static Scanner scan = new Scanner(System.in);
private Queue<CNode<AnyType>> cQueue = new LinkedList<CNode<AnyType>>();
class CNode<AnyType> {// 孩子链表节点定义
AnyType Data;
ArrayList<CNode<AnyType>> child;
public CNode() {
}
public CNode(AnyType Data) {// 构造方法
this.Data = Data;
this.child = new ArrayList<CNode<AnyType>>();
}
}
public CNode<AnyType> build(CNode<AnyType> r) {// 建立孩子链表
int n, m;
ArrayList<AnyType> array = new ArrayList<AnyType>();
CNode p = r;
System.out.print("请输入节点数:");
n = scan.nextInt();
System.out.println("请输入节点组合:");
do {
m = scan.nextInt();
for (int i = 0; i < m; i++) {
array.add((AnyType) scan.next());
}
if (this.root == null) {
this.root = new CNode(array.get(0));
System.out.println("建立根节点:" + array.get(0));
p = this.root;
}
else {
p = search(array.get(0));
if (p == null)
{
System.out.println("输入错误!,请再次输入");
continue;
}
}
for (int i = 0; i < m - 1; i++) {
p.child.add(new CNode(array.get(i + 1)));
System.out.println("建立节点:"
+ ((CNode<AnyType>) p.child.get(i)).Data);
}
array.clear();
n--;
} while (n > 0);
return root;
}
public CNode<AnyType> search(AnyType x) {// 查找结点
CNode<AnyType> p;
cQueue.clear();
if (root != null)
cQueue.add(root);
while (!cQueue.isEmpty()) {
p = cQueue.poll();
if (p.Data.equals(x))
return p;
for (int i = 0; i < p.child.size(); i++) {
cQueue.add(p.child.get(i));
}
}
return null;
}
public void level(CNode<AnyType> t) {// 层序遍历
CNode<AnyType> p;
if (t != null)
cQueue.add(t);
while (!cQueue.isEmpty()) {
p = cQueue.poll();
for (int i = 0; i < p.child.size(); i++) {
cQueue.add(p.child.get(i));
}
System.out.print(p.Data + " ");
}
}
}
class BiTree<AnyType> {// 二叉链表(孩子兄弟表示法)
BiTNode<AnyType> root = null;
private Queue<BiTNode<AnyType>> tQueue = new LinkedList<BiTNode<AnyType>>();
static Scanner scan = new Scanner(System.in);
class BiTNode<AnyType> {// 孩子兄弟节点定义
AnyType Data;
BiTNode<AnyType> firstChild, nextSibling;
public BiTNode() {// 构造方法
}
public BiTNode(AnyType Data) {// 构造方法
this.Data = Data;
}
public BiTNode(AnyType Data, BiTNode<AnyType> firstChild,
BiTNode<AnyType> nextSibling) {// 构造方法
this.Data = Data;
this.firstChild = firstChild;
this.nextSibling = nextSibling;
}
}
public void input() {// 建立孩子兄弟树
AnyType s, t, u;
int n;
BiTNode<AnyType> p;
System.out.println("请输入结点数:");
n = scan.nextInt() - 1;
System.out.println("请输入结点组合(层序输入):");
s = (AnyType) scan.next();
if (s.equals("null"))
s = null;
t = (AnyType) scan.next();
if (t.equals("null"))
t = null;
u = (AnyType) scan.next();
if (u.equals("null"))
u = null;
if (s != null) {
root = new BiTNode(s, new BiTNode(t), new BiTNode(u));
} else {
System.out.println("输入有误!");
}
while (n > 0) {
s = (AnyType) scan.next();
if (s.equals("null"))
s = null;
t = (AnyType) scan.next();
if (t.equals("null"))
t = null;
u = (AnyType) scan.next();
if (u.equals("null"))
u = null;
p = search(s);
if (p == null) {
System.out.println("输入有误!请再次输入:");
continue;
}
p.firstChild = new BiTNode(t);
p.nextSibling = new BiTNode(u);
n--;
}
}
public BiTNode<AnyType> search(AnyType x) {// 查找结点
BiTNode<AnyType> p;
tQueue.add(root);
while (!tQueue.isEmpty()) {
p = tQueue.poll();
if (p.Data.equals(x))
return p;
if (p.firstChild.Data != null)
tQueue.add(p.firstChild);
if (p.nextSibling.Data != null)
tQueue.add(p.nextSibling);
}
return null;
}
public void preTravel(BiTNode<AnyType> root) {// 先序遍历
if (root != null) {
if (root.Data != null)
System.out.print(root.Data + " ");
preTravel(root.firstChild);
preTravel(root.nextSibling);
}
}
public void behTravel(BiTNode<AnyType> root) {// 后序遍历
if (root != null) {
behTravel(root.firstChild);
if (root.Data != null)
System.out.print(root.Data + " ");
behTravel(root.nextSibling);
}
}
public void levelTravel() {// 层序遍历
BiTNode p;
tQueue.clear();
if (root != null)
tQueue.add(root);
while (!tQueue.isEmpty()) {
p = tQueue.poll();
System.out.print(p.Data + " ");
if (p.firstChild.Data != null)
tQueue.add(p.firstChild);
if (p.nextSibling.Data != null)
tQueue.add(p.nextSibling);
}
}
public int leaves(int sum, BiTNode<AnyType> root) {// 统计叶子节点
if (root == null)
return 0;
sum = leaves(sum, root.firstChild) + leaves(sum, root.nextSibling);
if (sum == 0) {
return ++sum;
}
return sum;
}
}
class HaffmanTree {// 哈弗曼树
HaffmanNode root;
int WPL;
static Scanner scan = new Scanner(System.in);
HaffmanNode head;
class HaffmanNode {// 哈弗曼节点定义
int weight;
HaffmanNode parent, lChild, rChild, next, pre;
public HaffmanNode() {
lChild = rChild = parent = null;
weight = 1;
}
public HaffmanNode(int weight) {
this.weight = weight;
}
public HaffmanNode(int weight, HaffmanNode lChild, HaffmanNode rChild) {
this.weight = weight;
this.lChild = lChild;
this.rChild = rChild;
}
public HaffmanNode(int weight, HaffmanNode next, HaffmanNode lChild,
HaffmanNode rChild) {
this.weight = weight;
this.next = next;
this.rChild = rChild;
this.lChild = lChild;
}
}
public HaffmanTree() {// 构造方法
root = null;
WPL = 0;
}
public void preTravel(HaffmanNode root) {// 先序输出哈夫曼树
if (root != null)
System.out.print(root.weight + " ");
if (root.lChild != null)
preTravel(root.lChild);
if (root.rChild != null)
preTravel(root.rChild);
}
public void input() {// 输入叶子节点
int n, k;
System.out.print("请输入叶子结点个数:");
n = scan.nextInt();
System.out.println("请输入每个结点的权重:");
int j = 0;
HaffmanNode p = null;
head = new HaffmanNode();
do {
k = scan.nextInt();
if (head.next == null) {
head.next = new HaffmanNode(k);
} else {
p = head.next;
head.next = new HaffmanNode(k);
head.next.next = p;
}
j++;
System.out.println("新建结点" + (j) + ":权重为" + head.next.weight);
} while (j < n);
System.out.println("先序遍历哈夫曼树:");
root = build();
}
public HaffmanNode build() {// 建立哈夫曼树
HaffmanNode p = null;
while (head.next.next.next != null) {
p = head.next.next;
HaffmanNode first = head.next, second = head.next.next;
do {
if (p.weight <= first.weight) {
second = first;
first = p;
} else if (p.weight < second.weight) {
second = p;
}
p = p.next;
} while (p != null);
p = head;
while (p.next != first) {
p = p.next;
}
p.next = first.next;
p = head;
while (p.next != second) {
p = p.next;
}
p.next = second.next;
HaffmanNode q = new HaffmanNode(first.weight + second.weight,
head.next, first, second);
head.next = q;
p = head;
}
if (head.next.weight < head.next.next.weight)
root = new HaffmanNode(head.next.weight + head.next.next.weight,
head.next, head.next.next);
else {
root = new HaffmanNode(head.next.weight + head.next.next.weight,
head.next.next, head.next);
}
head = null;
return root;
}
}
public class BSTree<AnyType extends Comparable<? super AnyType>> {// 二叉树
private BSTNode<AnyType> rootNode;
private Queue<BSTNode<AnyType>> treeQueue = new LinkedList<BSTNode<AnyType>>();
private int leaves;
ArrayList<BSTNode<AnyType>> order;
static Scanner scan = new Scanner(System.in);
private class BSTNode<AnyType> { // 二叉树结点结构
AnyType data;
BSTNode lchild, rchild; // 左右孩子
public BSTNode() {// 成员方法
data = null;
lchild = rchild = null;
}
public BSTNode(AnyType data) {// 成员方法
this.data = data;
lchild = rchild = null;
}
public BSTNode(AnyType data, BSTNode<AnyType> lchild,
BSTNode<AnyType> rchild) {// 成员方法
this.data = data;
this.lchild = lchild;
this.rchild = rchild;
}
}
int I;
public BSTNode<AnyType> preCreate(AnyType[] preOrder) {// 先序遍历建立
BSTNode<AnyType> rootNode = null;
if (I < preOrder.length) {
AnyType data = preOrder[I];
rootNode = new BSTNode<AnyType>(data);
I++;
if (data != null) {
rootNode.lchild = preCreate(preOrder);
rootNode.rchild = preCreate(preOrder);
}
}
return rootNode;
}
int J;
public BSTNode<AnyType> Create(ArrayList<AnyType> preOrder,
ArrayList<AnyType> midOrder) {// 先序中序遍历结果建立
AnyType x = null;
BSTNode<AnyType> rootNode;
if (!preOrder.get(0).equals("null")) {
rootNode = new BSTNode<AnyType>(preOrder.get(0));
x = preOrder.get(0);
} else {
rootNode = new BSTNode<AnyType>(null);
}
int i;
for (i = 0;; i++) {
if (x.equals(midOrder.get(i))) {
break;
}
}
ArrayList<AnyType> list1 = new ArrayList<AnyType>();
ArrayList<AnyType> list2 = new ArrayList<AnyType>();
for (int j = 0; j < i; j++) {
list1.add(midOrder.get(j));
}
for (int j = i + 1; j < midOrder.size(); j++) {
list2.add(midOrder.get(j));
}
i = i - 2;
try {
x = midOrder.get(i);
} catch (Exception e) {
return rootNode;
}
for (i = 1; i < preOrder.size(); i++) {
if (preOrder.get(i) == null)
continue;
if (preOrder.get(i).equals(x))
break;
}
ArrayList<AnyType> List1 = new ArrayList<AnyType>();
ArrayList<AnyType> List2 = new ArrayList<AnyType>();
for (int j = 1; j <= i + 2; j++) {
if (preOrder.get(i) == null)
j--;
List1.add(preOrder.get(j));
}
for (int j = i + 3; j < preOrder.size(); j++) {
List2.add(preOrder.get(j));
}
if (i != 1) {
rootNode.lchild = Create(List1, list1);
rootNode.rchild = Create(List2, list2);
} else {
// System.out.println(rootNode.data);
rootNode.lchild = new BSTNode<AnyType>(List1.get(0), null, null);
rootNode.rchild = new BSTNode<AnyType>(List2.get(0), null, null);
}
return rootNode;
}
public void level(BSTNode<AnyType> t) {// 层序遍历
BSTNode<AnyType> p;
if (t != null)
treeQueue.add(t);
while (!treeQueue.isEmpty()) {
p = treeQueue.poll();
if (p.lchild != null)
treeQueue.add(p.lchild);
if (p.rchild != null)
treeQueue.add(p.rchild);
if (p.data != null)
System.out.print(p.data + " ");
}
}
public int sumOfLeaves(BSTNode<AnyType> t) {// 叶子结点的个数
int L, R;
if (t == null)
return 0;
if (t.data != null) {
L = sumOfLeaves(t.lchild);
R = sumOfLeaves(t.rchild);
if (L + R == 0)
return L + R + 1;
else
return L + R;
} else {
return 0;
}
}
public BSTNode<AnyType> exchange(BSTNode<AnyType> t) {// 交换左右子树
if (t == null)
return t;
BSTNode<AnyType> tmp;
if (t.lchild != null) {
tmp = t.rchild;
t.rchild = t.lchild;
t.lchild = tmp;
t.lchild = exchange(t.lchild);
t.rchild = exchange(t.rchild);
} else if (t.rchild != null) {
tmp = t.rchild;
t.rchild = t.lchild;
t.lchild = tmp;
t.rchild = exchange(t.rchild);
t.lchild = exchange(t.lchild);
}
return t;
}
public boolean isCompelete(BSTNode<AnyType> t) {// 是不是完全二叉树
if (t != null)
treeQueue.add(t);
else
return false;
BSTNode<AnyType> p;
boolean flag = true;
while (!treeQueue.isEmpty()) {
p = treeQueue.poll();
if (p.lchild != null) {
treeQueue.add(p.lchild);
if (p.rchild != null)
treeQueue.add(p.rchild);
else
return false;
} else if (p.rchild != null)
return false;
// else if (flag)
// flag = false;
else
return flag;
}
return true;
}
public ArrayList<BSTNode<AnyType>> getInorder(BSTNode<AnyType> t) {// 顺序存储
BSTNode<AnyType> p;
treeQueue.clear();
if (t != null)
treeQueue.add(t);
ArrayList<BSTNode<AnyType>> array = new ArrayList<BSTNode<AnyType>>();
while (!treeQueue.isEmpty()) {
p = treeQueue.poll();
if (p.lchild != null)
treeQueue.add(p.lchild);
if (p.rchild != null)
treeQueue.add(p.rchild);
array.add(p);
}
return array;
}
public boolean isHeap(BSTNode<AnyType> t) {// 判断二叉树是不是堆
BSTNode<AnyType> p;
boolean flag = true;
if (t != null)
treeQueue.add(t);
while (treeQueue.isEmpty()) {
p = treeQueue.poll();
if (p.lchild != null) {
treeQueue.add(p.lchild);
if (((Comparable<? super AnyType>) p.lchild.data)
.compareTo(p.data) < 0) {
flag = false;
break;
}
}
if (p.rchild != null) {
treeQueue.add(p.rchild);
if (((Comparable<? super AnyType>) p.rchild.data)
.compareTo(p.data) > 0) {
flag = false;
break;
}
}
}
return flag;
}
static boolean judge = true;
public void isBST(BSTNode<AnyType> node) {// 判断堆是不是二叉排序树
if (node.data == null)
return;
if (node.lchild.data != null) {
if (((Comparable<? super AnyType>) node.lchild.data)
.compareTo((AnyType) node.data) > 0) {
judge = false;
return;
}
isBST(node.lchild);
}
if (node.lchild.data != null) {
if (((Comparable<? super AnyType>) node.rchild.data)
.compareTo((AnyType) node.data) < 0) {
judge = false;
return;
}
isBST(node.lchild);
}
}
public BSTNode<AnyType> insertBST(AnyType x, BSTNode<AnyType> root) {// 插入二叉排序树
if (root == null)
return root = new BSTNode<AnyType>(x, new BSTNode<AnyType>(null),
new BSTNode<AnyType>(null));
if (root.data == null) {
root.data = x;
root.lchild = new BSTNode<AnyType>(null);
root.rchild = new BSTNode<AnyType>(null);
return root;
}
if (root.data.compareTo(x) < 0) {
root.rchild = insertBST(x, root.rchild);
} else if (root.data.compareTo(x) > 0) {
root.lchild = insertBST(x, root.lchild);
}
return root;
}
public BSTNode<AnyType> insertBST(BSTNode<AnyType> x, BSTNode<AnyType> root) {// 插入二叉排序树
if (root == null)
return root = x;
if (root.data == null)
return root;
if (root.data.compareTo(x.data) < 0) {
root.rchild = insertBST(x, root.rchild);
} else if (root.data.compareTo(x.data) > 0) {
root.lchild = insertBST(x, root.lchild);
}
return root;
}
public BSTNode<AnyType> delete(AnyType x, BSTNode<AnyType> root) {// 二叉排序树节点删除
if (root.data == null)
return root;
if (root.data.equals(x)) {
if (root.lchild != null) {
if (root.rchild == null) {
return root.lchild;
} else {
root.lchild = insertBST(root.rchild, root.lchild);
return root.lchild;
}
} else {
if (root.rchild != null) {
return root.rchild;
} else {
return null;
}
}
}
if (root.data.compareTo(x) < 0) {
root.rchild = delete(x, root.rchild);
}
if (root.data.compareTo(x) > 0) {
root.lchild = delete(x, root.lchild);
}
return root;
}
public int depth(int i, BSTNode<AnyType> root) {// 节点深度
int LD, RD;
if (root.data == null)
return i;
else {
i++;
LD = depth(i, root.lchild);
RD = depth(i, root.rchild);
if (LD > RD) {
return LD;
} else {
return RD;
}
}
}
public BSTNode<AnyType> balance(BSTNode<AnyType> root) {// AVL树平衡操作
if (root.data == null) {
return root;
} else if (Math.abs(depth(0, root.lchild) - depth(0, root.rchild)) > 1) {
if (depth(0, root.lchild) > depth(0, root.rchild)) {
if (depth(0, root.lchild.lchild) < depth(0, root.lchild.rchild)) {// LR
BSTNode<AnyType> A, B, C;
A = root.lchild.rchild;
B = A.lchild;
C = A.rchild;
A.lchild = root.lchild;
A.rchild = root;
root.lchild.rchild = B;
root.lchild = C;
return root = balance(A);
} else {// LL
BSTNode<AnyType> A = root.lchild;
root.lchild = A.rchild;
A.rchild = root;
return root = balance(A);
}
} else {
if (depth(0, root.rchild.lchild) > depth(0, root.rchild.rchild)) {// RL
BSTNode<AnyType> A, B, C;
A = root.rchild.lchild;
B = A.lchild;
C = A.rchild;
A.lchild = root;
A.rchild = root.lchild;
root.rchild.lchild = C;
root.rchild = B;
root = A;
return root = balance(A);
} else {// RR
BSTNode<AnyType> A = root.rchild;
root.rchild = A.lchild;
A.lchild = root;
return root = balance(A);
}
}
} else {
root.lchild = balance(root.lchild);
root.rchild = balance(root.rchild);
}
return root;
}
public static void main(String[] args) {
int n;
String s[] = null;
/**/
BSTree<String> tree = new BSTree<String>();
System.out.println("先序遍历建立二叉树:");
System.out.print("请输入节点个数:");
n = scan.nextInt();
s = new String[n];
System.out.println("请按照先序遍历顺序输入各节点:");
for (int i = 0; i < n; i++) {
String S = scan.next();
s[i] = new String(S);
if (S.equals("null"))
s[i] = null;
System.out.println("建立节点" + s[i] + " ");
}
tree.rootNode = tree.preCreate(s);
s = null;
System.out.println("层序遍历二叉树:");
tree.level(tree.rootNode);
System.out.println();
System.out.println("二叉树叶子节点:");
System.out.println(tree.sumOfLeaves(tree.rootNode));
System.out.println("交换左右子树:");
tree.rootNode = tree.exchange(tree.rootNode);
System.out.println("层序遍历二叉树:");
tree.level(tree.rootNode);
System.out.println();
BSTree<String> tree1 = new BSTree<String>();
System.out.println("先序和中序遍历结果建立二叉树:");
System.out.print("请输入节点个数:");
n = scan.nextInt();
ArrayList<String> list1 = new ArrayList<String>();
System.out.println("请按照先序遍历顺序输入各节点:");
for (int i = 0; i < n; i++) {
String S = scan.next();
list1.add(S);
if (S.equals("null"))
list1.set(i, null);
}
ArrayList<String> list2 = new ArrayList<String>();
System.out.println("请按照中序遍历顺序输入各节点:");
for (int i = 0; i < n; i++) {
String S = scan.next();
list2.add(S);
}
tree1.rootNode = tree1.Create(list1, list2);
System.out.println("层序遍历二叉树:");
tree1.level(tree1.rootNode);
System.out.println();
System.out.println("是否是完全二叉树?");
if (tree1.isCompelete(tree1.rootNode)) {
System.out.println("是!");
tree1.order = tree1.getInorder(tree1.rootNode);
for (int i = 0; i < tree1.order.size(); i++)
System.out.print(tree1.order.get(i).data + " ");
} else {
System.out.println("否!");
}
System.out.println();
System.out.println("建立哈夫曼树:");
HaffmanTree h = new HaffmanTree();
h.input();
h.preTravel(h.root);
System.out.println();
System.out.println("判定二叉树是不是堆?");
if (tree1.isHeap(tree1.rootNode))
System.out.println("yes");
else {
System.out.println("no");
}
BSTree<String> tree2 = new BSTree<String>();
System.out.println("先序遍历建立二叉树:");
System.out.print("请输入节点个数:");
n = scan.nextInt();
String[] w = new String[n];
System.out.println("请按照先序遍历顺序输入各节点:");
for (int i = 0; i < n; i++) {
String S = scan.next();
w[i] = new String(S);
if (S.equals("null"))
w[i] = null;
}
tree2.rootNode = tree2.preCreate(w);
w = null;
System.out.println("判断二叉树是不是二叉排序树?");
tree2.isBST(tree2.rootNode);
if (tree2.judge)
System.out.println("yes");
else {
System.out.println("no");
}
System.out.println("插入一个节点:");
String S = scan.next();
tree2.rootNode = tree2.insertBST(S, tree2.rootNode);
System.out.println("层序遍历二叉树:");
tree2.level(tree2.rootNode);
System.out.println();
System.out.println("删除已有节点:");
S = scan.next();
tree2.rootNode = tree2.delete(S, tree2.rootNode);
System.out.println("层序遍历二叉树:");
tree2.level(tree2.rootNode);
System.out.println();
System.out.println("平衡二叉树:");
tree2.rootNode = tree2.balance(tree2.rootNode);
System.out.println("层序遍历二叉树:");
tree2.level(tree2.rootNode);
System.out.println();
System.out.println("建立孩子链表:");
ChildTree<String> tree4 = new ChildTree<String>();
tree4.build(tree4.root);
System.out.println("层序遍历孩子链表:");
tree4.level(tree4.root);
System.out.println();
System.out.println("用孩子兄弟表示法建立树:");
BiTree<String> tree3 = new BiTree<String>();
tree3.input();
System.out.println();
System.out.println("先序遍历孩子兄弟树:");
tree3.preTravel(tree3.root);
System.out.println();
System.out.println("后序遍历孩子兄弟树:");
tree3.behTravel(tree3.root);
System.out.println();
System.out.println("层序遍历孩子兄弟树:");
tree3.levelTravel();
System.out.println();
System.out.println("建立双亲链表:");
PTree<String> tree5 = new PTree<String>();
tree5.build();
tree5.depth();/**/
}
}
数据结构中树知识大全二叉树,哈夫曼树
最新推荐文章于 2022-11-20 10:00:00 发布