Skip to content

Commit 8ed6adf

Browse files
feat (functionality): real parent and child address
Able to get actual address of parent and last child of given tree node. Returns real address of parent and last child of given tree node instead of a copy. Resolves: #127
1 parent 0d040a3 commit 8ed6adf

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

inc/tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@ void export_tree_data_for_web_view (Tree* tree);
4747
void display_path_towards_root (Tree* tree, Node* node);
4848
Node* remove_child_node (Tree* tree, Node* parent_node);
4949
bool does_tree_contain_node (Tree* tree, Node* node);
50+
Node* get_last_child_node_address (Node* parent_node); // returns actual address; handle with care
51+
Node* get_parent_node_address (Node* child_node); // returns actual address; handle with care
5052

5153
#endif // TREE_H

log/memory.log

0 Bytes
Binary file not shown.

src/tree.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,4 +683,47 @@ append_child_node (new_tree, anchor, child_node);
683683
x_node = NULL;
684684

685685
return new_tree;
686+
}
687+
688+
Node* get_last_child_node_address (Node* parent_node) {
689+
if (parent_node == NULL) {
690+
perror ("Given Node doesn't exists");
691+
return NULL;
692+
}
693+
694+
if (parent_node -> type != N_Tree) {
695+
perror ("Given Node doesn't belong to a tree");
696+
return NULL;
697+
}
698+
699+
size_t address_count = parent_node -> address_list -> item_count;
700+
701+
if (address_count < 2) { // 0 based index
702+
perror ("Child does not exist to get last child!");
703+
return NULL;
704+
}
705+
706+
Data* addr_data = get_list_data_at_index (parent_node -> address_list, address_count - 1); // 0 based index
707+
Node* child_node = addr_data -> address;
708+
delete_data (&addr_data);
709+
710+
return child_node;
711+
}
712+
713+
Node* get_parent_node_address (Node* child_node) {
714+
if (NULL == child_node) {
715+
perror ("Given Node doesn't exists");
716+
return NULL;
717+
}
718+
719+
if (child_node -> type != N_Tree) {
720+
perror ("Given Node doesn't belong to a tree");
721+
return NULL;
722+
}
723+
724+
Data* addr_data = get_list_data_at_index (child_node -> address_list, 0);
725+
Node* parent_node = addr_data -> address;
726+
delete_data (&addr_data);
727+
728+
return parent_node;
686729
}

0 commit comments

Comments
 (0)