@@ -8,7 +8,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
8
8
} ;
9
9
Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
10
10
const BinarySearchTree_1 = __importStar ( require ( "./BinarySearchTree" ) ) ;
11
- function populateTree ( tree ) {
11
+ function populateTree1 ( tree ) {
12
12
BST . insert ( 5 ) ;
13
13
BST . insert ( 3 ) ;
14
14
BST . insert ( 8 ) ;
@@ -17,6 +17,14 @@ function populateTree(tree) {
17
17
BST . insert ( 1 ) ;
18
18
BST . insert ( 9 ) ;
19
19
}
20
+ function populateTree2 ( tree ) {
21
+ BST . insert ( 10 ) ;
22
+ BST . insert ( 6 ) ;
23
+ BST . insert ( 15 ) ;
24
+ BST . insert ( 8 ) ;
25
+ BST . insert ( 20 ) ;
26
+ BST . insert ( 3 ) ;
27
+ }
20
28
let BST = new BinarySearchTree_1 . default ( ) ;
21
29
beforeEach ( ( ) => BST = new BinarySearchTree_1 . default ( ) ) ;
22
30
describe ( 'basic binary search tree functionality' , ( ) => {
@@ -40,14 +48,14 @@ describe('basic binary search tree functionality', () => {
40
48
expect ( ( _b = ( _a = BST . root ) === null || _a === void 0 ? void 0 : _a . left ) === null || _b === void 0 ? void 0 : _b . data ) . toBe ( 1 ) ;
41
49
expect ( ( _d = ( _c = BST . root ) === null || _c === void 0 ? void 0 : _c . right ) === null || _d === void 0 ? void 0 : _d . data ) . toBe ( 10 ) ;
42
50
} ) ;
51
+ } ) ;
52
+ describe ( 'insertion' , ( ) => {
43
53
test ( 'Insert method exists' , ( ) => {
44
54
expect ( typeof BST . insert ) . toBe ( 'function' ) ;
45
55
} ) ;
46
- } ) ;
47
- describe ( 'insertion' , ( ) => {
48
56
test ( 'insert method works by inserting in order (left to right ascending)' , ( ) => {
49
57
var _a , _b , _c , _d , _e , _f , _g , _h , _j , _k , _l , _m , _o , _p , _q , _r , _s ;
50
- populateTree ( BST ) ;
58
+ populateTree1 ( BST ) ;
51
59
expect ( ( _a = BST . root ) === null || _a === void 0 ? void 0 : _a . data ) . toBe ( 5 ) ;
52
60
expect ( ( _c = ( _b = BST . root ) === null || _b === void 0 ? void 0 : _b . left ) === null || _c === void 0 ? void 0 : _c . data ) . toBe ( 3 ) ;
53
61
expect ( ( _e = ( _d = BST . root ) === null || _d === void 0 ? void 0 : _d . right ) === null || _e === void 0 ? void 0 : _e . data ) . toBe ( 8 ) ;
@@ -77,7 +85,7 @@ describe('binary search', () => {
77
85
} ) ;
78
86
test ( 'searching a populated tree' , ( ) => {
79
87
var _a , _b , _c , _d , _e , _f , _g ;
80
- populateTree ( BST ) ;
88
+ populateTree1 ( BST ) ;
81
89
expect ( ( _a = BST . find ( 1 ) ) === null || _a === void 0 ? void 0 : _a . data ) . toBe ( 1 ) ;
82
90
expect ( BST . find ( 2 ) ) . toBeNull ( ) ;
83
91
expect ( ( _b = BST . find ( 3 ) ) === null || _b === void 0 ? void 0 : _b . data ) . toBe ( 3 ) ;
@@ -91,27 +99,52 @@ describe('binary search', () => {
91
99
} ) ;
92
100
describe ( 'depth first search' , ( ) => {
93
101
test ( 'can log a populated tree in ascending order' , ( ) => {
94
- populateTree ( BST ) ;
102
+ populateTree1 ( BST ) ;
95
103
let ascendingArray = [ ] ;
96
104
BST . DFS ( ( node ) => { ascendingArray . push ( node . data ) ; } ) ;
97
105
expect ( ascendingArray ) . toEqual ( [ 1 , 3 , 4 , 5 , 6 , 8 , 9 ] ) ;
98
106
} ) ;
99
107
test ( 'can log a populated tree in descending order' , ( ) => {
100
- populateTree ( BST ) ;
108
+ populateTree1 ( BST ) ;
101
109
let descendingArray = [ ] ;
102
110
BST . DFS ( ( node ) => descendingArray . push ( node . data ) , BST . root , { order : 'descending' } ) ;
103
111
expect ( descendingArray ) . toEqual ( [ 9 , 8 , 6 , 5 , 4 , 3 , 1 ] ) ;
104
112
} ) ;
113
+ test ( 'preorder DFS working' , ( ) => {
114
+ populateTree2 ( BST ) ;
115
+ const array = [ ] ;
116
+ BST . DFS ( ( node ) => array . push ( node . data ) , BST . root , { order : 'preorder' } ) ;
117
+ expect ( array ) . toEqual ( [ 10 , 6 , 3 , 8 , 15 , 20 ] ) ;
118
+ } ) ;
119
+ test ( 'postorder DFS working' , ( ) => {
120
+ populateTree2 ( BST ) ;
121
+ const array = [ ] ;
122
+ BST . DFS ( ( node ) => array . push ( node . data ) , BST . root , { order : 'postorder' } ) ;
123
+ expect ( array ) . toEqual ( [ 3 , 8 , 6 , 20 , 15 , 10 ] ) ;
124
+ } ) ;
105
125
} ) ;
106
126
describe ( 'breadth first search' , ( ) => {
107
127
test ( 'BFS exists' , ( ) => {
108
128
expect ( BST . BFS ( null , BST . root ) ) . toBeNull ( ) ;
109
129
} ) ;
110
- test ( 'BFS can print out contents of a basic tree' , ( ) => {
130
+ test ( 'BFS can accurately print out contents of a basic tree 1' , ( ) => {
131
+ populateTree2 ( BST ) ;
111
132
let array = [ ] ;
112
- populateTree ( BST ) ;
113
133
BST . BFS ( ( node ) => array . push ( node . data ) ) ;
114
- console . log ( array ) ;
134
+ expect ( array ) . toEqual ( [ 10 , 6 , 15 , 3 , 8 , 20 ] ) ;
135
+ } ) ;
136
+ test ( 'BFS can accurately print out contents of a basic tree 2' , ( ) => {
137
+ var _a ;
138
+ let array = [ ] ;
139
+ populateTree1 ( BST ) ;
140
+ BST . BFS ( ( node ) => array . push ( node . data ) ) ;
115
141
expect ( array ) . toEqual ( [ 5 , 3 , 8 , 1 , 4 , 6 , 9 ] ) ;
142
+ BST . insert ( 7 ) ;
143
+ // 7 should be passed down to lowest level of tree beneath 6
144
+ array = [ ] ;
145
+ BST . BFS ( ( node ) => array . push ( node . data ) ) ;
146
+ const sixNode = BST . find ( 6 ) ;
147
+ expect ( ( _a = sixNode === null || sixNode === void 0 ? void 0 : sixNode . right ) === null || _a === void 0 ? void 0 : _a . data ) . toBe ( 7 ) ;
148
+ expect ( array ) . toEqual ( [ 5 , 3 , 8 , 1 , 4 , 6 , 9 , 7 ] ) ;
116
149
} ) ;
117
150
} ) ;
0 commit comments