Backend Development 6 min read

Designing a Tree‑Based Comment System with C Data Structures

This article explains how to model a comment system as a tree structure, defines node attributes and enums in C, and discusses handling deletions, replies, and conversation lists while keeping the data model extensible for future features.

Beike Product & Technology
Beike Product & Technology
Beike Product & Technology
Designing a Tree‑Based Comment System with C Data Structures

From a personal perspective, a comment is a discussion triggered by a topic, and modern platforms such as Weibo, Baidu Tieba, and Zhihu implement comment systems composed of several small functions like topics, replies, likes, reports, deletions, and conversation lists.

Conceptually, each topic and comment can be abstracted as a node, forming a tree; the article uses Weibo as an example to illustrate this design.

Key abstractions include:

Topic is a node.

Each comment is a node.

Attributes such as author info, likes, etc., are node properties.

Direct comments are those attached directly to the topic.

Replies to direct comments are called sub‑comments.

From these definitions, the following conclusions arise:

A tree can be built with the topic node as the root.

A subtree can be built with a direct comment as its root.

All subtrees together with the topic node form the complete comment tree.

Based on this abstraction, a simple C data structure for a comment node is proposed:

typedef struct {
  int id;
  int replied_id; // id of the comment being replied to
  int replied_root_id; // id of the direct comment (not the topic)
  char *content; // comment text
} CommentNode;

The implementation rules are straightforward:

If the node is a direct comment, set replied_id and replied_root_id to 0.

If the node is a sub‑comment, replied_id stores the parent comment’s id and replied_root_id stores the id of the direct comment it belongs to.

When a comment needs to be removed, deleting the node would break the tree structure. Instead, the content field is cleared and a status field is added to indicate deletion.

An enum NodeColor is introduced to represent node state, and the struct is extended accordingly:

typedef enum { GREEN, // normal node
               RED    // deleted node
} NodeColor;

typedef struct {
  int id;               // > 0
  int replied_id;        // id of the comment being replied to
  int replied_root_id;   // id of the direct comment
  char *content;        // comment text
  NodeColor color;      // node status
} CommentNode;

Additional fields such as from_user_id and to_user_id can be added to support conversation lists, enabling a dialogue flow between two users.

typedef struct {
  int id;
  int replied_id;
  int replied_root_id;
  char *content;
  NodeColor color;
  int from_user_id; // commenter id
  int to_user_id;   // replied user id
} CommentNode;

The core of program design lies in a well‑thought‑out data structure; extending functionality later merely requires adding new fields, keeping modification costs low and ensuring forward compatibility.

The article concludes with a brief personal summary of the comment system design.

backend designdata structuresC programmingtreecomment system
Beike Product & Technology
Written by

Beike Product & Technology

As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.