Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » Mehrere Struct verbinden ?

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
16.04.2004, 20:18 Uhr
~Gast
Gast


Hallo,
hab folgende Aufgabe:
Ich muß mehrere Struct per Pointer miteinander verbinden. Die Struct Elemente sind wie folgt aufgebaut (Knoten):

int knoten.id
char *pointer_links
char *pointer_rechts

Ich soll jetzt ein Programm erstellen mit dessen Hilfe ein beliebiges Geflecht erstellt werden kann (dynamisch)!

z.B.

1 (root)
/ \
4 3
/ /
7 6
/ \
9 10

Mehr Infos hab ich dazu leider nicht! Hat ma jemand en Tipp für mich wie ich das angehe oder da ma starte ? Hab kein Plan...

Danke!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
16.04.2004, 20:47 Uhr
(un)wissender
Niveauwart


Binärer Baum, wie?
Such mal danach, ist eine Standardaufgabe, hier solltest du zumindest Ansätze bieten können, oder konkrete Fragen haben.

Tipp:
Wenn das Kind (links oder rechts) nicht mehr vorhanden ist, dann Pointer auf null setzen.
--
Wer früher stirbt ist länger tot.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
16.04.2004, 20:56 Uhr
mike
Pinguinhüpfer
(Operator)


Hi!
Ja das geht.
Dazu musst du ein struct erstellen. Z.b. mit einer char Information

C++:
struct tnode {
   char *word;
   int count;
   struct tnode *left;
    struct tnode *right;
}:


Also nächstes musst du das Element einfügen:

C++:
struct tnode *addtree(struct tnode *p, char *w)
{
   int cond;
   if(p == NULL){
      t = tallaoc();
      p->word = strdup(w);
      p->count = 1;
      p->left = p->right = NULL;
   } else if ((cond == strcmp(w, p->word)) == 0)
      p->count++;
   else if(cond<0)
      p->left = addtree(p->left,w);
   else
      p->right = addtree(p->right, w);
  return p;
}

struct tnode *talloc(void)
{
   return (struct tnode *)malloc(sizeof(struct tnode));
}


Ausgegeben wird das ganze mit folgendem Src:

C++:
void treeprint(struct tnode *p)
{
   if(p != NULL){
      treeprint(p->left);
      printf("%4d %s\n",p->count, p->word);
      treeprint(p->right);
   }
}



So müsste es funtzen
mfg
PS: uje. Ich bin immer zu langsam
PSS: und Angabe schlecht gelesen. Doppel 5
--

Dieser Post wurde am 16.04.2004 um 21:00 Uhr von mike editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
003
17.04.2004, 00:10 Uhr
(un)wissender
Niveauwart


Ein rudimentärer C++-Ansatz.
Wer Fehler findet, darf sie melden.
Schöner wäre es noch, BinaryNode nur intern im Tree zu verwenden und std::pair alles Schlüssel-Wertepaar zu nehmen.



C++:
#include <iostream>

template <typename K, typename T>
class BinaryNode
{
public:
    BinaryNode(const K &key)
    : key_(key), value_(), leftChild_(0), rightChild_(0) {}
    BinaryNode(const K &key, const T &value)
    : key_(key), value_(value), leftChild_(0), rightChild_(0) {}
    
    void setLeftChild(BinaryNode<K,T> * leftChild)
    { leftChild_ = leftChild;}
    
    void setRightChild(BinaryNode<K,T> * rightChild)
    { rightChild_ = rightChild;}
    
    const BinaryNode<K,T> * getLeftChild() const
    { return leftChild_;}
    
    const BinaryNode<K,T> * getRightChild() const
    { return rightChild_;}
    
    void setValue(const T& value)
    { value_ = value; }
    
    const T& getValue() const
    { return value_; }
    
    void setKey(const K &key)
    { key_ = key; }
    
    const T& getKey() const
    { return key_; }
        
    bool operator< (const BinaryNode<K,T> &otherNode) const
    { return key_ < otherNode.key_; }
    
    bool operator> (const BinaryNode<K,T> &otherNode) const
    { return key_ > otherNode.key_; }
                    
private:      
    K key_;
    T value_;
    BinaryNode<K,T> *leftChild_;
    BinaryNode<K,T> *rightChild_;
};

template<typename T, typename K>
std::ostream & operator<<(std::ostream &out, const BinaryNode<K,T> &node)
{ return out << node.getValue(); }


template<typename K, typename T>
class BinaryTree
{
public:
    BinaryTree();
    BinaryTree(const BinaryTree<K,T> &node);
    BinaryTree<K,T> & operator=(const BinaryTree<K,T> &node);
    ~BinaryTree();
    
    void insert(const K& key, const T& value);
    void insert(const BinaryNode<K,T> &node);
    void erase(const K& key);
    void erase(const BinaryNode<K,T> &node);
    const BinaryNode<K,T> * find(const K& key) const;
    BinaryNode<K,T> * find(const K& key);
    
    //Ascii-art :)
    void printTree() const;
    
private:
    //Baum ausbalancieren, kann sehr aufwendig sein!
    void makeBalancedTree();
    //z.B rekuriv Baum durchsuchen: preorder, postorder...
    BinaryNode<K,T> * internalFind(const K& key) const;

    BinaryNode<K,T> *root;
};



--
Wer früher stirbt ist länger tot.

Dieser Post wurde am 17.04.2004 um 00:17 Uhr von (un)wissender editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
004
17.04.2004, 08:03 Uhr
Pablo
Supertux
(Operator)


Willst du das in C oder C++ machen?
--
A! Elbereth Gilthoniel!
silivren penna míriel
o menel aglar elenath,
Gilthoniel, A! Elbereth!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
005
17.04.2004, 11:34 Uhr
~Gast
Gast


Erstmal Danke für Eure Antworten.
Der Code soll in C sein und ich hätte vielleicht noch dazu schreiben sollen das ich eher Anfänger bin. Das sieht schon alles recht komplex aus. Aber da werd ich mich wohl durchkämpfen müssen.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
006
17.04.2004, 21:39 Uhr
Pablo
Supertux
(Operator)


ok, dann ist wissenders Vorschlag nicht benutzbar, da es C++ ist und sowas nicht mit einem C Compiler kompilieren kannst. ich mcahe etwas einfaches: (Ohne zu testen, Anpassungen sind erfordelich)


C++:
typedef struct T_NODE {
    int val;
    struct T_NODE* left; // wenn NULL ==> Blatt
    struct T_NODE* right;
} NODE;
typedef struct T_TREE {
    NODE* root;
} TREE;



Ich werde das nachher selber programmieren, ich habe jetzt ein wenig zu tun.
--
A! Elbereth Gilthoniel!
silivren penna míriel
o menel aglar elenath,
Gilthoniel, A! Elbereth!
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: