首页 > 代码库 > 二叉树
二叉树
package com.m01.program.maven_1;import java.util.Stack;public class TestBinaryTree { public static void main(String[] args) { TreeNode node=getBinaryTree(); node=destroy(node); System.out.println(node); Stack<TreeNode> s=new Stack(); } //创建二叉树 static TreeNode getBinaryTree(){ TreeNode node1=new TreeNode("A"); TreeNode node2=new TreeNode("B"); TreeNode node3=new TreeNode("C"); TreeNode node4=new TreeNode("BB1"); TreeNode node5=new TreeNode("BB2"); TreeNode node6=new TreeNode("CC1"); TreeNode node7=new TreeNode("CC2"); node1.setLeftNode(node2); node1.setRightNode(node3); node2.setLeftNode(node4); node2.setRightNode(node5); node3.setLeftNode(node6); node3.setRightNode(node7); return node1; } //输出节点 static void out(TreeNode<?> node){ System.out.println(node.getData()); } //先序遍历二叉树 static void xianIterator(TreeNode<?> node){ out(node); TreeNode leftNode=node.getLeftNode(); TreeNode rightNode=node.getRightNode(); if(leftNode!=null){ xianIterator(leftNode); } if(rightNode!=null){ xianIterator(rightNode); } } //中序遍历二叉树 static void zhongIterator(TreeNode<?> node){ TreeNode<?> leftNode=node.getLeftNode(); TreeNode<?> rightNode=node.getRightNode(); if(leftNode!=null){ zhongIterator(leftNode); } out(node); if(rightNode!=null){ zhongIterator(rightNode); } } //后序遍历二叉树 static void houIterator(TreeNode<?> node){ TreeNode<?> leftNode=node.getLeftNode(); TreeNode<?> rightNode=node.getRightNode(); if(rightNode!=null){ out(rightNode); } if(leftNode!=null){ out(leftNode); } out(node); } //判断 二叉树的高度 public static int getHeight(TreeNode<?> node){ if(node==null){ return 0; }else{ int left=getHeight(node.getLeftNode()); int right=getHeight(node.getRightNode()); return left>=right?left+1:right+1; } } //得到树中的对象数量 public static int getAllObjs(TreeNode<?> node){ if(node==null){ return 0; }else{ TreeNode leftNode=node.getLeftNode(); TreeNode rightNode=node.getRightNode(); int leftNum=getAllObjs(leftNode); int rightNum=getAllObjs(rightNode); return leftNum+rightNum+1; } } //删除node以及其以下的数据 static TreeNode<?> destroy(TreeNode<?> node){ System.out.println("--"); if(node!=null){ TreeNode leftNode=node.getLeftNode(); TreeNode rightNode=node.getRightNode(); destroy(leftNode); destroy(rightNode); node=null; } return node; }}/* * 定义节点 */class TreeNode<T>{ private T data; private TreeNode<T> leftNode; private TreeNode<T> rightNode; public TreeNode() { } public TreeNode(T data, TreeNode<T> leftNode, TreeNode<T> rightNode) { super(); this.data =http://www.mamicode.com/ data; this.leftNode = leftNode; this.rightNode = rightNode; } public TreeNode(T data) { this.data =http://www.mamicode.com/ data; } public TreeNode<T> getLeftNode() { return leftNode; } public void setLeftNode(TreeNode<T> leftNode) { this.leftNode = leftNode; } public TreeNode<T> getRightNode() { return rightNode; } public void setRightNode(TreeNode<T> rightNode) { this.rightNode = rightNode; } public T getData() { return data; } public void setData(T data) { this.data =http://www.mamicode.com/ data; } @Override public String toString() { return "TreeNode [data="http://www.mamicode.com/+ data +", leftNode=" + leftNode + ", rightNode=" + rightNode + "]"; } }
二叉树
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。