- //树结点类
- public class TreeNode {
- private int value;
- private TreeNode leftchild;
- private TreeNode rightchild;
- public int getValue() {
- return value;
- }
- public void setValue(int value) {
- this.value = value;
- }
- public TreeNode getLeftchild() {
- return leftchild;
- }
- public void setLeftchild(TreeNode leftchild) {
- this.leftchild = leftchild;
- }
- public TreeNode getRightchild() {
- return rightchild;
- }
- public void setRightchild(TreeNode rightchild) {
- this.rightchild = rightchild;
- }
- }
- //树类
- public class Tree {
- private TreeNode node;
- public TreeNode getNode() {
- return node;
- }
- public void setNode(TreeNode node) {
- this.node = node;
- }
- }
- //队列
- import java.util.LinkedList;
- public class Queue {
- private LinkedList<TreeNode> list;
- public LinkedList<TreeNode> getList() {
- return list;
- }
- public void setList(LinkedList<TreeNode> list) {
- this.list = list;
- }
- public Queue(){
- list = new LinkedList<TreeNode>();
- }
- public void enQueue(TreeNode node){
- list.add(node);
- }
- public TreeNode outQueue(){
- return list.removeFirst();
- }
- public boolean isEmpty(){
- return list.isEmpty();
- }
- }
- //二叉树类
- public class BinaryTree {
- private Tree tree;
- private Queue queue;
- public BinaryTree(){
- tree = new Tree();
- }
- //插入结点
- public void insertNode(TreeNode node){
- if(tree.getNode()==null){
- tree.setNode(node);
- return;
- }
- else{
- queue = new Queue();
- queue.enQueue(tree.getNode());
- while(!queue.isEmpty()){
- TreeNode temp = queue.outQueue();
- if(temp.getLeftchild()==null){
- temp.setLeftchild(node);
- return;
- }
- else if(temp.getRightchild()==null){
- temp.setRightchild(node);
- return;
- }
- else {
- queue.enQueue(temp.getLeftchild());
- queue.enQueue(temp.getRightchild());
- }
- }
- }
- }
- //中序遍历
- public void midOrder(TreeNode node){
- if(node!=null){
- this.midOrder(node.getLeftchild());
- System.out.println(node.getValue());
- this.midOrder(node.getRightchild());
- }
- }
- //前序遍历
- public void frontOrder(TreeNode node){
- if(node !=null){
- System.out.println(node.getValue());
- frontOrder(node.getLeftchild());
- frontOrder(node.getRightchild());
- }
- }
- //后序遍历
- public void lastOrder(TreeNode node){
- if(node != null){
- this.lastOrder(node.getLeftchild());
- this.lastOrder(node.getRightchild());
- System.out.println(node.getValue());
- }
- }
- public Tree getTree(){
- return tree;
- }
- }
- //测试类
- public class Client {
- public static void main(String[] args) {
- BinaryTree binaryTree = new BinaryTree();
- TreeNode[] nodes = new TreeNode[10];
- for(int i =0;i<nodes.length;i++){
- nodes[i] = new TreeNode();
- nodes[i].setValue(i);
- binaryTree.insertNode(nodes[i]);
- }
- System.out.println("先序遍历");
- binaryTree.frontOrder(binaryTree.getTree().getNode());
- System.out.println("中序遍历");
- binaryTree.midOrder(binaryTree.getTree().getNode());
- System.out.println("后序遍历");
- binaryTree.lastOrder(binaryTree.getTree().getNode());
- }
- }
- 输出结果:
- 先序遍历
- 0
- 1
- 3
- 7
- 8
- 4
- 9
- 2
- 5
- 6
- 中序遍历
- 7
- 3
- 8
- 1
- 9
- 4
- 0
- 5
- 2
- 6
- 后序遍历
- 7
- 8
- 3
- 9
- 4
- 1
- 5
- 6
- 2
- 0
用java构建完全二叉树
最新推荐文章于 2024-09-17 14:02:05 发布