Giter Club home page Giter Club logo

tree-chain's Introduction

tree-chain

A data structure in which each tree node is sequentially concatenated into a linked list based on a tree structure for Typescript. Support node inserting, deleting, moving, and supporting flat the tree to an array.

Structure image:

Structure_Image

start

npm install tree-chain

Build

npm run build

Usage

TreeChain

Properties

  • .map data map.
  • .chain head of tree chain.
  • .topLevelNodes return nodes of the top level of the tree.
  • .chainLength return the length of the chain.

methods

  • insertNodeIntoHead(node: **TreeChainNode<Data>**)
  • insertNodeIntoTarget(node: **TreeChainNode<Data>**, target: TreeChainNode<Data>, isTargetChild = false)
  • insertNodeByKey(node: **TreeChainNode<Data>**, key)
  • deleteNode(node: **TreeChainNode<Data>**)
  • deleteNodeByKey(key)
  • moveNode(from: **TreeChainNode<Data>**, to: **TreeChainNode<Data>**)
  • moveNodeByKey(fromKey, toKey)
  • getNodesByLevel(level = 0)
  • each(callback)
  • findNodeByKey(key)
  • toArray(config: ConfigToArray<Data>)

Static methods

  • TreeChain.create<TreeData>(source: TreeData[], config?: ConfigCreateTreeChain)
  • TreeChain.createTreeChain<TreeData>((source: TreeData[], config?: ConfigCreateTreeChain)
  • TreeChain.createTreeChainNode<TreeNodeData>(source: TreeNodeData, config?:ConfigCreateTreeChain)
  • TreeChain.findNodeByKeyFromChain(key, chain: TreeChain)
  • TreeChain.eachChain(node: **TreeChainNode<Data>**, callback)
  • TreeChain.getTreeCHainHeadByNode(node: **TreeChainNode<Data>**)

TreeChainNode

properties

  • .key
  • .prevNode
  • .nextNode
  • .parentNode
  • .siblingNextNode
  • .childNodes
  • .includeNodes
  • .ancestors
  • .firstAncestor
  • .tail
  • .leaf
  • .level
  • .nodeSize

methods

  • onDetach(callback)
  • onInsert(callback)
  • onDestroy(callback)
  • each(callback)
  • eachParent(callback)
  • clean
  • detach
  • insertInto(left: **TreeChainNode<Data>**, right: **TreeChainNode<Data>**)
  • insertChild(node: **TreeChainNode<Data>**)
  • addChild(node: **TreeChainNode<Data>**)
  • addChildIfHasNoChildNodes(node: **TreeChainNode<Data>**)
  • destroy

Type

ConfigToArray

  • filter?: FilterFn<Data>
  • keepAncestorsWithChildren?: boolean
  • keepParentWithoutChildren?: boolean
  • sort?: SortFn<Data>
  • startKey?: PropertyKey
  • count?: number

ConfigCreateTreeChain

  • childrenKey?: PropertyKey
  • dataKey?: PropertyKey

Example

const MOCK_DATA: Data[] = [
	{
		id: 0,
		name: "tom",
		children: [
			{
				id: 1,
				name: "jerry",
			},
			{
				id: 2,
				name: "john",
			},
		],
	},
	{
		id: 3,
		name: "halo",
		children: [
			{
				id: 4,
				name: "rox",
				children: [
					{
						id: 5,
						name: "holy",
						children: [
							{
								id: 6,
								name: "xekin",
							},
						],
					},
					{
						id: 7,
						name: "siri",
					},
					{
						id: 8,
						name: "san",
					},
					{
						id: 9,
						name: "dan",
						children: [
							{
								id: 10,
								name: "danny",
							},
							{
								id: 11,
								name: "ally",
							},
						],
					},
				],
			},
			{
				id: 12,
				name: "neo",
			},
		],
	},
	{
		id: 13,
		name: "belly",
	},
];

const treeChain = TreeChain.create(MOCK_DATA);

const arr = treeChain.toArray({
	filter: (node) => ![6, 9, 10].includes(Number(node.key)),
	keepAncestorsWithChildren: true,
	keepParentWithoutChildren: false,
	sort: (a, b) => Number(b.key) - Number(a.key),
	startKey: 5,
	count: 5,
});

arr.map((node) => node.key.toString());
// [12,9,11,8,7];

TODO

  • finish README.md
  • others...

tree-chain's People

Contributors

xekin97 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.