AI EducademyAIEducademy
🌳

AI基础

🌱
AI 种子

从零开始

🌿
AI 萌芽

打好基础

🌳
AI 枝干

付诸实践

🏕️
AI 树冠

深入探索

🌲
AI 森林

精通AI

🔨

AI精通

✏️
AI 草图

从零开始

🪨
AI 雕刻

打好基础

⚒️
AI 匠心

付诸实践

💎
AI 打磨

深入探索

🏆
AI 杰作

精通AI

🚀

职业准备

🚀
面试发射台

开启你的旅程

🌟
行为面试精通

掌握软技能

💻
技术面试

通过编程轮次

🤖
AI与ML面试

ML面试精通

🏆
Offer与未来

拿下最好的Offer

查看所有学习计划→

实验室

已加载 7 个实验
🧠神经网络游乐场🤖AI 还是人类?💬提示实验室🎨图像生成器😊情感分析器💡聊天机器人构建器⚖️伦理模拟器
🎯模拟面试进入实验室→
学习旅程博客
🎯
关于

让AI教育触达每一个人、每一个角落

❓
常见问题

Common questions answered

✉️
Contact

Get in touch with us

⭐
Open Source

在 GitHub 上公开构建

立即开始
AI EducademyAIEducademy

MIT 许可证。开源项目

学习

  • 学习计划
  • 课程
  • 实验室

社区

  • GitHub
  • 参与贡献
  • 行为准则
  • 关于
  • 常见问题

支持

  • 请我喝杯咖啡 ☕
  • 服务条款
  • 隐私政策
  • 联系我们
AI & 工程学习计划›✏️ AI 草图›课程›树与图可视化
🌳
AI 草图 • 中级⏱️ 18 分钟阅读

树与图可视化

Data With Relationships

So far, we've looked at data in lines - arrays, linked lists, stacks, and queues all arrange items in a sequence. But the real world isn't linear. Family trees branch out. Social networks form webs. Road maps create interconnected routes.

Trees and graphs capture these relationships, and they're at the heart of some of AI's most powerful techniques.

Trees - Hierarchical Data

A tree is a structure where each item (called a node) can have child nodes, forming a hierarchy. There's one special node at the top called the root, and nodes with no children are called leaves.

         CEO
        /    \
      CTO    CFO
     /   \      \
   Dev1  Dev2   Accountant
A tree structure with a root node branching into child nodes across three levels, alongside a graph with interconnected nodes
Trees flow downward from a root; graphs connect nodes in any direction.

Trees Are Everywhere

  • File systems: Folders contain subfolders which contain files - a tree.
  • HTML/DOM: Every web page is a tree of nested elements.
  • Organisation charts: Managers have reports who may have their own reports.
  • JSON data: The nested structure of JSON is fundamentally a tree.

Binary Trees

A binary tree restricts each node to at most two children - a left child and a right child. This simple constraint enables powerful algorithms.

        8
       / \
      3   10
     / \    \
    1   6    14

Binary Search Trees (BSTs)

A binary search tree adds one rule: for every node, all values in the left subtree are smaller, and all values in the right subtree are larger.

This makes searching fast - at each node, you know whether to go left or right:

Find 6 in the BST above:
  Start at 8 → 6 < 8, go left
  At 3 → 6 > 3, go right
  At 6 → Found it!
第 5 课,共 10 课已完成 0%
←链表与栈

Discussion

Sign in to join the discussion

建议修改本课内容

Time complexity: O(log n) for a balanced tree - the same logarithmic magic as binary search on a sorted array.

🧠小测验

In a balanced binary search tree with 1,000,000 nodes, roughly how many comparisons are needed to find a value?

Graphs - Everything Connected

A graph generalises trees by removing the hierarchy constraint. It consists of nodes (also called vertices) and edges (connections between nodes). Edges can be:

  • Directed (one-way: A → B) or undirected (two-way: A ↔ B)
  • Weighted (each edge has a cost/distance) or unweighted
Social network (undirected):
  Alice - Bob - Charlie
    \       |
     Diana - Eve

Road map (weighted, directed):
  London →(2h)→ Birmingham →(1.5h)→ Manchester

