2024-01-15 04:12:01 +00:00
import { AVLTree } from 'avl-tree-typed' ;
2023-08-30 09:31:49 +00:00
describe ( 'AVL Tree Test' , ( ) = > {
2023-09-19 09:00:25 +00:00
it ( 'should perform various operations on a AVL Tree' , ( ) = > {
2023-12-21 02:26:45 +00:00
const arr : [ number , number ] [ ] = [
[ 11 , 11 ] ,
[ 3 , 3 ] ,
[ 15 , 15 ] ,
[ 1 , 1 ] ,
[ 8 , 8 ] ,
[ 13 , 13 ] ,
[ 16 , 16 ] ,
[ 2 , 2 ] ,
[ 6 , 6 ] ,
[ 9 , 9 ] ,
[ 12 , 12 ] ,
[ 14 , 14 ] ,
[ 4 , 4 ] ,
[ 7 , 7 ] ,
[ 10 , 10 ] ,
[ 5 , 5 ]
] ;
2024-01-26 10:26:40 +00:00
const tree = new AVLTree < number , number > ( ) ;
2023-08-30 09:31:49 +00:00
2023-11-27 05:59:26 +00:00
for ( const i of arr ) tree . add ( i ) ;
2023-08-30 09:31:49 +00:00
2023-09-19 09:00:25 +00:00
const node6 = tree . get ( 6 ) ;
2023-08-30 09:31:49 +00:00
2023-09-19 09:00:25 +00:00
expect ( node6 && tree . getHeight ( node6 ) ) . toBe ( 3 ) ;
expect ( node6 && tree . getDepth ( node6 ) ) . toBe ( 1 ) ;
2023-08-30 09:31:49 +00:00
2023-11-01 10:19:49 +00:00
const getValueById = tree . get ( 10 ) ;
expect ( getValueById ) . toBe ( 10 ) ;
2023-08-30 09:31:49 +00:00
2023-09-19 09:00:25 +00:00
const getMinNodeByRoot = tree . getLeftMost ( ) ;
2023-10-10 13:56:55 +00:00
expect ( getMinNodeByRoot ? . key ) . toBe ( 1 ) ;
2023-08-30 09:31:49 +00:00
2023-11-01 10:19:49 +00:00
const node15 = tree . getNode ( 15 ) ;
2024-10-29 04:27:40 +00:00
const getMinNodeBySpecificNode = node15 && tree . getLeftMost ( node = > node , node15 ) ;
2023-10-10 13:56:55 +00:00
expect ( getMinNodeBySpecificNode ? . key ) . toBe ( 12 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
let subTreeSum = 0 ;
2024-01-14 11:49:50 +00:00
node15 && tree . dfs ( node = > ( subTreeSum += node . key ) , 'IN' , 15 ) ;
2023-09-19 09:00:25 +00:00
expect ( subTreeSum ) . toBe ( 70 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
let lesserSum = 0 ;
2024-01-26 10:26:40 +00:00
tree . lesserOrGreaterTraverse ( node = > ( lesserSum += node . key ) , - 1 , 10 ) ;
2023-09-19 09:00:25 +00:00
expect ( lesserSum ) . toBe ( 45 ) ;
2023-08-30 09:31:49 +00:00
2023-09-19 09:00:25 +00:00
// node15 has type problem. After the uniform design, the generics of containers (DirectedGraph, BST) are based on the type of value. However, this design has a drawback: when I attempt to inherit from the Vertex or BSTNode classes, the types of the results obtained by all methods are those of the parent class.
2023-10-31 03:22:21 +00:00
expect ( node15 ? . value ) . toBe ( 15 ) ;
2023-08-30 09:31:49 +00:00
2024-01-14 11:49:50 +00:00
const dfs = tree . dfs ( node = > node , 'IN' ) ;
2023-10-10 13:56:55 +00:00
expect ( dfs [ 0 ] . key ) . toBe ( 1 ) ;
expect ( dfs [ dfs . length - 1 ] . key ) . toBe ( 16 ) ;
2023-08-30 09:31:49 +00:00
2023-09-19 09:00:25 +00:00
tree . perfectlyBalance ( ) ;
2023-10-23 13:22:07 +00:00
const bfs = tree . bfs ( node = > node ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isPerfectlyBalanced ( ) ) . toBe ( true ) ;
2023-10-10 13:56:55 +00:00
expect ( bfs [ 0 ] . key ) . toBe ( 8 ) ;
expect ( bfs [ bfs . length - 1 ] . key ) . toBe ( 16 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 11 ) [ 0 ] . deleted ? . key ) . toBe ( 11 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( node15 && tree . getHeight ( node15 ) ) . toBe ( 2 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 1 ) [ 0 ] . deleted ? . key ) . toBe ( 1 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 4 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 4 ) [ 0 ] . deleted ? . key ) . toBe ( 4 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 4 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 10 ) [ 0 ] . deleted ? . key ) . toBe ( 10 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 3 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 15 ) [ 0 ] . deleted ? . key ) . toBe ( 15 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
2023-08-30 09:31:49 +00:00
2023-09-19 09:00:25 +00:00
expect ( tree . getHeight ( ) ) . toBe ( 3 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 5 ) [ 0 ] . deleted ? . key ) . toBe ( 5 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 3 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 13 ) [ 0 ] . deleted ? . key ) . toBe ( 13 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 3 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 3 ) [ 0 ] . deleted ? . key ) . toBe ( 3 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 3 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 8 ) [ 0 ] . deleted ? . key ) . toBe ( 8 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 3 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 6 ) [ 0 ] . deleted ? . key ) . toBe ( 6 ) ;
expect ( tree . delete ( 6 ) . length ) . toBe ( 0 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 2 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 7 ) [ 0 ] . deleted ? . key ) . toBe ( 7 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 2 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 9 ) [ 0 ] . deleted ? . key ) . toBe ( 9 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 2 ) ;
2023-10-23 13:22:07 +00:00
expect ( tree . delete ( 14 ) [ 0 ] . deleted ? . key ) . toBe ( 14 ) ;
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
expect ( tree . getHeight ( ) ) . toBe ( 1 ) ;
2023-08-30 09:31:49 +00:00
2023-09-19 09:00:25 +00:00
expect ( tree . isAVLBalanced ( ) ) . toBe ( true ) ;
2023-10-11 15:36:10 +00:00
const lastBFSIds = tree . bfs ( ) ;
2023-09-19 09:00:25 +00:00
expect ( lastBFSIds [ 0 ] ) . toBe ( 12 ) ;
expect ( lastBFSIds [ 1 ] ) . toBe ( 2 ) ;
expect ( lastBFSIds [ 2 ] ) . toBe ( 16 ) ;
2023-08-30 09:31:49 +00:00
2023-10-23 13:22:07 +00:00
const lastBFSNodes = tree . bfs ( node = > node ) ;
2023-10-10 13:56:55 +00:00
expect ( lastBFSNodes [ 0 ] . key ) . toBe ( 12 ) ;
expect ( lastBFSNodes [ 1 ] . key ) . toBe ( 2 ) ;
expect ( lastBFSNodes [ 2 ] . key ) . toBe ( 16 ) ;
2023-09-19 09:00:25 +00:00
} ) ;
2023-08-30 09:31:49 +00:00
} ) ;