二叉树 工具类 java (创建,前中后序遍历,层次打印,广度优先深度优先遍历)

树的节点类

package datastructure.tree.binarytree;

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

public class BinaryTree {

	public TreeNode root;
	public List nodes=new ArrayList();
	
	public BinaryTree(int x){
		root=new TreeNode(x);
		nodes.add(root);
	}
	//object内要么是int,要么是null,为了构建完全二叉树
	//构建二叉树,先将obect化为treeNode塞入list,然后根据索引确定每个元素的left,right
	public BinaryTree(Object[] x){		
		for(int i=0;i queue = new LinkedList();
        
        int current;//当前层 还未打印的结点个数
        int next;//下一层结点个数
        
        queue.offer(root);
        current = 1;
        next = 0;
        while(!queue.isEmpty()){
            TreeNode currentNode = queue.poll();
            if (currentNode!=null) {
            	System.out.print(currentNode.val+" ");
                current--;
               
			}
            else{
            	System.out.print("null ");
            	 current--;
            	 queue.offer(null);
                 next++;
                 queue.offer(null);
                 next++;                
                 if(current ==0){
                     System.out.println();
                     current = next;
                     next = 0;
                     int temp=0;
                     for (TreeNode treeNode : queue) {
						if(treeNode==null){
							temp++;
						}
					}
                     if(temp==current){
                    	 System.out.println("end");
                         break;
                     }
                     
                 }
                continue;
            }
            
            if(currentNode.left != null){
                queue.offer(currentNode.left);
                next++;
            }
            else{
            	queue.offer(null);
                next++;
            }
            if(currentNode.right != null){
                queue.offer(currentNode.right);
                next++;
            }
            else{
            	queue.offer(null);
                next++;
            }
            if(current ==0){
                System.out.println();
                current = next;
                next = 0;
                int temp=0;
                for (TreeNode treeNode : queue) {
					if(treeNode==null){
						temp++;
					}
				}
                if(temp==current){
               	 System.out.println("end");
                    break;
                }
                
            }
            
        }
    }
	
	//宽度优先遍历 是简化的按层遍历,没有了current和next和打印null
	public static void breadthFirstSearch(TreeNode root){
		Queue queue = new LinkedList();
		if(root==null){
			return;
		}
		queue.offer(root);
		while(!queue.isEmpty()){
			TreeNode now=queue.poll();
			System.out.print(now.val+" ");
			if(now.left!=null){
				queue.offer(now.left);
			}
			if(now.right!=null){
				queue.offer(now.right);
			}
		}
		System.out.println();
	}

	//深度优先遍历
	//用栈 弹出自身 先加入右节点 再加入左节点,这样先弹出左节点,左节点的左右子节点又塞进去,在原右节点上面
	public static void depthFirstSearch(TreeNode root){
		Stack stack=new Stack();
		if(root==null){
			return;
		}
		stack.push(root);
		while(!stack.isEmpty()){
			TreeNode now=stack.pop();
			System.out.print(now.val+" ");
			if(now.right!=null){
				stack.push(now.right);
			}
			if(now.left!=null){
				stack.push(now.left);
			}
			
		}
		System.out.println();
	}
	
	
	
	
}


你可能感兴趣的:(数据结构-树,数据结构)