What we usually call a “tree” in daily life, like the big trees in parks, grows many branches from a single root, with smaller branches further splitting on these, and leaves at the top. However, the “tree” in data structures, though abstract, has the same essence as the trees in life—it has a clear “root”, extends branches upward (or downward), and each branch may further split until reaching the outermost “leaf”.
一、Basic Concepts of Tree Structure (Understand with Daily Life Examples)¶
Imagine a big tree; we can break it down into parts corresponding to the “tree” in data structures:
- Root Node: The bottommost “root” of the tree, with no parent node, being the starting point of the entire tree. For example, the CEO of a company or the grandfather in a family is the “root” of their respective structures.
- Child Nodes & Parent Nodes: The new branches that sprout from the main branches of the tree are “child nodes”; the starting point of the branch (e.g., the root node’s branches) is the “parent node” of these child nodes.
For instance: If the grandfather (root node) has branches that split into the father and uncle (child nodes), then the father and uncle have the grandfather as their parent node. If the father’s branches further split into you and your younger brother (child nodes), then you and your brother have the father as their parent node. - Leaf Node: The smallest, non-splitting twigs (or leaves) at the very end of the branches. For example, the smallest leaves at the top of a tree, or you and your younger brother if you don’t have children (if you’re an only child, you’d be the father’s leaf node).
- Subtree: Each node and all its descendants form a “small branch”, which is called a “subtree”. For example, the father and all his children (you, your younger brother) form one subtree of the grandfather; if your younger brother has children, his subtree includes those children.
二、Tree Structure vs. Other Structures (Why Are Trees “Branched”?)¶
The linked list (e.g., a queue) we’re familiar with is “linear”, having only one path; trees, however, are “branched”, like tree branches that split. This is the core feature of trees: non-linear, hierarchical, and branchable.
For example:
- Linked List: A→B→C→D (only a single path from start to end, no branches)
- Tree: A is the root, branching into B and C, and B further splits into D and E (branches exist, with clear hierarchy)
三、Tree Structures in Daily Life (You Encounter Them Every Day!)¶
Tree structures are everywhere in life. Here are examples you must be familiar with:
1. Family Relationship Tree¶
- Root Node: Grandfather (or grandmother)
- Level 1 Child Nodes: Father, uncle, aunt (children of the grandfather)
- Level 2 Child Nodes: You, cousins (children of the father)
- Leaf Node: The youngest child (if they have no offspring)
2. Company Organizational Structure¶
- Root Node: CEO (highest manager of the company)
- Level 1 Child Nodes: Directors of various departments (e.g., CTO, Director of Sales)
- Level 2 Child Nodes: Department managers (e.g., Frontend Manager, Backend Manager under the CTO)
- Level 3 Child Nodes: Frontline employees (team members managed by each manager)
- Leaf Node: Regular employees with no subordinates
3. Computer File System¶
- Root Node: “C:” or “D:” drive (top-level folders)
- Level 1 Child Nodes: Folders like “Documents”, “Pictures”, “Videos” under the C: drive
- Level 2 Child Nodes: Files (e.g., “Reports”, “Resumes”) or subfolders under the “Documents” folder
- Leaf Node: Individual files (e.g., “Resume.docx” with no subfolders)
四、Why Learn Tree Structures?¶
Tree structures are a fundamental and crucial part of data structures because they efficiently handle hierarchical and branched problems. For example:
- Databases use trees for indexing to speed up queries.
- Mobile navigation uses trees for route planning (e.g., branching paths from start to end).
- Game scenes (e.g., forests or mazes on a map) use trees to represent areas and paths.
总结:树结构其实很简单¶
The “tree” in data structures essentially is a structure that starts from a root, extends multi-level branches upward (or downward), where each branch may further split until reaching the outermost “leaf”. In daily life, family relationships, company hierarchies, and computer file systems are all reflections of tree structures. By understanding the basic concepts of trees, you master the core thinking for handling “branching problems”—just as you describe family relationships using branch splitting, you can organize complex data with trees in code!
Next time you see a big tree, a company organizational chart, or a computer folder, try to think: “Which part is the root node? Which is the leaf node?” You’ll realize tree structures are all around us!