Java构造二叉树,遍历,叶子节点,高度,二叉树的左右视图
节点node
package Study_1;
public class Node {
private Object val;
Node left;
public Node right;
public Node(Object val) {
this.val = val;
}
public Node(Node left, Node node, Node right, Object val) {
this.val = val;
this.left = left;
this.right = right;
}
public String toString(){
return "[ "+val+" ]";
}
}
构造二叉树方法 遍历,叶子节点,高度,二叉树的左右视图
package Study_1;
import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.List;
public class BinaryTree {
private Node root;
public BinaryTree(Object Data) {
this.root = new Node(Data);
}
List<Object> num = new ArrayList<>();
public List<Object> rightSideView(Node root) {
dfs(root,0);
return num;
}
public void dfs(Node root,int depth){
if(root==null){
return;
}
if(depth == num.size()){
num.add(root.getVal());
}
depth++;
dfs(root.left,depth);
dfs(root.right,depth);
}
public Node add(Node parentNode, Object data, Boolean isLeft) {
if (parentNode == null) {
throw new RuntimeException("父节点为空,无法添加子节点");
}
if (isLeft && parentNode.left != null) {
throw new RuntimeException("左节点已存在,无法添加");
}
if (!isLeft && parentNode.right != null) {
throw new RuntimeException("右节点已存在,添加失败");
}
Node newNode = new Node(data);
if (isLeft) {
parentNode.left = newNode;
} else {
parentNode.right = newNode;
}
return newNode;
}
public List<Node> preBinaryTree() {
return preTrase(root);
}
public List<Node> preTrase(Node node) {
List<Node> list = new ArrayList<>();
list.add(node);
if(node.left!=null){
list.addAll(preTrase(node.left));
}
if(node.right!=null){
list.addAll(preTrase(node.right));
}
return list;
}
public List<Node> last_tree(){
return last(root);
}
public List<Node> last(Node root){
List<Node> list = new ArrayList<>();
if(root.left!=null){
list.addAll(last(root.left));
}
if(root.right!=null){
list.addAll(last(root.right));
}
list.add(root);
return list;
}
public List<Node> middle(){
return middleTree(root);
}
public List middleTree(Node root){
List<Node> list = new ArrayList<>();
if(root.left!=null){
list.addAll(middleTree(root.left));
}
list.add(root);
if(root.right!=null){
list.addAll(middleTree(root.right));
}
return list;
}
public int numLeaves(Node root){
if(root==null){
return 0;
}
if(root.left==null&&root.right==null)
{
System.out.println(root);
return 1;
}
return (root.left==null?0:numLeaves(root.left))+(root.right==null?0:numLeaves(root.right));
}
public int height(Node root){
if(root==null)
return -1;
int leftHeight = root.left==null?0:height(root.left);
int rightHeight = root.right==null?0:height(root.right);
return Math.max(leftHeight,rightHeight)+1;
}
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree("根节点");
Node t1 = binaryTree.add(binaryTree.root,"左节点",true);
Node t2 = binaryTree.add(binaryTree.root,"右节点",false);
Node t12 = binaryTree.add(t1,"左节点的右节点",false);
Node t21 = binaryTree.add(t2,"右节点的左节点",true);
Node t3 = binaryTree.add(t21,"右节点的左节点的左节点",true);
System.out.println(binaryTree.preBinaryTree());
System.out.println(binaryTree.middle());
System.out.println(binaryTree.last_tree());
System.out.println("叶子节点数为:"+binaryTree.numLeaves(binaryTree.root));
System.out.println("树的高度为:"+binaryTree.height(binaryTree.root));
System.out.println("树的右视图"+binaryTree.rightSideView(binaryTree.root));
}
}