AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

Marge Binar Tree

Offene Frage von "sk.Silvia"
Ein Thema von sk.Silvia · begonnen am 14. Apr 2006 · letzter Beitrag vom 14. Apr 2006
Antwort Antwort
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#1

Marge Binar Tree

  Alt 14. Apr 2006, 12:59
hi there i have a problem :

so i have two tree structures New (has value 77 in left upper corner) and BinTree (is the large tree) are declared as standard tree
Delphi-Quellcode:
type TBinTree=class
      Value:integer;
      Left,Right: TBinTree;
      constructor Create(New_Value:integer; New_Left, New_Right:TBinTree); Overload;
      constructor PowerCreate(Node:TBinTree); Overload;
      ...
      end;

  constructor TBinTree.Create(New_Value:integer; New_Left, New_Right:TBinTree);
      begin
      Value:=New_Value;
      Left:=New_Left;
      Right:=New_Right;
      end;

  constructor TBinTree.PowerCreate(Node:TBinTree);
      begin
      Self:=Node;
      end;
und ich mochte jetzt den ganzen rechten teil (.Right) von BinTree Baum mit Value=1 in den rechten teil von New baum tree stecken (2)) und dann diesen grossen New in den rechten teil (.Right) von BinTree mit Value=1 stecken, alzo eine neue node mit value=77 zwichen value=1 und value=2 erstelen, und dann will ich aber frei mit dem New tree arbeiten (wenn ich zumbeispiel New andere, mochte ich net das auch die node in BinTree geendert wird), weiss aber nich wie ich es machen soll sodas ich nicht die adresse von New in := aber eine neue node erstelle

Delphi-Quellcode:
  
procedure TEngine.Insert;
var X:TBinTree;
      begin
      X:=TBinTree.PowerCreate(NewNode);
      X.Right:=TBinTree.PowerCreate(BinTree.IsOverParentLine(Selected).Right); //BinTree.IsOverParentLine(Selected)=> Value=1
      BinTree.IsOverParentLine(Selected).Right:=TBinTree.PowerCreate(X); //ako precerpat celu nodu aj s pamatov
      end;
das geht aber nich denn immer bleibt die New node mit der node in BinTree verbunden

http://www.kolaudacia.sk/bintree.gif
  Mit Zitat antworten Zitat
Der_Unwissende

Registriert seit: 13. Dez 2003
Ort: Berlin
1.756 Beiträge
 
#2

Re: Marge Binar Tree

  Alt 14. Apr 2006, 17:00
Hi,
excuse me, I don't know if I get you right.
Well do you want to merge some trees (lets call them t1, t2, ... tn) to a new one (tNew). But although tNew contains the nodes of t1, t2, ..., tn you want independed changes (so changing t1 does not correspond in a change of nodes in tNew)?

If so, you can't just use references (light copies by := ) but you have to make deep copies (by creating new nodes with given values).

Well of course just if I get what your problem is

Regards Der Unwissende
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#3

Re: Marge Binar Tree

  Alt 14. Apr 2006, 19:24
ok, lets say that t1 has only left childrens and t2 only right childrens, i want marge up t2 to t1.right so t1 is a compleate tree, but t2 stays as before, and by acting with right childrens of t1 i will act only them, not also t2...so better to say, i will insert one tree to another but i will that the inserted tree will stay separate

lets say i have a main tree called t1 with many childrens and parents...then i have a tree caled New (better to say its a node, that contains no children - the whole tree is consists only from one item)...and now i will add this New (tree-node) to t1 tree (between a parent and a child)...i was consluding it like this

will insert New between Parent and Child ->
- i set the nil child of New to Child of the Parent
- i set New as the child of the Parent (New includes the whole old Child (tree))

Thats fine, but the inserted node stays connected to New node also when i construct it, when i Set New to nil, also the inserted node (once new) set itself to nil
  Mit Zitat antworten Zitat
marabu

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

Re: Marge Binar Tree

  Alt 14. Apr 2006, 19:39
Hi Silvia,

to improve on the semantics of your code you could name your class TBinaryTreeNode - it is a node after all and only aggregating some nodes will produce a binary tree.

Since you stress the decoupling of the new node from its tree-instance, this seems not to be a coding exercise in balanced binary trees. So I present some code for you to study, that in fact is focusing on the forementioned thought of a deep copy:

Delphi-Quellcode:
unit BinTree;

interface

type
  PBinaryTreeNode = ^TBinaryTreeNode;
  TBinaryTreeNode = class
  private
    FValue: Integer;
    FLeft: TBinaryTreeNode;
    FRight: TBinaryTreeNode;
  public
    constructor Create(Value: Integer);
    function Clone(deep: Boolean = false): TBinaryTreeNode;
    property Value: Integer read FValue;
    property Left: TBinaryTreeNode read FLeft write FLeft;
    property Right: TBinaryTreeNode read FRight write FRight;
  end;

implementation

constructor TBinaryTreeNode.Create(Value: Integer);
begin
  inherited Create;
  FValue := Value;
end;

function TBinaryTreeNode.Clone(deep: Boolean = false): TBinaryTreeNode;
begin
  Result := TBinaryTreeNode.Create(FValue);
  if deep then
  begin
    if Assigned(FLeft) then
      Result.Left := FLeft.Clone(true);
    if Assigned(FRight) then
      Result.Right := FRight.Clone(true);
  end;
end;

end.
To insert a copy of node 77 you could use the following lines of code:

Delphi-Quellcode:
var
  bt77, bt1, bt77Clone: TBinaryTreeNode;

procedure TDemoForm.ButtonClick(Sender: TObject);
begin
  bt77 := TBinaryTreeNode.Create(77);
  bt77Clone := bt77.Clone();
  bt77Clone.Right := bt1.Right;
  bt1.Right := bt77Clone;
end;
Kind regards

marabu
  Mit Zitat antworten Zitat
Benutzerbild von sk.Silvia
sk.Silvia

Registriert seit: 8. Feb 2006
Ort: Slovenia
90 Beiträge
 
Delphi 7 Personal
 
#5

Re: Marge Binar Tree

  Alt 14. Apr 2006, 20:03
thanks for your code maribu, is i understood Clone Clone (Creates based on same value) a new node but not only the node, but it also Clone all his childrens and childrens of their childrens...cool...i will find a great use for it when extracting (cloning a part) subtree...


i solded my problems follows

i thought that by using

consturctor PowerCreate(Node:TBinTree);
begin
self=node;
end;

delphi will create a new place in memory cause its in constructor, not only link memory //i thought it creates space for variables included in self and link only to the next dynamic variable, instate of linking all
  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 12:18 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