import java.util.*
class BiTree {
char data;
BiTree leftchild;
BiTree rightchild;
}
class Counter {
// String array="ABD###CE##F##";
String array = "ABDG###EHI####CF###";
int i = -1;
char next() {
i++;
return array.charAt(i);
}
}
class LevelCounter {
int level;
public LevelCounter(int level) {
this.level = level;
}
LevelCounter prelevel() {
level--;
return this;
}
}
class SNode {
BiTree node;
boolean visited;
public SNode(BiTree node, boolean visited) {
this.node = node;
this.visited = visited;
}
}
class Tree {
public BiTree buildBiTree(BiTree t, Counter counter) {
char ch = counter.next();
if (ch == '#') {
t = null;
} else {
t = new BiTree();
t.data = ch;
t.leftchild = buildBiTree(t, counter);
t.rightchild = buildBiTree(t, counter);
}
return t;
}
public void preorder(BiTree t) {
if (t != null) {
System.out.print(t.data);
preorder(t.leftchild);
preorder(t.rightchild);
}
}
public void inorder(BiTree t) {
if (t != null) {
inorder(t.leftchild);
System.out.print(t.data);
inorder(t.rightchild);
}
}
public void postorder(BiTree t) {
if (t != null) {
postorder(t.leftchild);
postorder(t.rightchild);
System.out.print(t.data);
}
}
public void preorder2(BiTree t) {
Stack<BiTree> stack = new Stack<BiTree>();
while (t != null || !stack.isEmpty()) {
while (t != null) {
System.out.print(t.data);
stack.add(t);
t = t.leftchild;
}
if (!stack.isEmpty()) {
BiTree node = stack.pop();
t = node.rightchild;
}
}
}
public void preorder3(BiTree t) {
Stack<BiTree> stack = new Stack<BiTree>();
if (t != null) {
stack.add(t);
while (!stack.isEmpty()) {
BiTree node = stack.pop();
System.out.print(node.data);
if (node.rightchild != null)
stack.add(node.rightchild);
if (node.leftchild != null)
stack.add(node.leftchild);
}
}
}
public void inorder2(BiTree t) {
Stack<BiTree> stack = new Stack<BiTree>();
while (t != null || !stack.isEmpty()) {
while (t != null) {
stack.add(t);
t = t.leftchild;
}
if (!stack.isEmpty()) {
BiTree node = stack.pop();
System.out.print(node.data);
t = node.rightchild;
}
}
}
public void postorder2(BiTree t) {
Stack<SNode> stack = new Stack<SNode>();
while (t != null) {
stack.add(new SNode(t, false));
t = t.leftchild;
}
while (!stack.isEmpty()) {
SNode snode = stack.peek();
if (snode.visited == true || snode.node.rightchild == null) {
System.out.print(snode.node.data);
stack.pop();
} else {
snode.visited = true;
BiTree rnode = snode.node.rightchild;
while (rnode != null) {
stack.push(new SNode(rnode, false));
rnode = rnode.leftchild;
}
}
}
}
public void postorder3(BiTree root) {
BiTree pre = null;
Stack<BiTree> stack = new Stack<>();
while (root != null || !stack.isEmpty()) {
while (root != null) {
stack.push(root);
root = root.leftchild;
}
BiTree node = stack.pop();
if (node.rightchild == null || node.rightchild == pre) {
System.out.print(node.data);
pre = node;
} else {
stack.push(node);
root = node.rightchild;
}
}
}
public void leveltravel(BiTree t) {
Queue<BiTree> queue = new LinkedList<BiTree>();
if (t != null)
queue.add(t);
while (!queue.isEmpty()) {
BiTree node = queue.poll();
System.out.print(node.data);
if (node.leftchild != null)
queue.add(node.leftchild);
if (node.rightchild != null)
queue.add(node.rightchild);
}
}
public void printatlevel(BiTree t, int level) {
if (t != null) {
if (level == 0) {
System.out.print(t.data);
return;
}
printatlevel(t.leftchild, level - 1);
printatlevel(t.rightchild, level - 1);
}
}
public int treedepth(BiTree t) {
if (t == null)
return 0;
else {
int left = treedepth(t.leftchild);
int right = treedepth(t.rightchild);
return 1 + Math.max(left, right);
}
}
public int treewidth(BiTree t) {
if (t == null)
return 0;
Queue<BiTree> queue = new LinkedList<BiTree>();
queue.add(t);
int curlevelwidth = 1;
int lastlevelwidth = 1;
int tempwidth;
int width = 1;
while (!queue.isEmpty()) {
tempwidth = lastlevelwidth;
while (tempwidth != 0) {
BiTree node = queue.poll();
if (node.leftchild != null)
queue.add(node.leftchild);
if (node.rightchild != null)
queue.add(node.rightchild);
tempwidth--;
}
curlevelwidth = queue.size();
width = (curlevelwidth > width) ? curlevelwidth : width;
lastlevelwidth = curlevelwidth;
}
return width;
}
public int treewidth2(TreeNode t) {
if (t == null)
return 0;
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(t);
int width = 1;
int count;
TreeNode node;
while (!queue.isEmpty()) {
count = queue.size();
while (count-- > 0) {
node = queue.poll();
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
}
width = Math.max(width, queue.size());
}
return width;
}
public boolean nodepath(BiTree t, char value, List<BiTree> path) {
if (t == null)
return false;
if (t.data != value) {
if (nodepath(t.leftchild, value, path) || nodepath(t.rightchild, value, path)) {
path.add(t);
return true;
} else
return false;
} else {
path.add(t);
return true;
}
}
public BiTree commonancestor(BiTree t, char value1, char value2) {
ArrayList<BiTree> path1 = new ArrayList<BiTree>();
ArrayList<BiTree> path2 = new ArrayList<BiTree>();
nodepath(t, value1, path1);
nodepath(t, value2, path2);
int offset = path1.size() - path2.size();
int i = 0;
int j = 0;
if (offset > 0)
i += offset;
else if (offset < 0)
j -= offset;
for (; i < path1.size(); i++, j++) {
if (path1.get(i).data == path2.get(j).data) {
return path1.get(i);
}
}
return null;
}
}
public class Solution {
public static void main(String[] args) {
BiTree t = null;
Tree tree = new Tree();
Counter c = new Counter();
//建树
t = tree.buildBiTree(t, c);
System.out.println(t);
//前序
tree.preorder(t);
System.out.print(" | ");
tree.preorder2(t);
System.out.print(" | ");
tree.preorder3(t);
System.out.println();
//中序
tree.inorder(t);
System.out.print(" | ");
tree.inorder2(t);
System.out.println();
//后序
tree.postorder(t);
System.out.print(" | ");
tree.postorder2(t);
System.out.print(" | ");
tree.postorder3(t);
System.out.println();
//层次遍历
tree.leveltravel(t);
System.out.println();
//遍历某一层
//tree.printatlevel(t, new LevelCounter(2));
tree.printatlevel(t, 3);
System.out.println();
//深度
System.out.println("depth = " + tree.treedepth(t));
//宽度
System.out.println("width = " + tree.treewidth(t));
//最近公共祖先 B
System.out.println("最近公共祖先LCA:" + tree.commonancestor(t, 'G', 'I').data);
}
}
树的基本操作
于 2015-06-16 14:53:27 首次发布