From 99b038a06d39ab0c470d51230e1af00b1950c706 Mon Sep 17 00:00:00 2001 From: Ioulianos Kakoulidis Date: Tue, 16 Apr 2019 12:53:27 +0300 Subject: [PATCH] Min, Max --- docs/AllClasses.html | 5 + docs/AllIdentifiers.html | 5 + docs/ClassHierarchy.html | 2 +- docs/ftreap.TImplicitTreapNode.html | 277 ++++++++++++++++++++++++++++ docs/ftreap.TTreapNode.html | 64 +++++-- docs/ftreap.html | 4 + ftreap.pas | 22 +++ test/treaptestcase.pas | 4 +- 8 files changed, 367 insertions(+), 16 deletions(-) create mode 100644 docs/ftreap.TImplicitTreapNode.html diff --git a/docs/AllClasses.html b/docs/AllClasses.html index cbc5ec9..e3d719d 100644 --- a/docs/AllClasses.html +++ b/docs/AllClasses.html @@ -18,6 +18,11 @@

All Classes, Interfaces, Objects and Records

Description +TImplicitTreapNode +ftreap +

 

+ + TTreapNode ftreap

 

diff --git a/docs/AllIdentifiers.html b/docs/AllIdentifiers.html index bca402e..e70db49 100644 --- a/docs/AllIdentifiers.html +++ b/docs/AllIdentifiers.html @@ -18,6 +18,11 @@

All Identifiers

Description +TImplicitTreapNode +ftreap +

 

+ + TTreapNode ftreap

 

diff --git a/docs/ClassHierarchy.html b/docs/ClassHierarchy.html index 3bcc840..24dc83b 100644 --- a/docs/ClassHierarchy.html +++ b/docs/ClassHierarchy.html @@ -13,7 +13,7 @@

Class Hierarchy


Generated by PasDoc 0.15.0. diff --git a/docs/ftreap.TImplicitTreapNode.html b/docs/ftreap.TImplicitTreapNode.html new file mode 100644 index 0000000..fca6ab3 --- /dev/null +++ b/docs/ftreap.TImplicitTreapNode.html @@ -0,0 +1,277 @@ + + + +ftreap: Class TImplicitTreapNode + + + + + + +
+

Class TImplicitTreapNode

+ +

Unit

+ +

Declaration

+

+type generic TImplicitTreapNode<T> = class(TObject)

+

Description

+

Hierarchy

+
  • TObject
  • +
  • TImplicitTreapNode

Overview

+

Fields

+ + + + + + + + + + + + + + + + + + + + + +
PrivateFValue: T;
PrivateFPriority: extended;
PrivateFSize: SizeUInt;
PrivateFLeft: TImplicitTreapNode;
PrivateFRight: TImplicitTreapNode;
+

Methods

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Publicconstructor Create(const v: T);
Publicdestructor Destroy; override;
Publicclass function GetSize(const node: TImplicitTreapNode): SizeUInt; inline;
Publicclass procedure UpdateSize(const node: TImplicitTreapNode); inline;
Publicclass function Meld(l, r: TImplicitTreapNode): TImplicitTreapNode;
Publicclass procedure DivideAt(node: TImplicitTreapNode; const pos: SizeUInt; var l, r: TImplicitTreapNode);
Publicclass function IsEmpty(const node: TImplicitTreapNode): boolean; inline;
Publicclass procedure InsertAt(var node: TImplicitTreapNode; const pos: SizeUInt; const v: T); inline;
Publicclass function GetAt(node: TImplicitTreapNode; pos: SizeUInt): T;
Publicclass function RemoveAt(var node: TImplicitTreapNode; const pos: SizeUInt): T;
Publicclass procedure DestroyTreap(var node: TImplicitTreapNode);
Publicclass function CheckStucture(node: TImplicitTreapNode): boolean;
+

Description

+

Fields

+ + + + + + +
PrivateFValue: T;
+

+Value

+
+ + + + + + +
PrivateFPriority: extended;
+

+Random heap priority

+
+ + + + + + +
PrivateFSize: SizeUInt;
+

