AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Sprachen und Entwicklungsumgebungen Sonstige Fragen zu Delphi Delphi ADS - Abstract Data Structure: Union-Find
Thema durchsuchen
Ansicht
Themen-Optionen

ADS - Abstract Data Structure: Union-Find

Ein Thema von Janulka · begonnen am 13. Dez 2006 · letzter Beitrag vom 13. Dez 2006
Antwort Antwort
Janulka

Registriert seit: 13. Dez 2006
1 Beiträge
 
#1

ADS - Abstract Data Structure: Union-Find

  Alt 13. Dez 2006, 10:40
Hallo!
Could someone please help me? I need to program ADS union-find in Delphi. How could I add nodes in a tree, which has this structure:
1. Every item is in a tree.
2. The root of the tree is the representative item of all items in that tree. i.e., the root of the tree represents the whole items.
3. In this up-tree implementation, every node (except the root) has a pointer pointing to its parent.
The root element has a pointer pointing to itself (or nil).

Delphi-Quellcode:
___________
interface :
___________

    TNode = class
       parent : TNode;
        value : integer;
        constructor Create(i : integer);
    end;

    TTree = class
       root : TNode;
        constructor Create(i : integer);
        procedure add(item : integer);
    end;

    TUnionFind = class
       root : TTree;
        parent : TUnionFind;
        size : integer;
        name : String;
        constructor Create(s : string);
    end;

function Union(X, Y : TUnionFind) : TUnionFind;
_________________
implementation :
_________________

    function Find_Set(X : TUnionFind) : TUnionFind;
   begin
       while (X <> X.parent) do
           X := X.parent;
        Result := X;
    end;
                
   function Union(X, Y : TUnionFind) : TUnionFind;
    var
       A, B : TUnionFind;
    begin
       A := Find_Set(X);
        B := Find_Set(Y);
        if (A.size <= B.size) then
        begin
           if (A.size = B.size) then
               inc(B.size);
           A.parent := b;
            Result := A;
        end else
        begin
           B.parent := A;
            Result := B;
        end;
    end;
Danke schoen

[edit=SirThornberry]Delphi-Tags korrigiert - Mfg, SirThornberry[/edit]
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#2

Re: ADS - Abstract Data Structure: Union-Find

  Alt 13. Dez 2006, 12:48
Hi Janulka,

welcome to Delphi-PRAXiS.

Your problem looks like an assignment and your question makes me wonder if you ever heard of union-find structures before. Those code lines are yours? Usually, there is no method named Add(), because the addition of a new node to a tree is accomplished by union. So where does Add() come from in your tree class?

Kind regards
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 14:17 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz