|
25 | 25 |
|
26 | 26 | #### create instance
|
27 | 27 |
|
28 |
| -```typescript |
29 |
| -const tree = new Tree<number>(1); |
30 |
| -``` |
31 |
| - |
32 | 28 | - Construct with the root data.
|
33 | 29 | - In this example, root is `1`.
|
34 | 30 |
|
35 |
| -#### add child |
36 |
| - |
37 | 31 | ```typescript
|
38 |
| -tree.addChild(current, child); |
| 32 | +const tree = new Tree<number>(1); |
39 | 33 | ```
|
40 | 34 |
|
| 35 | +#### add child |
| 36 | + |
41 | 37 | - Add the new node at child position of current node.
|
42 | 38 | - Current node should exist in tree. If not exist, it throw the error.
|
43 | 39 | - Child node should not exist in tree. If exist, it throw the error.
|
44 | 40 |
|
45 |
| -#### search |
46 | 41 |
|
47 | 42 | ```typescript
|
48 |
| -const subTreeNodes = tree.search(start); |
| 43 | +tree.addChild(current, child); |
49 | 44 | ```
|
50 | 45 |
|
| 46 | +#### search |
| 47 | + |
51 | 48 | - Get nodes in subtree.
|
52 | 49 | - Result's type is `Set<T>`, not `T[]`.
|
53 | 50 |
|
54 |
| -#### delete |
55 |
| - |
56 | 51 | ```typescript
|
57 |
| -tree.delete(node); |
| 52 | +const subTreeNodes = tree.search(start); |
58 | 53 | ```
|
59 | 54 |
|
| 55 | +#### delete |
| 56 | + |
60 | 57 | - Delete itself and all nodes in its subtree.
|
61 | 58 | - So, It should not delete root.
|
62 | 59 |
|
63 |
| -#### replace |
64 |
| - |
65 | 60 | ```typescript
|
66 |
| -tree.replace(prev, next); |
| 61 | +tree.delete(node); |
67 | 62 | ```
|
68 | 63 |
|
| 64 | +#### replace |
| 65 | + |
69 | 66 | - A new node replaces the prev node and the structure of tree is maintained.
|
70 | 67 | - Prev node should exist in tree. If not exist, it throw the error.
|
71 | 68 | - New node should not exist in tree. If exist, it throw the error.
|
72 | 69 |
|
| 70 | + |
| 71 | +```typescript |
| 72 | +tree.replace(prev, next); |
| 73 | +``` |
| 74 | + |
73 | 75 | #### root
|
74 | 76 |
|
| 77 | +- Use `getRoot` to get the root data. |
| 78 | +- Use `isRoot` to know whether the data is root. |
| 79 | + |
75 | 80 | ```typescript
|
76 | 81 | const tree = new Tree<number>(1);
|
77 | 82 | console.log(tree.getRoot()); // 1
|
78 | 83 | console.log(tree.isRoot(2)); // false
|
79 | 84 | ```
|
80 | 85 |
|
81 |
| -- Use `getRoot` to get root data. |
82 |
| -- Use `isRoot` to know whether the data is root. |
83 |
| - |
84 | 86 | #### has
|
85 | 87 |
|
| 88 | +- Check if a value exists. |
| 89 | + |
86 | 90 | ```typescript
|
87 | 91 | const tree = new Tree<number>(1);
|
88 | 92 | console.log(tree.has(1)); // true
|
89 | 93 | console.log(tree.has(2)); // false
|
90 | 94 | ```
|
91 | 95 |
|
92 |
| -- Check if a value exists. |
93 |
| - |
94 | 96 | #### get parent
|
95 | 97 |
|
| 98 | +- Get parent of node. |
| 99 | +- If node is root, this returns `null`. |
| 100 | +- After `destroy()` or node not exists, it returns `undefined`. |
| 101 | + |
96 | 102 | ```typescript
|
97 | 103 | const tree = new Tree<number>(1);
|
98 | 104 | tree.addChild(1, 2);
|
99 | 105 | console.log(tree.getParent(1)); // null
|
100 | 106 | console.log(tree.getParent(2)); // 1
|
101 | 107 | ```
|
102 | 108 |
|
103 |
| -- Get parent of node. |
104 |
| -- If node is root, this returns `null`. |
105 |
| -- After `destroy()` or node not exists, it returns `undefined`. |
106 |
| - |
107 | 109 | #### get children
|
108 | 110 |
|
109 |
| -```typescript |
110 |
| -tree.getChildren(node); |
111 |
| -``` |
112 |
| - |
113 | 111 | - Get direct children of the node.
|
114 | 112 | - If node not exists, it returns `undefined`
|
115 | 113 |
|
116 |
| -#### get depth |
117 |
| - |
118 | 114 | ```typescript
|
119 |
| -tree.getDepth(node); |
| 115 | +tree.getChildren(node); |
120 | 116 | ```
|
121 | 117 |
|
| 118 | +#### get depth |
| 119 | + |
122 | 120 | - Get depth of the node.
|
123 | 121 | - Root's depth is `0`.
|
124 | 122 | - If node not exists, it returns `undefined`
|
125 | 123 |
|
126 |
| -#### get all nodes |
127 |
| - |
128 | 124 | ```typescript
|
129 |
| -tree.getAllNodes(); |
| 125 | +tree.getDepth(node); |
130 | 126 | ```
|
131 | 127 |
|
| 128 | +#### get all nodes |
| 129 | + |
132 | 130 | - Get all nodes in the tree.
|
133 | 131 | - It's return type is `T[]`, not `Set<T>`.
|
134 | 132 |
|
135 |
| -#### destroy |
136 |
| - |
137 | 133 | ```typescript
|
138 |
| -tree.destroy(); |
| 134 | +tree.getAllNodes(); |
139 | 135 | ```
|
140 | 136 |
|
| 137 | +#### destroy |
| 138 | + |
141 | 139 | - If you want to clear memory, call this.
|
142 | 140 | - After call this, this tree is no longer available.
|
| 141 | + |
| 142 | +```typescript |
| 143 | +tree.destroy(); |
| 144 | +``` |
0 commit comments