Skip to content

Commit b8a33a4

Browse files
authored
Merge pull request #13 from nayounsang/tree-docs-order
docs: code and description order of tree docs
2 parents 29dfd64 + c1712d3 commit b8a33a4

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

src/tree/README.md

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -25,118 +25,120 @@
2525

2626
#### create instance
2727

28-
```typescript
29-
const tree = new Tree<number>(1);
30-
```
31-
3228
- Construct with the root data.
3329
- In this example, root is `1`.
3430

35-
#### add child
36-
3731
```typescript
38-
tree.addChild(current, child);
32+
const tree = new Tree<number>(1);
3933
```
4034

35+
#### add child
36+
4137
- Add the new node at child position of current node.
4238
- Current node should exist in tree. If not exist, it throw the error.
4339
- Child node should not exist in tree. If exist, it throw the error.
4440

45-
#### search
4641

4742
```typescript
48-
const subTreeNodes = tree.search(start);
43+
tree.addChild(current, child);
4944
```
5045

46+
#### search
47+
5148
- Get nodes in subtree.
5249
- Result's type is `Set<T>`, not `T[]`.
5350

54-
#### delete
55-
5651
```typescript
57-
tree.delete(node);
52+
const subTreeNodes = tree.search(start);
5853
```
5954

55+
#### delete
56+
6057
- Delete itself and all nodes in its subtree.
6158
- So, It should not delete root.
6259

63-
#### replace
64-
6560
```typescript
66-
tree.replace(prev, next);
61+
tree.delete(node);
6762
```
6863

64+
#### replace
65+
6966
- A new node replaces the prev node and the structure of tree is maintained.
7067
- Prev node should exist in tree. If not exist, it throw the error.
7168
- New node should not exist in tree. If exist, it throw the error.
7269

70+
71+
```typescript
72+
tree.replace(prev, next);
73+
```
74+
7375
#### root
7476

77+
- Use `getRoot` to get the root data.
78+
- Use `isRoot` to know whether the data is root.
79+
7580
```typescript
7681
const tree = new Tree<number>(1);
7782
console.log(tree.getRoot()); // 1
7883
console.log(tree.isRoot(2)); // false
7984
```
8085

81-
- Use `getRoot` to get root data.
82-
- Use `isRoot` to know whether the data is root.
83-
8486
#### has
8587

88+
- Check if a value exists.
89+
8690
```typescript
8791
const tree = new Tree<number>(1);
8892
console.log(tree.has(1)); // true
8993
console.log(tree.has(2)); // false
9094
```
9195

92-
- Check if a value exists.
93-
9496
#### get parent
9597

98+
- Get parent of node.
99+
- If node is root, this returns `null`.
100+
- After `destroy()` or node not exists, it returns `undefined`.
101+
96102
```typescript
97103
const tree = new Tree<number>(1);
98104
tree.addChild(1, 2);
99105
console.log(tree.getParent(1)); // null
100106
console.log(tree.getParent(2)); // 1
101107
```
102108

103-
- Get parent of node.
104-
- If node is root, this returns `null`.
105-
- After `destroy()` or node not exists, it returns `undefined`.
106-
107109
#### get children
108110

109-
```typescript
110-
tree.getChildren(node);
111-
```
112-
113111
- Get direct children of the node.
114112
- If node not exists, it returns `undefined`
115113

116-
#### get depth
117-
118114
```typescript
119-
tree.getDepth(node);
115+
tree.getChildren(node);
120116
```
121117

118+
#### get depth
119+
122120
- Get depth of the node.
123121
- Root's depth is `0`.
124122
- If node not exists, it returns `undefined`
125123

126-
#### get all nodes
127-
128124
```typescript
129-
tree.getAllNodes();
125+
tree.getDepth(node);
130126
```
131127

128+
#### get all nodes
129+
132130
- Get all nodes in the tree.
133131
- It's return type is `T[]`, not `Set<T>`.
134132

135-
#### destroy
136-
137133
```typescript
138-
tree.destroy();
134+
tree.getAllNodes();
139135
```
140136

137+
#### destroy
138+
141139
- If you want to clear memory, call this.
142140
- After call this, this tree is no longer available.
141+
142+
```typescript
143+
tree.destroy();
144+
```

0 commit comments

Comments
 (0)