day25--二叉树深度遍历的栈实现 (中序)

1.定义具有通用性的对象栈,依靠强制类型转换, 支持不同的数据类型.。
对象栈代码:

package com.two;

public class ObjectStack {

    public static final int MAX_DEPTH = 10;// the depth

    int depth;// the actual depth.

    Object[] data;// the data;

    public ObjectStack() {
        depth = 0;
        data = new Object[MAX_DEPTH];
    }// of constructor

    public String toString() {
        String resultString = "";
        for (int i = 0; i < depth; i++) {
            resultString += data[i];
        }// of for i
        return resultString;
    }// of toString

    /**
     * push an element
     * @param paraObject
     *          The given object.
     * @return success or not
     */
    public boolean push(Object paraObject) {
        if (depth == MAX_DEPTH) {
            System.out.println("stack full.");
            return false;
        }// of if
        data[depth] = paraObject;
        depth++;

        return true;
    }// of push

    /**
     * pop an elememt
     * @return the object at the top of the stack.
     */
    public Object pop() {
        if (depth == 0) {
            System.out.println("Nothing to pop.");
            return '\0';
        } // of if

        Object resultObject = data[depth - 1];
        depth--;

        return resultObject;
    }// Of pop

    public boolean isEmpty() {
        if (depth == 0) {
            return true;
        }// of if
        return false;
    }// of isEmpty

    public static void main(String[] args) {
        ObjectStack tempStack = new ObjectStack();

        for (char ch = 'a'; ch < 'm'; ch++) {
            tempStack.push(new Character(ch));
            System.out.println("The current stack is: " + tempStack);

        }// of for ch

        char tempChar;
        for (int i = 0; i < 12; i++) {
            tempChar = ((Character)tempStack.pop()).charValue();
            System.out.println("Poped: " + tempChar);
            System.out.println("The current stack is: " + tempStack);
        }// of for i
    }// of main
}//Of class ObjectStack

测试对象栈结果:

The current stack is: a
The current stack is: ab
The current stack is: abc
The current stack is: abcd
The current stack is: abcde
The current stack is: abcdef
The current stack is: abcdefg
The current stack is: abcdefgh
The current stack is: abcdefghi
The current stack is: abcdefghij
stack full.
The current stack is: abcdefghij
stack full.
The current stack is: abcdefghij
Poped: j
The current stack is: abcdefghi
Poped: i
The current stack is: abcdefgh
Poped: h
The current stack is: abcdefg
Poped: g
The current stack is: abcdef
Poped: f
The current stack is: abcde
Poped: e
The current stack is: abcd
Poped: d
The current stack is: abc
Poped: c
The current stack is: ab
Poped: b
The current stack is: a
Poped: a
The current stack is: 
Nothing to pop.
Poped:  
The current stack is: 
Nothing to pop.
Poped:  
The current stack is: 

2.中序遍历代码:

 public void inOrderVisitWithStack() {
        ObjectStack tempStack = new ObjectStack();
        BinaryCharTree tempNode = this;
        while (!tempStack.isEmpty() || tempNode != null) {
            if (tempNode != null) {
                tempStack.push(tempNode);
                tempNode = tempNode.leftChild;
            } else {
                tempNode = (BinaryCharTree) tempStack.pop();
                System.out.println("" + tempNode.value + " ");
                tempNode = tempNode.rightChild;
            }// of if
        }// of while
    }// of inOrderVisitWithStack

测试用例:


        char[] tempCharArray = {'A','B','C','D','E','F'};
        int[] tempIndicesArray = {0,1,2,4,5,12};
        BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray,tempIndicesArray);
       
        System.out.println("\r\nIn-order visit with stack:");
        tempTree2.inOrderVisitWithStack();

运行结果:

In-order visit with stack:
B 
D 
A 
E 
F 
C 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值