Graphs Are Everywhere

  • Social networks: People are nodes; friendships are edges.
  • The internet: Web pages are nodes; hyperlinks are edges.
  • Road maps: Intersections are nodes; roads are edges with distance weights.
  • Recommendation systems: Users and products are nodes; interactions are edges.
🤯

Facebook's social graph contains over 3 billion nodes (users) and hundreds of billions of edges (friendships). Graph algorithms determine your News Feed, friend suggestions, and ad targeting - all running on one of the largest graphs ever built.

How AI Uses Trees

Decision Trees

One of the most interpretable AI models is the decision tree. It asks a series of yes/no questions to classify data:

Is temperature > 30°C?
├── Yes: Is humidity > 70%?
│   ├── Yes: "Don't play tennis"
│   └── No: "Play tennis"
└── No: Is it windy?
    ├── Yes: "Don't play tennis"
    └── No: "Play tennis"

Decision trees are popular because humans can read and understand them - crucial in healthcare, finance, and legal AI where explainability matters.

🤔
Think about it:

A hospital uses an AI to predict patient risk. Regulators require the AI to explain its decisions. Why might a decision tree be preferred over a deep neural network here, even if the neural network is slightly more accurate?

Random Forests

A random forest builds hundreds of decision trees, each trained on a slightly different subset of the data, then takes a vote. This ensemble approach is more accurate and robust than a single tree - and it's still one of the most widely used AI techniques in industry.

How AI Uses Graphs

Knowledge Graphs

A knowledge graph stores facts as relationships between entities:

(London) --[capital_of]--> (United Kingdom)
(London) --[located_in]--> (England)
(Big Ben) --[located_in]--> (London)

Google's Knowledge Graph powers those information panels you see in search results. When you search "Big Ben," the graph connects it to London, the UK, and related landmarks.

Recommendation Graphs

Netflix, Spotify, and Amazon model users and items as a graph. If users A and B both liked items X and Y, and user A also liked item Z, the graph suggests Z to user B. This is collaborative filtering powered by graph structure.

🧠小测验

Why are graphs better than simple lists for modelling social networks?

Traversal - Walking Through Trees and Graphs

Depth-First Search (DFS)

DFS explores as far as possible along one path before backtracking. Think of it as exploring a maze by always taking the leftmost turn until you hit a dead end, then backtracking.

        A
       / \
      B   C
     / \   \
    D   E   F

DFS order: A → B → D → E → C → F

DFS uses a stack (naturally via recursion, or explicitly). It's great for:

  • Solving mazes and puzzles
  • Detecting cycles in graphs
  • Topological sorting (ordering tasks with dependencies)

Breadth-First Search (BFS)

BFS explores all neighbours at the current depth before moving deeper. Think of it as ripples spreading outward from a stone dropped in a pond.

        A
       / \
      B   C
     / \   \
    D   E   F

BFS order: A → B → C → D → E → F

BFS uses a queue. It's great for:

  • Finding the shortest path in an unweighted graph
  • Social network analysis (degrees of separation)
  • Web crawling (visiting pages level by level)
💡

Notice how stacks and queues from the previous lesson connect to tree and graph traversal? DFS uses a stack; BFS uses a queue. Data structures build upon each other - that's why learning them in order matters.

🧠小测验

You want to find the shortest route between two cities on a map where all roads are the same length. Which traversal should you use?

🤔
Think about it:

Social media platforms measure "degrees of separation" - how many friend-of-friend hops connect two people. Which traversal algorithm would efficiently find the fewest hops between two users? How does this relate to the "six degrees of separation" idea?

🤯

Google's PageRank algorithm - the original breakthrough that made Google dominant - models the web as a graph. Each web page is a node, each hyperlink is a directed edge, and a page's importance is determined by how many important pages link to it. It's essentially a random walk on a graph.

Key Takeaways

  • Trees model hierarchical data - file systems, HTML, and AI decision trees all use tree structures.
  • Binary search trees enable O(log n) lookups by maintaining a sorted hierarchy.
  • Graphs model interconnected data - social networks, knowledge bases, and recommendation engines.
  • DFS (stack-based) goes deep; BFS (queue-based) goes wide - both power core AI algorithms like PageRank and recommendation engines.