Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
A
ad
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Peter Gerwinski
ad
Commits
1edd92e7
Commit
1edd92e7
authored
May 7, 2018
by
Peter Gerwinski
Browse files
Options
Downloads
Patches
Plain Diff
Beispiel 2-3-4-tree-10.cpp, 7.5.2018
parent
edccbf80
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
20180507/2-3-4-tree-10.cpp
+156
-0
156 additions, 0 deletions
20180507/2-3-4-tree-10.cpp
with
156 additions
and
0 deletions
20180507/2-3-4-tree-10.cpp
0 → 100644
+
156
−
0
View file @
1edd92e7
#include
<iostream>
using
namespace
std
;
const
int
MaxLeaves
=
4
;
const
int
MaxEntries
=
MaxLeaves
-
1
;
template
<
typename
TElement
>
class
TTree
{
struct
TEntry
{
TElement
Content
;
bool
empty
;
TEntry
()
{
empty
=
true
;
}
};
struct
TNode
{
TEntry
Entry
[
MaxEntries
];
TNode
*
Child
[
MaxLeaves
];
TNode
()
{
for
(
int
i
=
0
;
i
<
MaxLeaves
;
i
++
)
Child
[
i
]
=
NULL
;
}
void
SetEntry
(
int
i
,
TElement
e
)
{
Entry
[
i
].
Content
=
e
;
Entry
[
i
].
empty
=
false
;
}
};
TNode
*
Root
;
private
:
void
SplitNode
(
TNode
*
FullNode
,
TElement
NewElement
,
TNode
*&
Left
,
TNode
*&
Right
,
TElement
&
Middle
)
{
int
i
=
0
;
while
(
i
<
MaxEntries
&&
NewElement
>
FullNode
->
Entry
[
i
].
Content
)
i
++
;
TElement
Top
;
if
(
i
<
MaxEntries
)
{
Top
=
FullNode
->
Entry
[
MaxEntries
-
1
].
Content
;
for
(
int
j
=
MaxEntries
-
1
;
j
>
i
;
j
--
)
FullNode
->
Entry
[
j
]
=
FullNode
->
Entry
[
j
-
1
];
FullNode
->
SetEntry
(
i
,
NewElement
);
}
else
Top
=
NewElement
;
// Achtung: Hier benutzen wir, daß MaxEntries = 3 ist!
Middle
=
FullNode
->
Entry
[
2
].
Content
;
FullNode
->
Entry
[
2
].
empty
=
true
;
Left
=
FullNode
;
Right
=
new
TNode
();
Right
->
SetEntry
(
0
,
Top
);
}
void
InsertNode
(
TNode
*
Node
,
TElement
e
)
{
if
(
Node
->
Child
[
0
])
{
int
i
=
0
;
while
(
i
<
MaxEntries
&&
!
Node
->
Entry
[
i
].
empty
&&
e
>
Node
->
Entry
[
i
].
Content
)
i
++
;
if
(
Node
->
Child
[
i
])
InsertNode
(
Node
->
Child
[
i
],
e
);
// @@@ Entartung möglich!
else
{
Node
->
Child
[
i
]
=
new
TNode
();
InsertNode
(
Node
->
Child
[
i
],
e
);
// @@@ B-Baum-Bedingung: Knoten müssen immer mindestens zur Hälfte aufgefüllt sein!
// @@@ Hier automatisch erfüllt, weil "Hälfte" = 1, aber bei "größeren" Bäumen nicht.
}
}
else
if
(
Node
->
Entry
[
MaxEntries
-
1
].
empty
)
{
int
i
=
0
;
while
(
i
<
MaxEntries
&&
!
Node
->
Entry
[
i
].
empty
&&
e
>
Node
->
Entry
[
i
].
Content
)
i
++
;
if
(
i
<
MaxEntries
)
{
if
(
!
Node
->
Entry
[
i
].
empty
)
for
(
int
j
=
MaxEntries
-
1
;
j
>
i
;
j
--
)
Node
->
Entry
[
j
]
=
Node
->
Entry
[
j
-
1
];
Node
->
SetEntry
(
i
,
e
);
}
}
else
{
TNode
*
Left
,
*
Right
;
TElement
Middle
;
SplitNode
(
Node
,
e
,
Left
,
Right
,
Middle
);
// "zerstört" Node
Node
=
new
TNode
();
Node
->
SetEntry
(
0
,
Middle
);
Node
->
Child
[
0
]
=
Left
;
Node
->
Child
[
1
]
=
Right
;
}
}
void
PrintNode
(
TNode
*
Node
)
{
if
(
Node
)
{
for
(
int
i
=
0
;
i
<
MaxEntries
;
i
++
)
{
PrintNode
(
Node
->
Child
[
i
]);
if
(
!
Node
->
Entry
[
i
].
empty
)
cout
<<
Node
->
Entry
[
i
].
Content
<<
" "
;
}
PrintNode
(
Node
->
Child
[
MaxLeaves
-
1
]);
}
}
public
:
TTree
()
{
Root
=
NULL
;
}
void
Insert
(
TElement
e
)
{
if
(
!
Root
)
Root
=
new
TNode
();
InsertNode
(
Root
,
e
);
}
void
Print
()
{
PrintNode
(
Root
);
cout
<<
endl
;
}
};
int
main
()
{
TTree
<
int
>
t
;
t
.
Insert
(
42
);
t
.
Insert
(
13
);
t
.
Insert
(
137
);
t
.
Insert
(
7
);
t
.
Insert
(
1117
);
t
.
Print
();
return
0
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment