英文:
In a tree with arbitrary number of children, find whether a node is in the left half subtrees or right half subtrees of a given node
问题
Question: 查找节点是否位于节点root的左半子树或右半子树中。
Meaning of left half and right half: 假设节点root有n个子节点,每个子节点都有子树。
如果一个节点是从左到右编号为[1..(Math.floor(n/2))]
的节点或是其中一个子树中的节点,那么它被认为位于root的左半子树中;否则,它位于root的右半子树中[(Math.floor(n/2) + 1)..n]
。
左半子树中的所有节点的key
值都小于root,右半子树中的所有节点的key
值都大于root。
Idea: 预处理
对树进行中序遍历,并为adjacencyLists.node
中的每个节点分配key(整数)。然后,在查询时比较key
值,以确定节点是否位于左半子树或右半子树:
伪代码:
QUERY(root, queryNode)
if (queryNode.key < root.key) {
print "queryNode is in left half of root"
} else {
print "queryNode is in the right half of root"
}
Implementation:
var adjacencyLists = {
root: 'A',
nodes: {
A: {
id: 'A',
connectedNodes: ['B', 'C']
},
// ... (其他节点的信息)
}
}
var keyLookup = {};
var count = 0;
function inorderTraversalNumberingOfNodes(cur) {
if (adjacencyLists.nodes[cur].connectedNodes.length) {
// 递归左半子树
for (let i = 0; i < Math.ceil(adjacencyLists.nodes[cur].connectedNodes.length / 2); i++) {
inorderTraversalNumberingOfNodes(adjacencyLists.nodes[cur].connectedNodes[i]);
}
// 递归右半子树
for (let i = Math.ceil(adjacencyLists.nodes[cur].connectedNodes.length / 2); i < adjacencyLists.nodes[cur].connectedNodes.length; i++) {
inorderTraversalNumberingOfNodes(adjacencyLists.nodes[cur].connectedNodes[i]);
}
count++;
keyLookup[cur] = { key: count };
} else {
count++;
keyLookup[cur] = {key : count };
}
}
inorderTraversalNumberingOfNodes(adjacencyLists.root);
console.log(keyLookup)
// 查询节点是否位于根节点的左半部分或右半部分
function query(rootNodeId, queryNodeId) {
if (keyLookup[queryNodeId].key < keyLookup[rootNodeId].key) {
console.log(`query node ${queryNodeId} is in the left half of root node ${rootNodeId}`);
} else {
console.log(`query node ${queryNodeId} is in the right half of root node ${rootNodeId}`);
}
}
query('A', 'D');
query('M', 'C');
预期节点的key值: 对邻接列表进行中序遍历应为节点分配以下key值:
{
"D": {
"key": 1
},
"K": {
"key": 3
},
"E": {
"key": 4
},
"B": {
"key": 2
},
// ... (其他节点的key值)
}
现在,在QUERY(A, D)
上,输出应为queryNode is in left half of root
,因为1 < 5
。您之前未能获得预期的答案,因为未能正确分配节点的key
值。
英文:
Question: To find whether a node is in the left half subtrees or the right half subtrees of a node root.
Meaning of left half and right half : Suppose a node root has n child and each of which has subtrees.
A node is said to be in the left half subtrees of root, if it is a node numbered [1..(Math.floor(n/2))]
from left to right or it is a node in one of their subtrees, else it is in the right half subtrees of root [(Math.floor(n/2) + 1)..n]
.
All the nodes in the left half get key
values less than root and all the nodes in the right half get key
values greater than root.
Idea: Preprocessing
Do an inorder traversal of the tree and assign key(Whole numbers) to each node in adjacencyLists.node
. Then on query, compare key
values to determine whether a node is in the left half subtrees or right half subtrees:
Pseudo code:
QUERY(root, queryNode)
if (queryNode.key < root.key) {
print "queryNode is in left half of root"
} else {
print "queryNode is in the right half of root"
}
Implementation:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var adjacencyLists = {
root: 'A',
nodes: {
A: {
id: 'A',
connectedNodes: ['B', 'C']
},
B: {
id: 'B',
connectedNodes: ['D', 'E']
},
C: {
id: 'C',
connectedNodes: ['F', 'G', 'H', 'Q', 'R']
},
D: {
id: 'D',
connectedNodes: []
},
E: {
id: 'E',
connectedNodes: ['K']
},
F: {
id: 'F',
connectedNodes: ['I']
},
G: {
id: 'G',
connectedNodes: ['J', 'L', 'N', 'P']
},
H: {
id: 'H',
connectedNodes: ['M', 'O']
},
K: {
id: 'K',
connectedNodes: []
},
I: {
id: 'I',
connectedNodes: []
},
J: {
id: 'J',
connectedNodes: []
},
L: {
id: 'L',
connectedNodes: []
},
M: {
id: 'M',
connectedNodes: []
},
N: {
id: 'N',
connectedNodes: []
},
O: {
id: 'O',
connectedNodes: []
},
P: {
id: 'P',
connectedNodes: []
},
Q: {
id: 'Q',
connectedNodes: []
},
R: {
id: 'R',
connectedNodes: []
},
}
}
var keyLookup = {};
var count = 0;
function inorderTraversalNumberingOfNodes(cur) {
if (adjacencyLists.nodes[cur].connectedNodes.length) {
// recurse left half subtrees
for (let i = 0; i < Math.ceil(adjacencyLists.nodes[cur].connectedNodes.length / 2); i++) {
inorderTraversalNumberingOfNodes(adjacencyLists.nodes[cur].connectedNodes[i]);
}
// recurse right half subtrees
for (let i = Math.ceil(adjacencyLists.nodes[cur].connectedNodes.length / 2); i < adjacencyLists.nodes[cur].connectedNodes.length; i++) {
inorderTraversalNumberingOfNodes(adjacencyLists.nodes[cur].connectedNodes[i]);
}
count++;
keyLookup[cur] = { key: count };
} else {
count++;
keyLookup[cur] = {key : count };
}
}
inorderTraversalNumberingOfNodes(adjacencyLists.root);
console.log(keyLookup)
// query to determine whether a node is in the left half or right half of root
function query(rootNodeId, queryNodeId) {
if (keyLookup[queryNodeId].key < keyLookup[rootNodeId].key) {
console.log(`query node ${queryNodeId} is in the left half of root node ${rootNodeId}`);
} else {
console.log(`query node ${queryNodeId} is in the right half of root node ${rootNodeId}`);
}
}
query('A', 'D');
query('M', 'C');
<!-- end snippet -->
Expected key values of nodes: An inorder traversal of the adjacency list should assign following key to nodes:
{
"D": {
"key": 1
},
"K": {
"key": 3
},
"E": {
"key": 4
},
"B": {
"key": 2
},
"I": {
"key": 6
},
"F": {
"key": 7
},
"J": {
"key": 8
},
"L": {
"key": 9
},
"N": {
"key": 11
},
"P": {
"key": 12
},
"G": {
"key": 10
},
"M": {
"key": 14
},
"O": {
"key": 16
},
"H": {
"key": 15
},
"Q": {
"key": 17
},
"R": {
"key": 18
},
"C": {
"key": 13
},
"A": {
"key": 5
}
}
Now, on QUERY(A, D)
, the output should be queryNode is in left half of root
, since 1 < 5
.
I don't get the expected answer since I am unable to assign correct key
to nodes.
答案1
得分: 1
你可以首先获取订单,然后获取用于获取侧边的索引值。
var adjacencyLists = { root: 'A', nodes: { A: { id: 'A', connectedNodes: ['B', 'C'] }, B: { id: 'B', connectedNodes: ['D', 'E'] }, C: { id: 'C', connectedNodes: ['F', 'G', 'H', 'Q', 'R'] }, D: { id: 'D', connectedNodes: [] }, E: { id: 'E', connectedNodes: ['K'] }, F: { id: 'F', connectedNodes: ['I'] }, G: { id: 'G', connectedNodes: ['J', 'L', 'N', 'P'] }, H: { id: 'H', connectedNodes: ['M', 'O'] }, K: { id: 'K', connectedNodes: [] }, I: { id: 'I', connectedNodes: [] }, J: { id: 'J', connectedNodes: [] }, L: { id: 'L', connectedNodes: [] }, M: { id: 'M', connectedNodes: [] }, N: { id: 'N', connectedNodes: [] }, O: { id: 'O', connectedNodes: [] }, P: { id: 'P', connectedNodes: [] }, Q: { id: 'Q', connectedNodes: [] }, R: { id: 'R', connectedNodes: [] } } },
index = 0,
getOrder = parent => value => {
if (!adjacencyLists.nodes[value].connectedNodes.length) {
adjacencyLists.nodes[value].index = adjacencyLists.nodes[value].index || ++index;
}
adjacencyLists.nodes[value].connectedNodes.forEach(getOrder(value));
adjacencyLists.nodes[parent].index = adjacencyLists.nodes[parent].index || ++index;
},
query = (a, b) => a === b
? 'middle'
: adjacencyLists.nodes[a].index < adjacencyLists.nodes[b].index
? 'left'
: 'right';
getOrder('A')('A');
console.log(query('A', 'D')); // 或反之亦然...?
console.log(adjacencyLists);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:
You could get the order first and then take the index value for getting the side.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var adjacencyLists = { root: 'A', nodes: { A: { id: 'A', connectedNodes: ['B', 'C'] }, B: { id: 'B', connectedNodes: ['D', 'E'] }, C: { id: 'C', connectedNodes: ['F', 'G', 'H', 'Q', 'R'] }, D: { id: 'D', connectedNodes: [] }, E: { id: 'E', connectedNodes: ['K'] }, F: { id: 'F', connectedNodes: ['I'] }, G: { id: 'G', connectedNodes: ['J', 'L', 'N', 'P'] }, H: { id: 'H', connectedNodes: ['M', 'O'] }, K: { id: 'K', connectedNodes: [] }, I: { id: 'I', connectedNodes: [] }, J: { id: 'J', connectedNodes: [] }, L: { id: 'L', connectedNodes: [] }, M: { id: 'M', connectedNodes: [] }, N: { id: 'N', connectedNodes: [] }, O: { id: 'O', connectedNodes: [] }, P: { id: 'P', connectedNodes: [] }, Q: { id: 'Q', connectedNodes: [] }, R: { id: 'R', connectedNodes: [] } } },
index = 0,
getOrder = parent => value => {
if (!adjacencyLists.nodes[value].connectedNodes.length) {
adjacencyLists.nodes[value].index = adjacencyLists.nodes[value].index || ++index;
}
adjacencyLists.nodes[value].connectedNodes.forEach(getOrder(value));
adjacencyLists.nodes[parent].index = adjacencyLists.nodes[parent].index || ++index;
},
query = (a, b) => a === b
? 'middle'
: adjacencyLists.nodes[a].index < adjacencyLists.nodes[b].index
? 'left'
: 'right';
getOrder('A')('A');
console.log(query('A', 'D')); // or vice versa ...?
console.log(adjacencyLists);
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论