From 9a430fdbc6ff4de0ca6d4cdd3a7983b139a68c73 Mon Sep 17 00:00:00 2001 From: Revone Date: Wed, 22 Nov 2023 21:29:44 +0800 Subject: [PATCH] docs: Binary Tree print method --- README.md | 96 ++++++++++++++++--------------------- test/integration/index.html | 75 ++++++++++++++++++++++++++++- 2 files changed, 115 insertions(+), 56 deletions(-) diff --git a/README.md b/README.md index 9826e8a..200131f 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,15 @@ Data Structures of Javascript & TypeScript. Do you envy C++ with [STL]() (std::), Python with [collections](), and Java with [java.util]() ? Well, no need to envy anymore! JavaScript and TypeScript now have [data-structure-typed](). -Now you can use this library in Node.js and browser environments in CommonJS(require export.modules = ), ESModule(import export), Typescript(import export), UMD(var Queue = dataStructureTyped.Queue) +Now you can use this in Node.js and browser environments + +CommonJS:**`require export.modules =`** + +ESModule:   **`import export`** + +Typescript:   **`import export`** + +UMD:           **`var Deque = dataStructureTyped.Deque`** [//]: # (![Branches](https://img.shields.io/badge/branches-55.47%25-red.svg?style=flat)) @@ -29,7 +37,7 @@ Now you can use this library in Node.js and browser environments in CommonJS(req ### npm ```bash -npm i --save data-structure-typed +npm i data-structure-typed --save ``` ### yarn @@ -60,7 +68,7 @@ Copy the line below into the head tag in an HTML document. ``` -Copy the code below into the script tag of your HTML, and you're good to go with your development work. +Copy the code below into the script tag of your HTML, and you're good to go with your development. ```js const {Heap} = dataStructureTyped; const { @@ -142,6 +150,18 @@ bst.delete(6); bst.get(6); // undefined bst.isAVLBalanced(); // true bst.bfs()[0] === 11; // true +bst.print() +// ______________11_____ +// / \ +// ___3_______ _13_____ +// / \ / \ +// 1_ _____8____ 12 _15__ +// \ / \ / \ +// 2 4_ _10 14 16 +// \ / +// 5_ 9 +// \ +// 7 const objBST = new BST<{height: number, age: number}>(); @@ -192,13 +212,10 @@ bst.get(6); // undefined bst.isAVLBalanced(); // true or false const bfsIDs = bst.bfs(); bfsIDs[0] === 11; // true - ``` ### AVLTree snippet -#### TS - ```ts import {AVLTree} from 'data-structure-typed'; @@ -209,22 +226,30 @@ avlTree.delete(10); avlTree.isAVLBalanced(); // true ``` -#### JS +### RedBlackTree snippet -```js -const {AVLTree} = require('data-structure-typed'); +```ts +import {RedBlackTree} from 'data-structure-typed'; -const avlTree = new AVLTree(); -avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]) -avlTree.isAVLBalanced(); // true -avlTree.delete(10); -avlTree.isAVLBalanced(); // true +const rbTree = new RedBlackTree(); +rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]) +rbTree.isAVLBalanced(); // true +rbTree.delete(10); +rbTree.isAVLBalanced(); // true +rbTree.print() +// ___6________ +// / \ +// ___4_ ___11________ +// / \ / \ +// _2_ 5 _8_ ____14__ +// / \ / \ / \ +// 1 3 7 9 12__ 15__ +// \ \ +// 13 16 ``` ### Directed Graph simple snippet -#### TS or JS - ```ts import {DirectedGraph} from 'data-structure-typed'; @@ -254,8 +279,6 @@ const topologicalOrderKeys = graph.topologicalSort(); // ['A', 'B', 'C'] ### Undirected Graph snippet -#### TS or JS - ```ts import {UndirectedGraph} from 'data-structure-typed'; @@ -802,40 +825,3 @@ Array.from(dijkstraResult?.seen ?? []).map(vertex => vertex.key) // ['A', 'B', ' - - - - -- [Installation and Usage](#installation-and-usage) - - [npm](#npm) - - [yarn](#yarn) - - [CDN](#cdn) - - [development](#development) - - [production](#production) -- [Vivid Examples](#vivid-examples) - - [Binary Tree](#binary-tree) - - [Binary Tree DFS](#binary-tree-dfs) - - [AVL Tree](#avl-tree) - - [Tree Multi Map](#tree-multi-map) - - [Matrix](#matrix) - - [Directed Graph](#directed-graph) - - [Map Graph](#map-graph) -- [Code Snippets](#code-snippets) - - [Binary Search Tree (BST) snippet](#binary-search-tree-bst-snippet) - - [TS](#ts) - - [JS](#js) - - [AVLTree snippet](#avltree-snippet) - - [TS](#ts-1) - - [JS](#js-1) - - [Directed Graph simple snippet](#directed-graph-simple-snippet) - - [TS or JS](#ts-or-js) - - [Undirected Graph snippet](#undirected-graph-snippet) - - [TS or JS](#ts-or-js-1) -- [API docs & Examples](#api-docs--examples) -- [Data Structures](#data-structures) -- [Standard library data structure comparison](#standard-library-data-structure-comparison) -- [Benchmark](#benchmark) -- [Built-in classic algorithms](#built-in-classic-algorithms) -- [Software Engineering Design Standards](#software-engineering-design-standards) - - \ No newline at end of file diff --git a/test/integration/index.html b/test/integration/index.html index 7f70105..097a5d7 100644 --- a/test/integration/index.html +++ b/test/integration/index.html @@ -97,7 +97,7 @@ } console.log((performance.now() - tS).toFixed(2), `RedBlackTree ${n.toLocaleString()} add`); console.log(`rbTree.size`, rbTree.size); - for (let i = 0; i < n - 20; i++) { + for (let i = 0; i < n - 8; i++) { rbTree.delete(i) } rbTree.print(rbTree.root, { isShowRedBlackNIL: true }); @@ -142,6 +142,79 @@ } catch (e) { console.error(e); } + + try { + const { BST, RedBlackTree, AVLTree } = dataStructureTyped; + + const bst = new BST(); + bst.add(11); + bst.add(3); + bst.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]); + bst.size === 16; // true + bst.has(6); // true + const node6 = bst.getNode(6); // BSTNode + bst.getHeight(6) === 2; // true + bst.getHeight() === 5; // true + bst.getDepth(6) === 3; // true + + bst.getLeftMost()?.key === 1; // true + + bst.delete(6); + bst.get(6); // undefined + bst.isAVLBalanced(); // true + bst.bfs()[0] === 11; // true + bst.print(); + + const objBST = new BST(); + + objBST.add(11, { "name": "Pablo", "age": 15 }); + objBST.add(3, { "name": "Kirk", "age": 1 }); + + objBST.addMany([15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5], [ + { "name": "Alice", "age": 15 }, + { "name": "Bob", "age": 1 }, + { "name": "Charlie", "age": 8 }, + { "name": "David", "age": 13 }, + { "name": "Emma", "age": 16 }, + { "name": "Frank", "age": 2 }, + { "name": "Grace", "age": 6 }, + { "name": "Hannah", "age": 9 }, + { "name": "Isaac", "age": 12 }, + { "name": "Jack", "age": 14 }, + { "name": "Katie", "age": 4 }, + { "name": "Liam", "age": 7 }, + { "name": "Mia", "age": 10 }, + { "name": "Noah", "age": 5 } + ] + ); + objBST.print() + + objBST.delete(11); + objBST.print() + + + const rbTree = new RedBlackTree(); + rbTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]) + rbTree.isAVLBalanced(); // true + rbTree.delete(10); + rbTree.isAVLBalanced(); // true + console.log(`rbTree.delete(10)`); + rbTree.print(); + + rbTree.delete(14); + rbTree.print() + + const avlTree = new AVLTree(); + avlTree.addMany([11, 3, 15, 1, 8, 13, 16, 2, 6, 9, 12, 14, 4, 7, 10, 5]) + avlTree.isAVLBalanced(); // true + avlTree.delete(10); + avlTree.isAVLBalanced(); // true + avlTree.print() + avlTree.delete(14); + avlTree.print() + } catch (e) { + console.error(e); + }