home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sunic!dkuug!daimi!u912080
- From: u912080@daimi.aau.dk (Rune Bang Lyngs|)
- Newsgroups: comp.lang.pascal
- Subject: Help! Dispose pointers in a tree
- Message-ID: <1992Aug19.015706.29728@daimi.aau.dk>
- Date: 19 Aug 92 01:57:06 GMT
- Sender: u912080@daimi.aau.dk (Rune Bang Lyngs|)
- Organization: DAIMI: Computer Science Department, Aarhus University, Denmark
- Lines: 139
-
-
- The following program should make a binartree.
- Get it's height and start over again.
-
- My problem is that the procedure DelTree maybe not delete the whole tree
- (all poineters) as it should.
- Anyway the heap-memory is being eating up.
- If I write tree 200 2000 I get a heap-overflow after some time - why?
- And if I write tree 20000 4 crash - why?
- The solution are simple - I believe that but please help me!!!!
-
-
-
-
-
-
-
-
- Program binaertrae;
- Uses crt;
-
- Type
- TreePointer = ^Tree;
- Tree = Record
- Element : Word;
- left : Treepointer;
- right : Treepointer;
- End;
-
- Var
- P : TreePointer;
- Taeller,i,j : Word;
- hs,ss : Longint;
- Kode : Word;
- Antal:Word;
- ghs: real;
-
- Function Max(x,y:Word):Word;
- Begin
- if x>y then Max:=x else Max:=y;
- End;
-
- Procedure Init(Var TP: TreePointer);
- Begin
- New(TP);
- TP:=NIL;
- End;
-
- Procedure Insert(Var TP: TreePointer;Element: Word;Var a:Word);
- Var
- P : TreePointer;
-
- Procedure Insert2(Var TP : TreePointer;EP: TreePointer;Var a:Word);
- Begin
- IF EP^.Element=TP^.Element Then exit;
- If EP^.Element<TP^.Element Then
- If TP^.Left=NIL Then
- Begin
- a:=a+1;
- TP^.Left:=EP;
- End
- else Insert2(TP^.Left,EP,a)
- else
- If TP^.Right=NIL Then
- Begin
- a:=a+1;
- TP^.Right:=EP;
- End
- else Insert2(TP^.Right,EP,a);
- End;
-
- Begin
- New(P);
- P^.Element:=Element;
- P^.Left:=NIL;
- P^.Right:=NIL;
- If TP=NIL Then
- TP:=P
- else
- Insert2(TP,P,a);
- End;
-
- Procedure Print(Var TP : Treepointer);
- Begin
- If TP=NIL then exit;
- Print(TP^.Left);
- Write(TP^.Element:8);
- Print(TP^.Right);
- End;
-
- Function Height(Var TP : TreePointer):Word;
- Begin
- If TP=NIL then
- Begin
- Height:=0;
- End
- else
- Height:=1+Max(Height(TP^.Left),Height(TP^.Right));
- End;
-
- Procedure DelTree(Var Point : TreePointer);
- Begin
- If Point=NIL then exit;
- If Point^.Left<>NIL then DelTree(Point^.Left);
- If Point^.Right<>NIL then DelTree(Point^.Right);
- dispose(Point);
- End;
-
- Begin
- hs:=0;
- ss:=0;
- Val(Paramstr(2),i,kode);
- If ParamCount<2 then exit;
- for j:=1 to i do
- Begin
- Val(Paramstr(1),antal,kode);
- If Paramcount=0 then antal:=10000;
- kode:=1;
- randomize;
- Init(P);
- For taeller:=1 to Antal do
- Begin
- Insert(P,random(65535),kode);
- End;
- {
- Print(P);
- }
- hs:=hs+height(p);
- inc(hs);
- ss:=ss+kode;
- ghs:=1+hs/j;
- DelTree(p);
- clrscr;
- writeln('Average elements=',(ss/j):5:1);
- writeln('Average height=',(ghs):3:2);
- writeln('Loop number:',j);
- writeln('MemAvail=',MemAvail);
- end;
- End.
-