使用php mysql在二叉树中查找插入位置和子节点数
技术问答
494 人阅读
|
0 人回复
|
2023-09-12
|
脸是我的表数据
/ G. z1 H t$ N( a. b, m. k ------------- ----------- ---------------- | customer_id | parent_id | node_direction | ------------- ----------- ---------------- | | | T || | | L || | | R || | | L || 5 5 5 5 55 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5555 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 | | R || | | L | ------------- ----------- ---------------- Which represents the following tree | --------- | | 3 | ------- | | 5 | ----- | 我需要通过父 ID 找到插入位置6 K; b( Y4 M+ E s
例如:
! D* ]% M) Q [* e* I. H1) 如果 parent_id 为 1,插入位置为 root-3 position-L
! y3 u1 u; X- x: z0 h& L2) 如果 parent_id 为 2,插入位置为 root-4 position-R
- P- B( C9 d" C! Q$ P3) 如果 parent_id 为 3,插入位置为 root-3 位置-L
9 W7 p7 l5 c+ r! {+ L问题是它需要遵循二进制结构
& x8 t/ f4 Z3 z我还需要根据父节点计算子节点数,例如:
9 x0 w9 {: P6 @ \1 - 5 2 - 3 3 - 0 4 - 1 5 - 0我需要在 php 和 mysql 完成这个。
, Y3 f4 q3 _0 N" U. t- ?任何人都能向我建议实现这个目标最简单的方法吗?- H+ |) W+ C6 Y8 {7 `
7 v9 v3 c$ A: \ @
解决方案: |
|
|
|
|
|