+Number of nodes in subtree

+
+ + + + + + +
PrivateFLeft: TImplicitTreapNode;
+

+Left subtree reference

+
+ + + + + + +
PrivateFRight: TImplicitTreapNode;
+

+Right subtree reference

+
+

Methods

+ + + + + + +
Publicconstructor Create(const v: T);
+

+Tree node constructor.

+
+ + + + + + +
Publicdestructor Destroy; override;
+

+Tree node destructor.

+
+ + + + + + +
Publicclass function GetSize(const node: TImplicitTreapNode): SizeUInt; inline;
+

+Returns number of keys in the tree rooted at node.

+
+ + + + + + +
Publicclass procedure UpdateSize(const node: TImplicitTreapNode); inline;
+

+Recalculates number of nodes in the tree rooted at node after insert, delete operations.

+
+ + + + + + +
Publicclass function Meld(l, r: TImplicitTreapNode): TImplicitTreapNode;
+

+Creates new tree from two trees

+
+ + + + + + +
Publicclass procedure DivideAt(node: TImplicitTreapNode; const pos: SizeUInt; var l, r: TImplicitTreapNode);
+

+Divides tree into two trees. Where Size(l) = pos.

+
+ + + + + + +
Publicclass function IsEmpty(const node: TImplicitTreapNode): boolean; inline;
+

+Returns True if tree rooted at node is empty, False otherwise

+
+ + + + + + +
Publicclass procedure InsertAt(var node: TImplicitTreapNode; const pos: SizeUInt; const v: T); inline;
+

+Insert value k at position in tree rooted at node.

+
+ + + + + + +
Publicclass function GetAt(node: TImplicitTreapNode; pos: SizeUInt): T;
Exceptions raised
+
+
EArgumentException
+
+
+
+ + + + + + +
Publicclass function RemoveAt(var node: TImplicitTreapNode; const pos: SizeUInt): T;
+

+Removes value from the given position.

+
Returns
+

key

+ + + + + + +
Publicclass procedure DestroyTreap(var node: TImplicitTreapNode);
+

+Destroy tree.

+
+ + + + + + +
Publicclass function CheckStucture(node: TImplicitTreapNode): boolean;
+
Generated by PasDoc 0.15.0. + +
diff --git a/docs/ftreap.TTreapNode.html b/docs/ftreap.TTreapNode.html index 58434dc..32f3480 100644 --- a/docs/ftreap.TTreapNode.html +++ b/docs/ftreap.TTreapNode.html @@ -70,54 +70,66 @@

Description

Public -class procedure Divide(node: TTreapNode; k: T; var l, r: TTreapNode); +class procedure DivideRight(node: TTreapNode; k: T; var l, r: TTreapNode); Public -class procedure DivideAt(node: TTreapNode; const pos: SizeUInt; var l, r: TTreapNode); +class procedure DivideLeft(node: TTreapNode; k: T; var l, r: TTreapNode); Public -class function IsEmpty(const node: TTreapNode): boolean; inline; +class procedure DivideAt(node: TTreapNode; const pos: SizeUInt; var l, r: TTreapNode); Public -class procedure Insert(var node: TTreapNode; const k: T); inline; +class function IsEmpty(const node: TTreapNode): boolean; inline; Public -class function Contains(node: TTreapNode; const k: T): boolean; inline; +class procedure Insert(var node: TTreapNode; const k: T); inline; Public -class function BisectLeft(node: TTreapNode; const k: T): SizeUInt; +class function Contains(node: TTreapNode; const k: T): boolean; inline; Public -class function BisectRight(node: TTreapNode; const k: T): SizeUInt; +class function BisectLeft(node: TTreapNode; const k: T): SizeUInt; Public -class function GetPosition(node: TTreapNode; const k: T): SizeUInt; +class function BisectRight(node: TTreapNode; const k: T): SizeUInt; Public +class function GetPosition(node: TTreapNode; const k: T): SizeUInt; + + +Public class function GetAt(node: TTreapNode; pos: SizeUInt): T; + +Public +class function Min(node: TTreapNode): T; + Public -class function Remove(var node: TTreapNode; const k: T): boolean; +class function Max(node: TTreapNode): T; Public -class function RemoveAt(var node: TTreapNode; const pos: SizeUInt): T; +class function Remove(var node: TTreapNode; const k: T): boolean; Public -class procedure DestroyTreap(var node: TTreapNode); +class function RemoveAt(var node: TTreapNode; const pos: SizeUInt): T; Public +class procedure DestroyTreap(var node: TTreapNode); + + +Public class function CheckStucture(node: TTreapNode): boolean; @@ -227,7 +239,7 @@

