Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Having problem in creating tree

Having problem in creating tree

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 1.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kitten
    wrote on last edited by
    #1

    hi to everyone

    i want to create a tree with lots of children
    i write this code:
    @class Tree
    {
    friend class AIClass;
    int v;
    int **board;
    QList<Tree> *children;
    public:

    Tree()
    {
        v = 0;
        int n = 25;
        board=(int**) malloc((n)*sizeof(int*));
        for(int i=0;i<n;i++)
        {
            board[i]=(int*) malloc((n)*sizeof(int));
            for(int j=0;j<n;j++)
                board[i][j]=0;
        }
        children = new QList<Tree>();
    }
    
    Tree(int n)
    {
        v = 0;
        board=(int**) malloc((n)*sizeof(int*));
        for(int i=0;i<n;i++)
        {
            board[i]=(int*) malloc((n)*sizeof(int));
            for(int j=0;j<n;j++)
                board[i][j]=0;
        }
        children = new QList<Tree>();
    }
    void setxy(int x,int y,int value)
    {
        board[x][y] = value;
    }
    
    void setValue(int p)
    {
        v = p;
    }
    

    };
    class AIClass : public QObject
    {
    public:
    int predict( Tree *item,int depth,int alpha,int beta,bool Player);

    private:
    Tree *root;
    int board[25][25];
    int count;
    int k;
    };
    @
    and i wrote this:
    @
    int AIClass::predict(Tree *item,int depth,int alpha,int beta,bool MaxPlayer)
    {
    for(int i=0;i<count;i++)
    {
    for(int j=0;j<count;j++)
    {
    if(item->board[i][j]==0) {
    temp = new Tree(count);
    for(int k=0;k<count;k++)
    for(int h=0;h<count;h++)
    {
    temp->setxy(k,h,item->board[k][h]);
    if((i==k)&&(j==h)) temp->setxy(k,h,2);
    }
    item->children->append(temp);
    }
    }
    }
    for (int i = 0; i < item->children->size(); ++i) {
    int temp = predict(&(item->children->value(i)),depth-1,alpha,beta,!MaxPlayer);
    if(temp > alpha)
    alpha = temp;
    if(beta < alpha) break;
    }
    item->setValue(alpha);
    }@
    ( it is a part of alpha beta pruning algorithm)
    but when it is compiled,the children does'nt get its value,after calling predict function,and again the v value in Tree class is 0 and does'nt get updated value
    and i get this warning message:(i think the problem is):
    @..\dooz\src\AIClass.cpp: In member function 'int AIClass::predict(Tree
    , int, int, int, bool)':
    ..\dooz\src\AIClass.cpp:98: warning: taking address of temporary@
    what do you think?

    www.kitten.mihanblog.com

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      What line in your posted code does line AIClass.cpp:98 translate to?
      And where does temp[m] come from in line 8 of AIClass::predict? Why do you redefine it to an int in line 21? And is temp[m] memory leaked (you deep-copy by value when appending * temp[m] to the list. Maybe you want a QList<Tree*> instead)?
      Is children memory leaked? Where's the dtor deleting children?
      Why do you mix malloc and new?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        kitten
        wrote on last edited by
        #3

        thanks alot i found the problem
        i need QList<Tree *>

        www.kitten.mihanblog.com

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved