笔记二叉树核心算法
二叉树
二叉树的分层遍历
public class TreeNode {
private TreeNode lChild;
private TreeNode rChild;
private TreeNode mRoot;
private List<TreeNode> datas;
private Object data ;
public TreeNode(){
}
public TreeNode(Object obj){
this(null,null,obj);
}
public TreeNode(TreeNode left,TreeNode right,Object data){
this.lChild= left;
this.rChild = right;
this.data = data;
}
public void createTree(Object[] objs) {
datas = new ArrayList<TreeNode>();
for (Object obj : objs)
datas.add(new TreeNode(obj));
mRoot = datas.get(0);
for (int i = 0; i < objs.length / 2; i++) {
datas.get(i).lChild = datas.get(i * 2 + 1);
if (i * 2 + 2 < datas.size()) {
datas.get(i).rChild = datas.get(i * 2 + 2);
}
}
}
//todo 按层次遍历二叉树 算法 经典
public ArrayList<Object> printFromTopBottom(TreeNode treeNode){
ArrayList<Object> list = new ArrayList<Object>();
Queue<TreeNode> queue = new ArrayBlockingQueue<TreeNode>(100);
TreeNode last = mRoot;
TreeNode nLast = mRoot;
queue.add(treeNode);
while(!queue.isEmpty()){
TreeNode out = queue
.poll();
System.out.println(out.data+" ");
list.add(out.data);
if(out.lChild!= null) {
queue.add(out.lChild);
nLast = out.lChild;
}
if(out.rChild!= null){
queue.add(out.rChild);
nLast = out.rChild;
}
if(out == last){
System.out.println("");
last= nLast;
}
}
return list;
}
前序遍历
public static void preorderTraversalRec(TreeNode root){
if(root == null) return ;
System.out.println(root.getData());
preorderTraversalRec(root.getLeft());
preorderTraversalRec(root.getRight());
}
public static void preorderTraversalStack(TreeNode root){
if(root == null )return ;
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode cur = stack.pop();
System.out.println(cur.getData().toString());
if(cur.getRight()!= null)//先押入右子节点,这样弹栈的时候先出左子节点
{
stack.push(cur.getRight());
}
if(cur.getLeft()!= null){
stack.push(cur.getLeft());
}
}
}