链式二叉树的创建及遍历
描述:
树的遍历有先序遍历、中序遍历和后序遍历。先序遍历的操作定义是先访问根结点,然后访问左子树,最后访问右子树。中序遍历的操作定义是先访问左子树,然后访问根,最后访问右子树。后序遍历的操作定义是先访问左子树,然后访问右子树,最后访问根。对于采用链式存储结构的二叉树操作中,创建二叉树通常采用先序次序方式输入二叉树中的结点的值,空格表示空树。对于如下的二叉树,我们可以通过如下输入“AE-F–H–”得到( ‘-’表示空子树)。
试根据输入创建对应的链式二叉树,并输入其先序、中序和后序遍历结果。
输入:
输入第一行为一个自然数n,表示用例个数
接下来为n行字符串,每行用先序方式输入的要求创建的二叉树结点,’-’表示前一结点的子树为空子树。
输出:
对每个测试用例,分别用三行依次输出其先序、中序和后序遍历结果。
样例输入:
1
abdh---e-i--cf-j--gk---
1
2
样例输出:
abdheicfjgk
hdbeiafjckg
hdiebjfkgca
————————————————
import java.util.*;
public class Main {
private static class Node{
char value;
Node lchild, rchild;
public Node(char value) {
this.value = value;
}
}
private static Node root = null;
// 用来标记当前创建到哪了
private static int id;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while (n-- > 0) {
String str = sc.next();
id = -1;
root = create(root, str);
pre_order();
in_order();
post_order();
}
}
public static Node create(Node root, String str) {
++id;
if (id < str.length() && str.charAt(id) != '-') {
root = new Node(str.charAt(id));
root.lchild = create(root.lchild, str);
root.rchild = create(root.rchild, str);
} else {
root = null;
}
return root;
}
// 前序遍历
public static void pre_order() {
pre_order(root);
System.out.println();
}
private static void pre_order(Node root) {
if (root != null) {
System.out.print(root.value);
pre_order(root.lchild);
pre_order(root.rchild);
}
}
// 中序遍历
public static void in_order() {
in_order(root);
System.out.println();
}
private static void in_order(Node root) {
if (root != null) {
in_order(root.lchild);
System.out.print(root.value);
in_order(root.rchild);
}
}
// 后序遍历
public static void post_order() {
post_order(root);
System.out.println();
}
private static void post_order(Node root) {
if (root != null) {
post_order(root.lchild);
post_order(root.rchild);
System.out.print(root.value);
}
}
}
二叉排序树的创建与使用
描述:
二叉排序树的定义是:或者是一棵空树,或者是具有下列性质的二叉树:(1)若它的左子树不空,则左子树上所有的结点值均小于它的根结点的值;(2)若它的右子树不空,则右子树上所有结点的值均大于或等于它的根结点的值;(3)它的左右子树也分别为二叉排序树。现要求根据输入的元素值,构造一棵二叉排序树,并输出其先序遍历、中序遍历和后序遍历结果。
输入:
输入第一行为测试用例个数n,接下来为n个测试用例,每个测试用例占两行,其中第一行为元素个数m,第二行为m个需要构造成二叉排序树的元素值。
输出:
每个测试用例用三行输出,其中第一行输出先序遍历结果,第二行输出中序遍历结果,第三行输出后序遍历结果。各元素之间用一个空格隔开。
样例输入:
1
5
8 4 2 6 4
样例输出:
8 4 2 6 4
2 4 4 6 8
2 4 6 4 8
import java.util.Scanner;
public class BinaryTree {
public static void main(String[] args){
TNode Root ;
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for (int i = 0; i < n; i++) {
Root=null;
int m= sc.nextInt();
for (int j = 0; j < m; j++) {
int e= sc.nextInt();
Root=IncreaseTree(Root,e);
}
preorder(Root);
System.out.println();
inorder(Root);
System.out.println();
postorder(Root);
System.out.println();
}
}
private static TNode IncreaseTree(TNode root, int e) {
if(root==null){
root=new TNode();
root.data=e;
root.LChild=null;
root.RChild=null;
}
else {
if(e<root.data){
root.LChild=IncreaseTree(root.LChild,e);
}
else
root.RChild=IncreaseTree(root.RChild,e);
}
return root;
}
//鍏堝簭閬嶅巻
private static void preorder(TNode root) {
if(root==null) return;
System.out.print(root.data+" ");
preorder(root.LChild);
preorder(root.RChild);
}
//涓簭閬嶅巻
private static void inorder(TNode root) {
if(root==null) return;
inorder(root.LChild);
System.out.print(root.data+" ");
inorder(root.RChild);
}
//鍚庡簭閬嶅巻
private static void postorder(TNode root) {
if(root==null) return;
postorder(root.LChild);
postorder(root.RChild);
System.out.print(root.data+" ");
}
}
class TNode{
public int data;
public TNode LChild;
public TNode RChild;
}
构造哈夫曼树
题目描述:
根据给定的叶结点字符及其对应的权值创建哈夫曼树。
输入:
第一行为叶子结点的数目n(1<=n<=100)。第二行为一个字符串,包含n个字符,每个字符对应一个叶子结点,第三行为每个叶子结点的概率(即权值),要求根据各叶结点构造哈夫曼树。构造哈夫曼树的原则是先两个最小的,构造一个父结点,其中最小的结点为左孩子,次小的为右孩子,如果两个最小的叶结点相等,则取排在前一个位置的为左孩子。
输出:
哈夫曼树的权值,左孩子,右孩子及其对应的父亲,相邻数据之间用空格隔开;
输入样例:
5
abcde
15 25 15 20 25
1
2
3
输出样例:
15 0 0 6
25 0 0 7
15 0 0 6
20 0 0 7
25 0 0 8
30 1 3 8
45 4 2 9
55 5 6 9
100 7 8 0
import java.util.*;
public class Main {
private static class Node{
int value, lchild, rchild, parent;
public Node() {
this.value = 0;
this.lchild = 0;
this.rchild = 0;
this.parent = 0;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String str = sc.next();
Node[] hfm = new Node[2 * n - 1];
for (int i = 0; i < n; i++) {
hfm[i] = new Node();
hfm[i].value = sc.nextInt();
}
for (int i = 0; i < n - 1; i++) {
// l1 记录最小叶节点下标,l2 记录次小叶节点下标
int l1 = -1, l2 = -1;
for (int j = 0; j < n + i; j++) {
if (hfm[j].parent == 0 && (l1 == -1 || hfm[j].value < hfm[l1].value)) {
l2 = l1;
l1 = j;
} else if (hfm[j].parent == 0 && (l2 == -1 || hfm[j].value < hfm[l2].value)) {
l2 = j;
}
}
hfm[n + i] = new Node();
hfm[n + i].value = hfm[l1].value + hfm[l2].value;
hfm[n + i].lchild = l1 + 1;
hfm[n + i].rchild = l2 + 1;
hfm[l1].parent = hfm[l2].parent = n + i + 1;
}
for (int i = 0; i < 2 * n - 1; i++) {
System.out.println(hfm[i].value + " " + hfm[i].lchild + " " + hfm[i].rchild + " " + hfm[i].parent);
}
}
}
二叉排序树的插入删除和查找
pre: 前序遍历
in: 中序遍历
post:后序遍历
insert: 插入,本题中不会出现相同的元素
delete: 删除,删除成功输出TRUE,没有该元素则输出FALSE,删除的方法是如果有左子树,以左子树中最大值作为新的树根,否则,以右子树最小值作为树根。
search: 查找,存在该元素输出YES, 否则输出NO
exit:退出
输入:
输入的第一行为整数 N
接下来一行为N个整数,你需要把这N个整数构建出相应的二叉排序树
之后是一组指令。
输出:
根据相应的指令进行输出。说明:遍历各元素之间用一个空格隔开。
样例输入:
10
31 15 9 5 4 7 8 50 42 2
pre
in
post
delete
15
delete
88
in
search
8
search
222
insert
15
pre
in
post
exit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
样例输出:
31 15 9 5 4 2 7 8 50 42
2 4 5 7 8 9 15 31 42 50
2 4 8 7 5 9 15 42 50 31
TRUE
FALSE
2 4 5 7 8 9 31 42 50
YES
NO
31 9 5 4 2 7 8 15 50 42
2 4 5 7 8 9 15 31 42 50
2 4 8 7 5 15 9 42 50 31
注意:在样例中,我先把15删除掉了,然后再插入15,观察最开始的三个遍历,和最后的三次遍历,遍历结果是有区别的
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
BTree bt = new BTree();
for (int i = 0; i < n; i++) {
bt.insert(sc.nextInt());
}
String op = sc.next();
while (!op.equals("exit")) {
if (op.equals("pre")) {
bt.pre_order();
} else if (op.equals("in")) {
bt.in_order();
} else if (op.equals("post")) {
bt.post_order();
} else if (op.equals("delete")) {
if (bt.delete(sc.nextInt())) {
System.out.println("TRUE");
} else {
System.out.println("FALSE");
}
} else if (op.equals("search")) {
if (bt.search(sc.nextInt())) {
System.out.println("YES");
} else {
System.out.println("NO");
}
} else if (op.equals("insert")) {
bt.insert(sc.nextInt());
}
op = sc.next();
}
}
}
class BTree{
static private class Node{
int value;
Node lchild;
Node rchild;
public Node() {
}
public Node(int value) {
this.value = value;
}
}
Node root = null;
// 插入元素
public void insert(int x) {
root = insert(root, x);
}
private Node insert(Node root, int value) {
if (root == null) {
root = new Node(value);
} else if (value > root.value) {
root.rchild = insert(root.rchild, value);
} else if (value < root.value) {
root.lchild = insert(root.lchild, value);
}
return root;
}
// 用来判断是否删除成功
boolean has_delete = false;
// 删除元素
public boolean delete(int x) {
has_delete = false;
root = delete(root, x);
return has_delete;
}
private Node delete(Node root, int value) {
if (root != null) {
// 如果找到之后 就删除
if (root.value == value) {
has_delete = true;
// 如果没有子树了,就将该节点删除
if (root.lchild == null && root.rchild == null) {
root = null;
} else if (root.lchild != null) {
// 如果有左孩子,找到最大值并替换
Node now = root.lchild;
Node last = root.lchild;
while (now.rchild != null) {
last = now;
now = now.rchild;
}
if (now != last) {
last.rchild = now.lchild;
root.value = now.value;
} else {
root.value = now.value;;
root.lchild = now.lchild;
}
} else {
// 如果有右孩子,找到最小值并替换
Node now = root.rchild;
Node last = root.rchild;
while (now.lchild != null) {
last = now;
now = now.lchild;
}
if (last != now) {
last.lchild = now.rchild;
root.value = now.value;
} else {
root.value = now.value;
root.rchild = now.rchild;
}
}
} else if (value < root.value) {
root.lchild = delete(root.lchild, value);
} else {
root.rchild = delete(root.rchild, value);
}
}
return root;
}
// 查找元素
public boolean search(int value) {
Node current = root;
while (current != null) {
if (value < current.value) {
current = current.lchild;
} else if (value > current.value) {
current = current.rchild;
} else {
break;
}
}
return current != null;
}
// 前序遍历
public void pre_order() {
pre_order(root);
System.out.println();
}
private void pre_order(Node root) {
if (root != null) {
System.out.print(root.value + " ");
pre_order(root.lchild);
pre_order(root.rchild);
}
}
// 中序遍历
public void in_order() {
in_order(root);
System.out.println();
}
private void in_order(Node root) {
if (root != null) {
in_order(root.lchild);
System.out.print(root.value + " ");
in_order(root.rchild);
}
}
// 后序遍历
public void post_order() {
post_order(root);
System.out.println();
}
private void post_order(Node root) {
if (root != null) {
post_order(root.lchild);
post_order(root.rchild);
System.out.print(root.value + " ");
}
}
}