您的位置:首页 > 其它

树的基本操作

2015-06-16 14:53 357 查看
package datastructure;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

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 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 else if  else
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 DSTreet1 {

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.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);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: