-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDS_Tree.class.php
170 lines (152 loc) · 4.09 KB
/
DS_Tree.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Created by JetBrains PhpStorm.
* User: horsley
* Date: 12-12-29
* Time: 下午12:06
* To change this template use File | Settings | File Templates.
*/
if (!defined('NODE_PARENT')) define('NODE_PARENT', 'parent');
if (!defined('NODE_CHILDREN')) define('NODE_CHILDREN', 'children');
if (!defined('NODE_ARC')) define('NODE_ARC', 'arc');
class DS_Tree
{
private $nodes = array(); //节点数组
private $current_node_ptr; //当前节点指针(索引值)
/**
* 树是否为空
* @return bool
*/
public function is_empty() {
return empty($this->nodes);
}
/**
* 返回节点数量
* @return int
*/
public function get_size() {
return count($this->nodes);
}
/**
* 在当前节点下插入子节点,返回插入的节点ID
* @param $node
* @return int
*/
public function insert_node($node) {
$node_id = array_push($this->nodes, $node) - 1;
$new_node = &$this->nodes[$node_id];
if ($node_id != 0) { //非根节点插入
$pnode = &$this->nodes[$this->current_node_ptr];
if (empty($pnode[NODE_CHILDREN])) $pnode[NODE_CHILDREN] = array();
array_push($pnode[NODE_CHILDREN], $node_id);
$new_node[NODE_PARENT] = $this->current_node_ptr;
} else {
$new_node[NODE_PARENT] = NULL;
}
return $node_id;
}
/**
* 更新当前节点的属性值
* @param $key
* @param $value
*/
public function update_node_attr($key, $value) {
$cur_node = &$this->nodes[$this->current_node_ptr];
$cur_node[$key] = $value;
}
/**
* 取得当前节点的属性值
* @param $key
* @return mixed
*/
public function get_node_attr($key) {
return $this->nodes[$key];
}
/**
* 当前节点设置为树的根节点
*/
public function goto_root() {
if ($this->get_size() > 0) {
reset($this->nodes);
$this->current_node_ptr = key($this->nodes);
return true;
} else {
return false;
}
}
/**
* 跳到指定索引的节点
* @param $index
* @return bool
*/
public function goto_index($index) {
if(isset($this->nodes[$index])) {
$this->current_node_ptr = $index;
return true;
} else {
return false;
}
}
/**
* 取回当前节点的索引
* @return mixed
*/
public function get_index() {
return $this->current_node_ptr;
}
/**
* 取子节点数
* @return int
*/
public function count_children() {
return count($this->nodes[$this->current_node_ptr][NODE_CHILDREN]);
}
/**
* 取叶子节点
* @return array
*/
public function get_leafs() {
$ret = array();
foreach($this->nodes as $k => $n) {
if (empty($n[NODE_CHILDREN]))
$ret[$k] = $n;
}
return $ret;
}
public function draw_tree($nid = NULL) {
if ($nid == NULL) {
$nid = 0;
$ret = '<ul><li>结果:';
} else {
$node = $this->nodes[$nid];
$ret = '<ul><li>' . $node[NODE_NAME]. ': ' . $node[NODE_ARC];
}
if (isset($node[NODE_CHILDREN])) {
$ret .= '<ul><li>' . $node[NODE_NAME] ;
foreach($node[NODE_CHILDREN] as $cnid) {
$ret .= $this->draw_tree($cnid);
}
$ret .= '</li></ul>';
} else {
$ret .= '<li>';
$ret.= $node[NODE_ARC] . ': ' . $node[NODE_NAME];
$ret .= '<li>';
}
if ($nid == NULL) {
$ret .= '</li></ul>';
}
return $ret;
}
/**
* 返回当前节点下面的一层子节点
* @return array
*/
public function get_children() {
$ret = array();
$children_id = $this->nodes[$this->current_node_ptr][NODE_CHILDREN];
foreach($children_id as $k) {
$ret[$k] = $this->nodes[$k];
}
return $ret;
}
}