given the code below, I am bit confused as to how the order of operations happens to get an in order traversal of a binary search tree.
BinarySearchTree.prototype.inOrder = function() {
if (this.root == null) {
return null;
} else {
var result = new Array();
function traverseInOrder(node) {
node.left && traverseInOrder(node.left);
result.push(node.data);
node.right && traverseInOrder(node.right);
}
traverseInOrder(this.root);
return result;
};
}
I tried to add a debugger statement and follow but I get lost inside:
function traverseInOrder(node) {
node.left && traverseInOrder(node.left); //step 1
result.push(node.data); //step 2
node.right && traverseInOrder(node.right); //step 3
}
node.left && traverseInOrder(node.left);
(Step 1) gets run then gets run again then gets run again. Finally when there is no node.left
, Step 2 gets called : result.push(node.data);
This is the part where it loses me. Now it tries to run node.right && traverseInOrder(node.right)
but there is no node.right
but instead of moving out of the function, it goes back up to Step 2, result.push(node.data);
.
Is this queued up from the multiple recursive calls in Step 1?
given the code below, I am bit confused as to how the order of operations happens to get an in order traversal of a binary search tree.
BinarySearchTree.prototype.inOrder = function() {
if (this.root == null) {
return null;
} else {
var result = new Array();
function traverseInOrder(node) {
node.left && traverseInOrder(node.left);
result.push(node.data);
node.right && traverseInOrder(node.right);
}
traverseInOrder(this.root);
return result;
};
}
I tried to add a debugger statement and follow but I get lost inside:
function traverseInOrder(node) {
node.left && traverseInOrder(node.left); //step 1
result.push(node.data); //step 2
node.right && traverseInOrder(node.right); //step 3
}
node.left && traverseInOrder(node.left);
(Step 1) gets run then gets run again then gets run again. Finally when there is no node.left
, Step 2 gets called : result.push(node.data);
This is the part where it loses me. Now it tries to run node.right && traverseInOrder(node.right)
but there is no node.right
but instead of moving out of the function, it goes back up to Step 2, result.push(node.data);
.
Is this queued up from the multiple recursive calls in Step 1?
Share Improve this question asked Apr 18, 2020 at 6:46 burtonLowelburtonLowel 7941 gold badge10 silver badges23 bronze badges2 Answers
Reset to default 5Let's look at an example. Let it be the below tree which will be traversed in order
.
- The first
traverseInOrder
is called withthis.root
, it means the parameter from the above example isA
. Thenode.left
exists, it followstraverseInOrder
is called with parameterB
.- In the call of
B
thenode.left
exists, it followstraverseInOrder
is called with parameterD
.- In the call of
D
node.left
does not exist (node.left
is falsy), soresult.push(node.data);
is called with parameterD
. In the next stepnode.right
is falsy, it follows thetraversInOrder
with parameterD
is finished. We get back to theB
.
- In the call of
- We are back to call
B
, and because thetraversInOrder
withD
just finished,result.push(node.data)
will be called with parameterB
. And as nextnode.right
is truthy sotraverseInOrder
is called with thenode.right
so withE
.- At
E
node.left
is falsy,result.push
is called with parameterE
.node.right
is falsy is the call withE
ended here. We get back to callA
, because as we return from here to the call ofB
it finishes at this point.
- At
- In the call of
- At call with parameter
A
we just finished the left node,result.push(node.data);
is called forA
and nextnode.right
is truthy soresult.push(node.data)
is called with the right node witch means parameterC
.
And it goes on the same with the C,F,G.
tenkmilan already did an excellent job of showing how to envision that code.
Here I go a different path, and write a simpler inorder
traversal. It should be fairly clear how that can map to the code supplied.
An inorder
traversal is simple enough. preorder
and postorder
are the other most mon tree traversals and they work on arbitrary finite trees. Inorder is only defined for binary trees, and uses the left
- and right
-children of your node. The traversal order is to (recursively) traverse the left child, then visit the node itself, and finally to (recursively) traverse the right child.
We can write such a traversal simply:
const inorder = (tree) =>
tree
? [
... inorder (tree .left),
tree .data,
... inorder (tree .right)
]
: []
We have a base-case where the node we're looking at is empty, and we just return an empty array. In the general case we just concatenate together a recursive call on left .tree
with our current node's value and a recursive call on right .tree
.
That's all there is to inorder
traversal. You can see it in action in this snippet:
const inorder = (tree) =>
tree
? [
... inorder (tree .left),
tree .data,
... inorder (tree .right)
]
: []
const tree = {
data: 'F',
left: {
data: 'B',
left: {data: 'A'},
right: {
data: 'D',
left: {data: 'C'},
right: {data: 'E'}
}
},
right: {
data: 'H',
left: {data: 'G'},
right: {data: 'I'}
}
}
console .log (inorder (tree))
Of course, this is for a simple tree stored as a plain JS object. But mapping back your sample code is easy enough. I'm guessing that if you can follow this one, you can quickly follow that one.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745134815a4613149.html
评论列表(0条)