Methods

- +
Publicclass procedure Divide(node: TTreapNode; k: T; var l, r: TTreapNode);class procedure DivideRight(node: TTreapNode; k: T; var l, r: TTreapNode);

@@ -237,11 +249,21 @@

Methods

+ + + +
Publicclass procedure DivideLeft(node: TTreapNode; k: T; var l, r: TTreapNode);
+

+Divides tree into two trees. Where Max(l) < k.

+
+ + +
Public class procedure DivideAt(node: TTreapNode; const pos: SizeUInt; var l, r: TTreapNode);

-Divides tree into two trees. Where Size(l) = pos + 1.

+Divides tree into two trees. Where Size(l) = pos.

@@ -318,6 +340,22 @@

Methods

+ + + +
Publicclass function Min(node: TTreapNode): T;
+ + + + + + +
Publicclass function Max(node: TTreapNode): T;
+ + + + + + +
Public class function Remove(var node: TTreapNode; const k: T): boolean;
diff --git a/docs/ftreap.html b/docs/ftreap.html index 5a7974f..05ce6c8 100644 --- a/docs/ftreap.html +++ b/docs/ftreap.html @@ -25,6 +25,10 @@

Unit ftreap

Class TTreapNode  
Class TImplicitTreapNode 

Generated by PasDoc 0.15.0. diff --git a/ftreap.pas b/ftreap.pas index 1adf501..7d8f6e3 100644 --- a/ftreap.pas +++ b/ftreap.pas @@ -67,6 +67,8 @@ generic TTreapNode = class (* @raises(EArgumentException) *) class function GetAt(node: TTreapNode; pos: SizeUInt): T; + class function Min(node: TTreapNode): T; + class function Max(node: TTreapNode): T; (* Removes key from the tree. @returns(@true if successful, @false otherwise) *) @@ -360,6 +362,26 @@ class function TTreapNode.GetAt(node: TTreapNode; pos: SizeUInt): T; raise Exception.Create('Unreachable point.'); end; +// Min, Max +class function TTreapNode.Min(node: TTreapNode): T; +begin + if node = nil then + raise EArgumentException.Create('Set is empty.'); + while node.FLeft <> nil do + node := node.FLeft; + Exit(node.FKey); +end; + +class function TTreapNode.Max(node: TTreapNode): T; +begin + if node = nil then + raise EArgumentException.Create('Set is empty.'); + while node.FRight <> nil do + node := node.FRight; + Exit(node.FKey); +end; +// Min, Max + class function TTreapNode.Remove(var node: TTreapNode; const k: T): boolean; var n: TTreapNode; diff --git a/test/treaptestcase.pas b/test/treaptestcase.pas index be3cc28..97041c0 100644 --- a/test/treaptestcase.pas +++ b/test/treaptestcase.pas @@ -162,7 +162,7 @@ procedure TTreapTestCase.TestMin; try begin for i := 0 to KEYS_NUMBER - 1 do - AssertEquals(0, TIntTreapNode.GetAt(root, 0)); + AssertEquals(0, TIntTreapNode.Min(root)); end; except Fail('Invalid min key!'); @@ -179,7 +179,7 @@ procedure TTreapTestCase.TestMax; s := TIntTreapNode.GetSize(root); for i := 0 to KEYS_NUMBER - 1 do begin - AssertEquals(2 * (KEYS_NUMBER - 1), TIntTreapNode.GetAt(root, s-1)); + AssertEquals(2 * (KEYS_NUMBER - 1), TIntTreapNode.Max(root)); end; end; except