Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
arbor
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
arbor-sim
arbor
Commits
42431d4c
Commit
42431d4c
authored
Dec 17, 2015
by
Benjamin Cumming
Browse files
Options
Downloads
Patches
Plain Diff
work on rebalancing
parent
ea6efa26
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.cpp
+19
-5
19 additions, 5 deletions
main.cpp
tree.hpp
+65
-25
65 additions, 25 deletions
tree.hpp
with
84 additions
and
30 deletions
main.cpp
+
19
−
5
View file @
42431d4c
...
...
@@ -103,12 +103,7 @@ TEST(cell_tree, from_parent_index) {
EXPECT_EQ
(
tree
.
num_children
(
3
),
0
);
EXPECT_EQ
(
tree
.
num_children
(
4
),
0
);
}
}
TEST
(
cell_tree
,
test_balance
)
{
{
// a cell with the following structure
// should be rebalanced around node 1
// 0
// / \
// 1 2
...
...
@@ -131,6 +126,25 @@ TEST(cell_tree, test_balance) {
}
}
TEST
(
tree
,
change_root
)
{
{
// a cell with the following structure
// should be rebalanced around node 1
// 0
// / \
// 1 2
// / \
// 3 4
// / \
// 5 6
std
::
vector
<
int
>
parent_index
=
{
0
,
0
,
0
,
1
,
1
,
4
,
4
};
tree
t
;
t
.
init_from_parent_index
(
parent_index
);
auto
new_tree
=
t
.
change_root
(
1
);
}
}
/*
void test_json() {
json cell_data;
...
...
This diff is collapsed.
Click to expand it.
tree.hpp
+
65
−
25
View file @
42431d4c
...
...
@@ -175,6 +175,34 @@ class tree {
return
sizeof
(
int_type
)
*
data_
.
size
()
+
sizeof
(
tree
);
}
tree
change_root
(
int
b
)
const
{
assert
(
b
<
num_nodes
());
// no need to rebalance if the root node has been requested
if
(
b
==
0
)
{
return
tree
(
*
this
);
}
// create new tree with memory allocated
tree
new_tree
;
new_tree
.
init
(
num_nodes
());
// add the root node
//new_tree.parents_[0] = -1;
new_tree
.
child_index_
[
0
]
=
0
;
// allocate space for the permutation vector that
// will represent the permutation performed on the branches
// during the rebalancing
index_type
p
(
num_nodes
()
+
1
);
std
::
cout
<<
"allocated for array of size "
<<
p
.
size
()
<<
std
::
endl
;
// recersively rebalance the tree
new_tree
.
add_children
(
0
,
b
,
p
,
*
this
,
true
);
return
new_tree
;
}
private
:
void
init
(
int
nnode
)
{
...
...
@@ -200,58 +228,70 @@ class tree {
assert
(
parents_
.
size
()
==
nnode
);
}
/// Renumber the sub-tree with old_
branch
as its root with new_
branch
as
/// the new index of old_
branch
. This is a helper function for the
/// Renumber the sub-tree with old_
node
as its root with new_
node
as
/// the new index of old_
node
. This is a helper function for the
/// renumbering required when a new root node is chosen to improve
/// the balance of the tree.
/// Optionally add the parent of old_
branch
as a child of new_
branch
, which
/// Optionally add the parent of old_
node
as a child of new_
node
, which
/// will be applied recursively until the old root has been processed,
/// which indicates that the renumbering is finished.
/*
int add_child( int new_branch, int old_branch, index_view p,
bool parent_as_child)
///
/// precondition - the node new node has already been placed in the tree
int
add_children
(
int
new_node
,
int
old_node
,
index_view
p
,
tree
const
&
old_tree
,
bool
parent_as_child
)
{
std
::
cout
<<
"add_children(old "
<<
old_node
<<
", new "
<<
new_node
<<
", "
<<
parent_as_child
<<
")"
<<
std
::
endl
;
// check for the senitel that indicates that the old root has
// been processed
if(old_
branch
==-1) {
if
(
old_
node
==-
1
)
{
assert
(
parent_as_child
);
// sanity check
return new_
branch
;
return
new_
node
;
}
auto
kids =
children(old_
branch
);
auto pos =
new_
child_index[new_
branch
];
auto
old_children
=
old_tree
.
children
(
old_
node
);
auto
pos
=
child_index
_
[
new_
node
];
new_child_index[new_branch+1] = pos + kids.size();
// add an extra one if adding the parent as a child
if(parent_as_child) {
++new_child_index[new_branch+1];
}
auto
n_children
=
old_children
.
size
()
+
(
parent_as_child
?
1
:
0
);
child_index_
[
new_node
+
1
]
=
pos
+
n_children
;
std
::
cout
<<
" inserting "
<<
n_children
<<
" into "
<<
child_index_
(
new_node
,
new_node
+
2
)
<<
std
::
endl
;
std
::
cout
<<
" inserting"
;
// first renumber the children
for(auto b : kids) {
p[new_branch] = b;
new_children[pos++] = new_branch++;
for
(
auto
b
:
old_children
)
{
std
::
cout
<<
" "
<<
b
;
new_node
++
;
p
[
new_node
]
=
b
;
children_
[
pos
++
]
=
new_node
;
}
// then add and renumber the parent as a child
if
(
parent_as_child
)
{
p[new_branch] = parents_[old_branch];
new_children[pos++] = new_branch++;
std
::
cout
<<
" "
<<
yellow
(
std
::
to_string
(
old_tree
.
parent
(
old_node
)));
new_node
++
;
p
[
new_node
]
=
old_tree
.
parent
(
old_node
);
children_
[
pos
++
]
=
new_node
;
}
std
::
cout
<<
std
::
endl
;
// then visit the sub-tree of each child recursively
// - traverse _down_ the tree
for(auto b : kids) {
new_branch = add_children(new_branch, b, p, false);
//auto child_range = range();
for
(
auto
b
:
old_children
)
{
new_node
=
add_children
(
new_node
,
b
,
p
,
old_tree
,
false
);
}
// finally visit the parent recursively
// - traverse _up_ the tree towards the old root
if
(
parent_as_child
)
{
new_
branch
= add_children(new_
branch
, parents_[old_
branch], p
, true);
new_
node
=
add_children
(
new_
node
,
parents_
[
old_
node
],
p
,
old_tree
,
true
);
}
return new_
branch
;
return
new_
node
;
}
*/
index_type
data_
;
index_view
children_
;
...
...
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
sign in
to comment