首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >给定一个表示层次结构的数组,将数据输出到JS中的树形式

给定一个表示层次结构的数组,将数据输出到JS中的树形式
EN

Stack Overflow用户
提问于 2018-05-30 06:33:55
回答 2查看 100关注 0票数 1

给定一个数据文件,该数据文件具有表示层次结构的数组。通过在Javascript中编写脚本来创建树数据结构。以树的形式输出数据:

数据文件:

代码语言:javascript
复制
["transportation.cars.Mazda",
 "transportation.cars.Honda",
 "transportation.cars.Toyota",
 "transportation.train.lightRail",
 "transportation.train.rapidTransit",
 "transportation.waterVehicle.ferry",
 "transportation.waterVehicle.boats" 
 ...]

以树的形式输出:

代码语言:javascript
复制
 root
  transportation
    cars
      Mazda
      Honda
      Toyota
    train 
      lightRail
      rapidTransit
    waterVehicle
      ferry
      boats 

我的尝试是:

代码语言:javascript
复制
var root = new Node('root'); 

var arr = ["transportation.cars.Mazda",
 "transportation.cars.Honda",
 "transportation.cars.Toyota",
 "transportation.train.lightRail",
 "transportation.train.rapidTransit",
 "transportation.waterVehicle.ferry",
 "transportation.waterVehicle.boats" 
 ]

for(var i of arr){
   var res=i.split(".");
    root.addChild(new Node(res[0]));
    res[0].addChild(new Node(res[1])); 
    res[1].addChild(new Node(res[2])); 
 }

this.addChild = function(node) {
    node.setParentNode(this);
    this.children[this.children.length] = node;
} 
console.log(root);

我正在尝试使用JavaScript创建一个树结构,但它不具有与Java语言相同的功能(即,除非使用Typescript,否则它没有类方法。)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-30 07:39:54

您可以使用类似于trie树的东西。添加节点的方式必须更加具体。但是像这样的事情是有可能的。

代码语言:javascript
复制
function Node(word)
{
  this.value = word;
  this.children = {};
}

function AddDotChain(chain)
{
  let arr = chain.split('.');
  let currentNode = this;

  function recurse(currentIndex)
  {
    if(currentIndex === arr.length)
    {
      return;
    }

    let currentWord = arr[currentIndex];
    if(currentNode.children[currentWord])
    {
      currentNode = currentNode[currentWord];
      return recurse(currentIndex + 1);
    }

    let child = new Node(currentWord);
    currentNode.children[currentWord] = child;
    currentNode = child;
    return recurse(currentIndex + 1);
  }
}

你只需把整条链子插进去就行了。我的逻辑中可能有一个缺陷,但总体思路应该是可行的。如果你想减少递归的开销,这也可以迭代完成。请原谅我的凌乱,我试着尽快把它打出来。

下面是一个在repl.it上草率的实现。

票数 1
EN

Stack Overflow用户

发布于 2018-05-30 08:01:10

你可以做到这一点,在数据结构为Tree的情况下,您只需循环遍历包含数据的字符串数组,并按点拆分它们,然后将每个项目添加到树实例中,该实例将在您执行将数组作为tree数据结构输出的函数时创建。

这段代码可以帮助你

代码语言:javascript
复制
var arr = ["transportation.cars.Mazda",
 "transportation.cars.Honda",
 "transportation.cars.Toyota",
 "transportation.train.lightRail",
 "transportation.train.rapidTransit",
 "transportation.waterVehicle.ferry",
 "transportation.waterVehicle.boats" 
 ];

function Node(data) {
	this.data = data;
	this.children = [];
}

function Tree(data) {
	this.root = null;
}

Tree.prototype.contains = function(data) {
	return this.find(data) ? true : false;
}

Tree.prototype.add = function(data, node) {
	const newNode = new Node(data);

	if (!this.root)	{
		this.root = newNode;
		return;
	}

	const parent = node ? this.find(node) : null;
	
	if (parent) {
		if (!this.contains(data)) {
				parent.children.push(newNode);
		}
	}
}

Tree.prototype.find = function(data) {
	if (this.root) {
		const queue = [this.root];
		while(queue.length) {
			const node = queue.shift();
			if (node && node.data === data) {
				return node;
			}

			for(var i = 0; i < node.children.length; i++) {
				const child = node.children[i];
				queue.push(child);
			}
		}
	}
	return null;
}

function createTreeOfTransportation(arr) {
	const tree = new Tree();
	for(var i = 0; i < arr.length; i++) {
		const element = arr[i];
		const nodes = element.split('.');

		for (var j = 0; j < nodes.length; j++) {
			const currentNode = nodes[j];
			const parent = nodes[j-1];
			console.log(j, parent);
			tree.add(currentNode, parent);
		}
	}
	
	return tree;
}

console.log(createTreeOfTransportation(arr));

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50593926

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档