day22

Day 22

Background

今天是学习java的第22天,今天学习的是树的存储。

Description

对于树的储存,用到了循环队列。具体操作是建立了两个队列,一个存值,另一个存对应的在二叉树中的序号。

Code

Code1:对之前写的循环队列进行一个改写,使其适合树的储存。

package datastructure;

public class CircleObjectQueue {
    /**
     * The tatal space. One space can't used.
     */
    public static final int TOTAL_SPACE = 10;

    Object[] data;

    /**
     * 队列的头和尾。
     */
    int head;
    int tail;

    public CircleObjectQueue() {
        data = new Object[TOTAL_SPACE];
        head = 0;
        tail = 0;
    } // Of the first constructure
    
    public void enQueue(Object paraValue) {
        if ((tail + 1) % TOTAL_SPACE == head) {
            System.out.println("Queue full.");
            return;
        } // Of if

        data[tail % TOTAL_SPACE] = paraValue;
        tail++;
    } // Of enQueue

    public Object deQueue() {
        if (tail == head) {
            return null;
        }

        Object resultValue = data[head % TOTAL_SPACE];
        head++;

        return resultValue;
    } // Of deQueue

    public String toString() {
        String resultString = "";

        if (head == tail) {
            return "empty";
        } // Of if

        for (int i = head; i < tail - 1; i++) {
            resultString += data[i % TOTAL_SPACE] + ", ";
        } // Of for i

        resultString += data[(tail - 1) % TOTAL_SPACE];
        return resultString;
    } // Of toString

    public static void main(String[] args) {
        CircleObjectQueue tempQueue = new CircleObjectQueue();
    } // Of main
}

Code 2 存储二叉树

package datastructure;

import java.util.Arrays;
import datastructure.CircleObjectQueue.*;

public class BinaryCharTree {
    
    /**
     * The value of node.
     */
    char value;

    /**
     * 二叉树的左孩子。
     */
    BinaryCharTree leftChild;
    /**
     * 二叉树的右孩子。
     */
    BinaryCharTree rightChild;

    /**
     **************
     * The first constructor.
     * 
     * @param paraValue The given value.
     **************
     */
    public BinaryCharTree(char paraValue) {
        value = paraValue;
        leftChild = null;
        rightChild = null;
    } // Of the first construe

    public static BinaryCharTree manualConstructTree() {
        // step 1. Creat root.
        BinaryCharTree resultTree = new BinaryCharTree('a');

        // step 2. creat children and linked them.
        BinaryCharTree tempTreeB = new BinaryCharTree('b');
        BinaryCharTree tempTreeC = new BinaryCharTree('c');
        BinaryCharTree tempTreeD = new BinaryCharTree('d');
        BinaryCharTree tempTreeE = new BinaryCharTree('e');
        BinaryCharTree tempTreeF = new BinaryCharTree('f');
        BinaryCharTree tempTreeG = new BinaryCharTree('g');
        // Link strat
        resultTree.leftChild = tempTreeB;
        resultTree.rightChild = tempTreeC;
        tempTreeB.rightChild = tempTreeD;
        tempTreeC.leftChild = tempTreeE;
        tempTreeD.leftChild = tempTreeF;
        tempTreeD.rightChild = tempTreeG;

        return resultTree;
    } // Of manualConstructTree

    /**
     **************
     * 先根、中根、后根遍历树。
     **************
     */
    public void preOrderVisit() {
        System.out.print(value + " ");

        if (leftChild != null) {
            leftChild.preOrderVisit();
        } // Of if

        if (rightChild != null) {
            rightChild.preOrderVisit();
        } // Of if
    } // Of preOrderVisit

    public void inOrderVisit() {
        if (leftChild != null) {
            leftChild.inOrderVisit();
        } // Of if

        System.out.print(value + " ");

        if (rightChild != null) {
            rightChild.inOrderVisit();
        } // Of if
    }

    public void postOrderVisit() {
        if (leftChild != null) {
            leftChild.postOrderVisit();
        } // Of if

        if (rightChild != null) {
            rightChild.postOrderVisit();
        } // Of if

        System.out.print(value + " ");
    }

    public int getDepth() {
        if ((leftChild == null) && (rightChild == null)) {
            return 1;
        } // Of if

        // check left
        int tempLeftDepth = 0;
        if (leftChild != null) {
            tempLeftDepth = leftChild.getDepth();
        } // Of if

        // Check right
        int tempRightDepth = 0;
        if (rightChild != null) {
            tempRightDepth = rightChild.getDepth();
        } // Of if

        if (tempLeftDepth >= tempRightDepth) {
            return tempLeftDepth + 1;
        } else {
            return tempRightDepth + 1;
        } // Of if
    } // Of getDepth

    // 遍历时存储节点中的值。
    char[] valueArray;
    // 二叉树的索引。
    int[] indicesArray;

    public void toDataArrays() {
        int tempLength = getNumNodes();

        valueArray = new char[tempLength];
        indicesArray = new int[tempLength];
        int i = 0;

        // Traverse and convert at the same time.
		CircleObjectQueue tempQueue = new CircleObjectQueue();
		tempQueue.enQueue(this);
		CircleObjectQueue tempIntQueue = new CircleObjectQueue();
		tempIntQueue.enQueue(0);
 
		BinaryCharTree tempTree = (BinaryCharTree) tempQueue.deQueue();
		int tempIndex = (int) tempIntQueue.deQueue();
		while (tempTree != null) {
			valueArray[i] = tempTree.value;
			indicesArray[i] = tempIndex;
			i++;
 
			if (tempTree.leftChild != null) {
				tempQueue.enQueue(tempTree.leftChild);
				tempIntQueue.enQueue(tempIndex * 2 + 1);
			} // Of if
 
			if (tempTree.rightChild != null) {
				tempQueue.enQueue(tempTree.rightChild);
				tempIntQueue.enQueue(tempIndex * 2 + 2);
			} // Of if
 
			tempTree = (BinaryCharTree) tempQueue.deQueue();
			if (tempTree != null)
				tempIndex = (int) tempIntQueue.deQueue();
		} // Of while
    }



    public int getNumNodes() {
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		} // Of if

		int tempLeftNodes = 0;
		if (leftChild != null) {
			tempLeftNodes = leftChild.getNumNodes();
		} // Of if

		int tempRightNodes = 0;
		if (rightChild != null) {
			tempRightNodes = rightChild.getNumNodes();
		} // Of if

		return tempLeftNodes + tempRightNodes + 1;
	}// Of getNumNodes

	/**
	 *********************
	 * The entrance of the program.
	 * 
	 * @param args Not used now.
	 *********************
	 */
	public static void main(String args[]) {
		BinaryCharTree tempTree = manualConstructTree();
		System.out.println("\r\nPreorder visit:");
		tempTree.preOrderVisit();
		System.out.println("\r\nIn-order visit:");
		tempTree.inOrderVisit();
		System.out.println("\r\nPost-order visit:");
		tempTree.postOrderVisit();

		System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth());
		System.out.println("The number of nodes is: " + tempTree.getNumNodes());

        tempTree.toDataArrays();
        System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
		System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
	}// Of main
}

运行结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值