npm

@idogo/i-class

1.0.2 • Public • Published

i-class

Usually data structure constructor class.

Install

npm install @idogo/i-class --save

Usage

Browser

<script src="url/i-class/dist/i-class.umd.js"></script>
<script>
  var TreeNode = iClass.TreeNode;
</script>

Es6

import { TreeNode } from '@idogo/i-class';
const node = new TreeNode(10);

TreeNode

const TreeNode = require('@idogo/i-class').TreeNode;
const node = new TreeNode(10);

Interface class

TreeNode

construct tree node

Constructor instance:

import { TreeNode } from '@idogo/i-class';
const node = new TreeNode(10);

Constructor by object element which has required key value and optional key payLoad:

const ele = { value: 90 }; 
const node = new TreeNode(ele);
const ele = { value: 90, payLoad: 'I am old man' }; 
const node = new TreeNode(ele);

Constructor chained instances:

const root = new TreeNode(19);
root.left = new TreeNode(20);
root.right = new TreeNode(22);
const root = new TreeNode(19, new TreeNode(29), new TreeNode(22));

Other api:

  • left default:null
  • rightdefault:null
  • payLoaddefault:undefined

Heap

Constructor instance:

import { Heap } from '@idogo/i-class';
const heap = new Heap({
  data: [67, 78, 23, 56, 12, 2]
});

Constructor a min(default) or max heap with type in config:

const heap = new Heap({
  data: [67, 78, 23, 56, 12, 2],
  type: 'max'
});

Shift top element:

const heap = new Heap({
  data: [67, 78, 23, 56, 12, 2],
  type: 'max'
});
heap.shift(); // 78
heap.size(); // 5

Append an element:

import { Heap } from '@idogo/i-class';
const heap = new Heap({
  data: [67, 78, 23, 56, 12, 2]
});
heap.append(88); // [88, 67, 78, 56, 12, 2, 23]
heap.append(6); // [88, 67, 78, 56, 12, 2, 23, 6]
heap.size(); // 8

BinaryTree

Constructor instance:

import { BinaryTree } from '@idogo/i-class';
const binaryTree = new BinaryTree();

Construct a ordinary binaryTree by Array fill with Number:

const binaryTree = new BinaryTree();
const tree = binaryTree.constructTree([5, 56, 78, 8, 12]); 

Construct a ordinary binaryTree by Array fill with Object, element of Array has required key value and optional key payLoad:

const binaryTree = new BinaryTree();
const tree = binaryTree.constructTree([{ value: 5 }, { value: 56, payLoad: null }, { value: 78}]);
const source = [{ name: 'leo', age: 89 }, { name: 'jack', age: 12 }, { name: 'rose', age: 10 }, { name: 'john', age: 22 }];
const list = source.map(data => {
  const item = {};
  item.value = data.value;
  item.payLoad = data;
  return item;
});
const binaryTree = new BinaryTree();
const tree = binaryTree.constructTree(list);

Construct a Max binaryTree:

const binaryTree = new BinaryTree();
const maxTree = binaryTree.constructMaxTree([5, 56, 78, 8, 12]);

Other api:

  • insert insert a node in already existed BinaryTree instance.if existed BinaryTree instance was MaxTree or other special tree, this handler would change construction.
  • deepestLeaves return depth of BinaryTree instance.
  • nodesOfLevels return nodes of levels by Array.(eg: [[5], [6, 7], [12, 5, 7]])
  • deepestLeavesSum return sum of deepest nodes.

License

MIT.

Readme

Keywords

none

Package Sidebar

Install

npm i @idogo/i-class

Weekly Downloads

1

Version

1.0.2

License

none

Unpacked Size

281 kB

Total Files

15

Last publish

Collaborators

  • leotwe