#Does the difference of DFS or BFS matter when traversing the entire tree?

1 messages · Page 1 of 1 (latest)

lime sleet
#

Hey guys, really quick question . I have a situation where when a certain post is deleted, all of its replies and the replies to those replies should be deleted as well. I'm collecting all the IDs of them and deleting them using DFS at the moment. I was wondering if it makes a difference whether its DFS or BFS? My guess was no because it searches all the elements anyways, but maybe there is like a processing or memory aspect. Thank you in advance!

warped delta
#

I think the real question is why you don’t have some kind of index of child posts? This way you can avoid the overhead of the search and just iterate the children

lime sleet
# warped delta I think the real question is why you don’t have some kind of index of child post...

Sorry, im not sure i understand fully by having an index. Could you clarify what you mean? 😅

The current system is that each post has an optional parent ID, so if i delete post 1, it will find all posts where parent_id = 1, then repeat that for all the children and so on. The code below is the method for deleting the posts, and the image is the SQL table for posts.

  public void deleteReplies(Post post) {
      List<Post> children = postRepository.findAllByParentId(post.getId());
      for (Post child : children) {
          deleteReplies(child);
          notificationService.deleteAllNonFollowNotificationsByReferenceId(child.getId());
          postRepository.delete(child);
      }
  }
lime sleet
#

Sorry i probably misunderstand, but is your suggestion to create a table in the DB to hold these relationships?🤔

with columns such as

post_id and descendant_id?

So in the image, Post 1 would have descandants 2, 3, 4, 5, 6,

But if i add post 7, wouldnt i need to go traverse up and add a row for the posts 1, 3, and 6 to account for 7 as one of their descendants?

pastel trout