1

我不是一个完整的新手,所以我理解它的含义等,但我似乎无法弄清楚 Eclipse 似乎认为我缺少什么神秘的括号。

(忽略我还没有构建我所说的我会在我的代码中构建的事实——我还没有走到那一步。)

#include "./BinaryTree.h"
using namespace ods;
typedef BTNode1 Node;
Node * buildNode(Node *p = NULL, Node *l = NULL, Node *r = NULL) {
    Node * ans = new Node;
    ans->parent = p; ans->left = l; ans->right = r;
    return ans;
}

typedef BTNode1 Node;
Node * buildNodeComplete(Node *p = NULL, Node*l = NULL, Node *r = NULL) {
    Node * ans = new Node;
    ans->parent = p; ans->left = l; ans->right = r;
    return ans;
}

typedef BTNode1 Node;
Node * buildNodeStringy(Node *p = NULL, Node*l = NULL, Node *r = NULL) {
    Node * ans = new Node;
    ans->parent = p; ans->left = l; ans->right = r;
    return ans;
}

int main() {
    // build random tree of 1023 nodes
    BinaryTree<Node> T;
    Node *u;
    T.root() = buildNode();
    int d;

    for (int i = 1; i < 1023; ++i) {
        T.randomWalk(u, d);
        if (d == 0) u->left = buildNode(u);
        else u->right = buildNode(u);
    }

    //build complete tree of 1023 nodes
    BinaryTree<BTNode1> C;
    Node *a;
    C.root() = buildNodeComplete();
    int g;

    for (int i = 1; i <1023; i++) {
        C.randomWalk(a, g);
        if (g == 0) a->left = buildNodeComplete(a);
        else a->right = buildNodeComplete(a);
    }

    //build stringy tree of 1023 nodes
    BinaryTree<Node> S;
    Node *q;
    S.root() = buildNodeStringy();
    int v;

    for (int i = 1; i <1023; i++) {
         S.randomWalk(q, v);
         if (v == 0) q->left = buildNodeStringy(q);
         else q->right = buildNodeStringy(q);
    }

    // measure random walks in T
    double sum=0;
    double sum1=0;
    double sum2=0;
    for (int i = 0; i < 1000; ++i) {
        T.randomWalk(u, d);
        sum += T.depth(u);
        C.randomWalk(a, g);
        sum1 += C.depth(a);
        S.randomWalk(q, v);
        sum2 += S.depth(q);
    }

    std::cout << "Average length of random walk through arbitrary tree is "
              << sum/1000;
    std::cout << " and lg(n+1) = lg(1024) = 10."
              << std::endl;

   //lab 5 part b
    std::cout << "Average length of random walk through complete tree is "
              << sum1/1000 << endl;
    std::cout << "Average length of random walk through stringy tree is "
              << sum2/1000;

    return 0;
}
4

1 回答 1

0

据说Eclipse 中的这个错误已修复,但尝试在右括号后添加一个新行。过去需要在 .cpp 文件的末尾有一个换行符,但现在实际上没有必要了。

此外,如果我曾经见过,这看起来像是一个数据结构类分配,所以祝你好运(它变得更难之前变得更容易。)

于 2013-10-10T22:56:30.720 回答