算法导论第十章练习参考答案

10.1-5 栈插入和删除元素只能在同一端进行,队列的插入操作和删除操作分别在两端进行,与他们不同的,有一种双端队列(deque),其插入操作和删除都可以在两端进行。写出4个时间均为 O ( 1 ) O(1) 的过程,分别实现在双端队列的两端插入和删除元素的操作,该队列是用一个数组实现的。

初始化时, Q . h e a d Q.head 指向队头元素, Q . t a i l Q.tail 指向队尾元素的下一个元素, n n 是队列数组长度。当 Q . h e a d = Q . t a i l Q.head=Q.tail 时队列为空。 Q . h e a d = 1 Q.head=1 Q . t a i l = n + 1 Q.tail=n+1 时队列满。规定 Q . h e a d = 1 Q.head=1 时不能在队头插入元素。 Q . t a i l = n + 1 Q.tail=n+1 时队尾不能插入元素。

伪代码:

//队头插入
PUSHFRONT(Q,x)
if Q.head == 1
	error "overflow"
if Q.head == Q.tail
	Q.tail++
else
	Q.head--
Q[Q.head] = x

//队头删除
POPFRONT(Q)
if Q.head == Q.tail
	error "underflow"
Q.head = Q.head + 1
return Q[Q.head-1]

//队尾插入
PUSHBACK(Q,x)
if Q.tail == n+1
	error "overflow"
Q[Q.tail] = x
Q.tail = Q.tail + 1

//队尾删除
POPBACK(Q,x)
if Q.head==Q.tail
	error "underflow"
Q.tail = Q.tail - 1
return Q[Q.tail]

java代码

class Deque{
    static final int QUEUESIZE = 10;
    int A[] = new int[QUEUESIZE+1];
    int head = 1,tail = 1;

    public void pushFront(int x) throws OverflowException {
        if(head==1)
            throw new OverflowException();
        if(head==tail)
            tail ++;
        else
            head--;
        A[head] = x;
    }
    public int popFront() throws UnderflowException {
        if(head==tail)
            throw new UnderflowException();
        head++;
        return A[head-1];
    }
    //队尾插入
    public void pushBack(int x) throws OverflowException {
        if(tail == QUEUESIZE+1)
            throw new OverflowException();
        A[tail] = x;
        tail++;
    }
    //队尾删除
    public int popBack() throws UnderflowException {
        if(head==tail)
            throw new UnderflowException();
        tail--;
        return A[tail];
    }
    int [] getValue(){
        if(head==tail)
            return null;
        int [] res = new int[tail-head];
        for(int i = head ;i<tail;i++)
            res[i-head] = A[head];
        return res;
    }
}
class OverflowException extends Exception{

}
class UnderflowException extends Exception{

}

10.4-3 给定一个 n n 个结点的二叉树,写出一个 O ( n ) O(n) 时间的非递归过程,将该树每个节点的关键字输出。可以使用一个栈作为辅助数据结构。

public class E10_4_3 {
    class Node {
        Node left;
        Node right;
        int value;

        public Node(Node left, Node right, int value) {
            this.left = left;
            this.right = right;
            this.value = value;
        }

        public Node(int value) {
            this.value = value;
        }
    }
    public void visit(Node n){
        System.out.println(n.value);
    }
    public void trans(Node root){
        Stack<Node> s = new Stack<>();
        Node x = root;
        while(true){
            visit(x);
            if(x.right!=null)
                s.push(x.right);
            if(s.empty()) break;
            if(x.left!=null)
                x = x.left;
            else
                x = s.pop();
        }
    }

    @Test
    public void testTrans(){
        Node a = new Node(4);
        Node b = new Node(3);
        Node root = new Node(a,b,1);
        Node c = new Node(2);
        a.left = c;
        new E10_4_3().trans(root);
        //output 1 4 2 3
    }
}

10.4-4 对于一个含 n n 个结点的任意有根树,写出一个 O ( n ) O(n) 时间的过程,输出其所有关键字,该树以左孩子右兄弟表示法存储。

先序遍历即可

10.4-5 给定一个 n n 结点的二叉树,写出一个 O ( n ) O(n) 时间的非递归过程,将该树每个节点的关键字输出。要求除该树本身的存储空间外只能使用固定量的额外存储空间,且在过程中不得修改该树,即使是暂时的修改也不允许。

发布了5 篇原创文章 · 获赞 0 · 访问量 147

猜你喜欢

转载自blog.csdn.net/suchvaliant/article/details/105718457
今日推荐