Skip to content

Commit 688b469

Browse files
committed
merge
2 parents 044fdbc + 75ae8b4 commit 688b469

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,21 @@
3131
- [size](#size-1)
3232
- [get member variables](#get-member-variables-1)
3333
- [count](#count)
34+
- [Tree](#tree)
35+
- [Term Explanation](#term-explanation-2)
36+
- [Usage](#usage-2)
37+
- [create instance](#create-instance-2)
38+
- [add child](#add-child)
39+
- [search](#search)
40+
- [delete](#delete-2)
41+
- [replace](#replace)
42+
- [root](#root)
43+
- [has](#has-2)
44+
- [get parent](#get-parent)
45+
- [get children](#get-children)
46+
- [get depth](#get-depth)
47+
- [get all nodes](#get-all-nodes)
48+
- [destroy](#destroy)
3449
<!-- TOC -->
3550

3651
# Relation map
@@ -382,3 +397,94 @@ myMap.set("a", 5);
382397
myMap.set("b", 20);
383398
myMap.count(); // {"a" => 2, "b" => 1}
384399
```
400+
401+
## Tree
402+
### Term Explanation
403+
- node
404+
- parent
405+
- children
406+
- root
407+
- sub tree
408+
### Usage
409+
#### create instance
410+
```typescript
411+
const tree = new Tree<number>(1);
412+
```
413+
- Construct with the root data.
414+
- In this example, root is `1`.
415+
#### add child
416+
```typescript
417+
tree.addChild(current, child);
418+
```
419+
- Add the new node at child position of current node.
420+
- Current node should exist in tree. If not exist, it throw the error.
421+
- Child node should not exist in tree. If exist, it throw the error.
422+
#### search
423+
```typescript
424+
const subTreeNodes = tree.search(start);
425+
```
426+
- Get nodes in subtree.
427+
- Result's type is `Set<T>`, not `T[]`.
428+
#### delete
429+
```typescript
430+
tree.delete(node);
431+
```
432+
- Delete itself and all nodes in its subtree.
433+
- So, It should not delete root.
434+
#### replace
435+
```typescript
436+
tree.replace(prev, next);
437+
```
438+
- A new node replaces the prev node and the structure of tree is maintained.
439+
- Prev node should exist in tree. If not exist, it throw the error.
440+
- New node should not exist in tree. If exist, it throw the error.
441+
#### root
442+
```typescript
443+
const tree = new Tree<number>(1);
444+
console.log(tree.getRoot()) // 1
445+
console.log(tree.isRoot(2)) // false
446+
```
447+
- Use `getRoot` to get root data.
448+
- Use `isRoot` to know whether the data is root.
449+
#### has
450+
```typescript
451+
const tree = new Tree<number>(1);
452+
console.log(tree.has(1)); // true
453+
console.log(tree.has(2)); // false
454+
```
455+
- Check if a value exists.
456+
#### get parent
457+
```typescript
458+
const tree = new Tree<number>(1);
459+
tree.addChild(1, 2);
460+
console.log(tree.getParent(1)); // null
461+
console.log(tree.getParent(2)); // 1
462+
```
463+
- Get parent of node.
464+
- If node is root, this returns `null`.
465+
- After `destroy()` or node not exists, it returns `undefined`.
466+
#### get children
467+
```typescript
468+
tree.getChildren(node);
469+
```
470+
- Get direct children of the node.
471+
- If node not exists, it returns `undefined`
472+
#### get depth
473+
```typescript
474+
tree.getDepth(node);
475+
```
476+
- Get depth of the node.
477+
- Root's depth is `0`.
478+
- If node not exists, it returns `undefined`
479+
#### get all nodes
480+
```typescript
481+
tree.getAllNodes();
482+
```
483+
- Get all nodes in the tree.
484+
- It's return type is `T[]`, not `Set<T>`.
485+
#### destroy
486+
```typescript
487+
tree.destroy();
488+
```
489+
- If you want to clear memory, call this.
490+
- After call this, this tree is no longer available.

0 commit comments

Comments
 (0)