New posts
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
Sample tree: every traversal, recursive and iterative
|
||||
=====================================================
|
||||
|
||||
tree (value(left,right), - = no child): 1(2(4,5(7,-)),3(-,6(8,9)))
|
||||
|
||||
recursive inorder : [4, 2, 7, 5, 1, 3, 8, 6, 9]
|
||||
recursive preorder : [1, 2, 4, 5, 7, 3, 6, 8, 9]
|
||||
recursive postorder : [4, 7, 5, 2, 8, 9, 6, 3, 1]
|
||||
|
||||
iterative inorder : [4, 2, 7, 5, 1, 3, 8, 6, 9]
|
||||
iterative preorder : [1, 2, 4, 5, 7, 3, 6, 8, 9]
|
||||
iterative postorder (2 stacks): [4, 7, 5, 2, 8, 9, 6, 3, 1]
|
||||
iterative postorder (1 stack) : [4, 7, 5, 2, 8, 9, 6, 3, 1]
|
||||
|
||||
Morris inorder : [4, 2, 7, 5, 1, 3, 8, 6, 9]
|
||||
Morris preorder : [1, 2, 4, 5, 7, 3, 6, 8, 9]
|
||||
tree after Morris : 1(2(4,5(7,-)),3(-,6(8,9))) (unchanged)
|
||||
|
||||
level order (queue) : [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||
level order by level : [[1], [2, 3], [4, 5, 6], [7, 8, 9]]
|
||||
zigzag : [[1], [3, 2], [4, 5, 6], [9, 8, 7]]
|
||||
@@ -0,0 +1,22 @@
|
||||
Iterative inorder on the sample tree, step by step
|
||||
==================================================
|
||||
|
||||
step | action | stack (bottom..top) | output so far
|
||||
1 | push 1 | [1] | []
|
||||
2 | push 2 | [1 2] | []
|
||||
3 | push 4 | [1 2 4] | []
|
||||
4 | pop 4, visit | [1 2] | [4]
|
||||
5 | pop 2, visit | [1] | [4 2]
|
||||
6 | push 5 | [1 5] | [4 2]
|
||||
7 | push 7 | [1 5 7] | [4 2]
|
||||
8 | pop 7, visit | [1 5] | [4 2 7]
|
||||
9 | pop 5, visit | [1] | [4 2 7 5]
|
||||
10 | pop 1, visit | [] | [4 2 7 5 1]
|
||||
11 | push 3 | [3] | [4 2 7 5 1]
|
||||
12 | pop 3, visit | [] | [4 2 7 5 1 3]
|
||||
13 | push 6 | [6] | [4 2 7 5 1 3]
|
||||
14 | push 8 | [6 8] | [4 2 7 5 1 3]
|
||||
15 | pop 8, visit | [6] | [4 2 7 5 1 3 8]
|
||||
16 | pop 6, visit | [] | [4 2 7 5 1 3 8 6]
|
||||
17 | push 9 | [9] | [4 2 7 5 1 3 8 6]
|
||||
18 | pop 9, visit | [] | [4 2 7 5 1 3 8 6 9]
|
||||
@@ -0,0 +1,13 @@
|
||||
Iterative preorder on the sample tree, step by step
|
||||
===================================================
|
||||
|
||||
step | action | stack (bottom..top) | output so far
|
||||
1 | pop 1, visit, push its children | [3 2] | [1]
|
||||
2 | pop 2, visit, push its children | [3 5 4] | [1 2]
|
||||
3 | pop 4, visit, push its children | [3 5] | [1 2 4]
|
||||
4 | pop 5, visit, push its children | [3 7] | [1 2 4 5]
|
||||
5 | pop 7, visit, push its children | [3] | [1 2 4 5 7]
|
||||
6 | pop 3, visit, push its children | [6] | [1 2 4 5 7 3]
|
||||
7 | pop 6, visit, push its children | [9 8] | [1 2 4 5 7 3 6]
|
||||
8 | pop 8, visit, push its children | [9] | [1 2 4 5 7 3 6 8]
|
||||
9 | pop 9, visit, push its children | [] | [1 2 4 5 7 3 6 8 9]
|
||||
@@ -0,0 +1,22 @@
|
||||
Iterative postorder with two stacks, step by step
|
||||
=================================================
|
||||
|
||||
step | action | s1 (bottom..top) | s2 (bottom..top) | output so far
|
||||
1 | move 1 to s2, push its children on s1 | [2 3] | [1] | []
|
||||
2 | move 3 to s2, push its children on s1 | [2 6] | [1 3] | []
|
||||
3 | move 6 to s2, push its children on s1 | [2 8 9] | [1 3 6] | []
|
||||
4 | move 9 to s2, push its children on s1 | [2 8] | [1 3 6 9] | []
|
||||
5 | move 8 to s2, push its children on s1 | [2] | [1 3 6 9 8] | []
|
||||
6 | move 2 to s2, push its children on s1 | [4 5] | [1 3 6 9 8 2] | []
|
||||
7 | move 5 to s2, push its children on s1 | [4 7] | [1 3 6 9 8 2 5] | []
|
||||
8 | move 7 to s2, push its children on s1 | [4] | [1 3 6 9 8 2 5 7] | []
|
||||
9 | move 4 to s2, push its children on s1 | [] | [1 3 6 9 8 2 5 7 4] | []
|
||||
10 | pop 4 from s2 to output | [] | [1 3 6 9 8 2 5 7] | [4]
|
||||
11 | pop 7 from s2 to output | [] | [1 3 6 9 8 2 5] | [4 7]
|
||||
12 | pop 5 from s2 to output | [] | [1 3 6 9 8 2] | [4 7 5]
|
||||
13 | pop 2 from s2 to output | [] | [1 3 6 9 8] | [4 7 5 2]
|
||||
14 | pop 8 from s2 to output | [] | [1 3 6 9] | [4 7 5 2 8]
|
||||
15 | pop 9 from s2 to output | [] | [1 3 6] | [4 7 5 2 8 9]
|
||||
16 | pop 6 from s2 to output | [] | [1 3] | [4 7 5 2 8 9 6]
|
||||
17 | pop 3 from s2 to output | [] | [1] | [4 7 5 2 8 9 6 3]
|
||||
18 | pop 1 from s2 to output | [] | [] | [4 7 5 2 8 9 6 3 1]
|
||||
@@ -0,0 +1,26 @@
|
||||
Iterative postorder with one stack, step by step
|
||||
================================================
|
||||
|
||||
step | action | stack (bottom..top) | output so far
|
||||
1 | push 1 | [1] | []
|
||||
2 | push 2 | [1 2] | []
|
||||
3 | push 4 | [1 2 4] | []
|
||||
4 | pop 4, visit | [1 2] | [4]
|
||||
5 | peek 2: right child 5 not done, go right | [1 2] | [4]
|
||||
6 | push 5 | [1 2 5] | [4]
|
||||
7 | push 7 | [1 2 5 7] | [4]
|
||||
8 | pop 7, visit | [1 2 5] | [4 7]
|
||||
9 | pop 5, visit | [1 2] | [4 7 5]
|
||||
10 | pop 2, visit | [1] | [4 7 5 2]
|
||||
11 | peek 1: right child 3 not done, go right | [1] | [4 7 5 2]
|
||||
12 | push 3 | [1 3] | [4 7 5 2]
|
||||
13 | peek 3: right child 6 not done, go right | [1 3] | [4 7 5 2]
|
||||
14 | push 6 | [1 3 6] | [4 7 5 2]
|
||||
15 | push 8 | [1 3 6 8] | [4 7 5 2]
|
||||
16 | pop 8, visit | [1 3 6] | [4 7 5 2 8]
|
||||
17 | peek 6: right child 9 not done, go right | [1 3 6] | [4 7 5 2 8]
|
||||
18 | push 9 | [1 3 6 9] | [4 7 5 2 8]
|
||||
19 | pop 9, visit | [1 3 6] | [4 7 5 2 8 9]
|
||||
20 | pop 6, visit | [1 3] | [4 7 5 2 8 9 6]
|
||||
21 | pop 3, visit | [1] | [4 7 5 2 8 9 6 3]
|
||||
22 | pop 1, visit | [] | [4 7 5 2 8 9 6 3 1]
|
||||
@@ -0,0 +1,13 @@
|
||||
Level order with a queue, step by step
|
||||
======================================
|
||||
|
||||
step | action | queue (front..back) | output so far
|
||||
1 | poll 1, visit, offer its children | [2 3] | [1]
|
||||
2 | poll 2, visit, offer its children | [3 4 5] | [1 2]
|
||||
3 | poll 3, visit, offer its children | [4 5 6] | [1 2 3]
|
||||
4 | poll 4, visit, offer its children | [5 6] | [1 2 3 4]
|
||||
5 | poll 5, visit, offer its children | [6 7] | [1 2 3 4 5]
|
||||
6 | poll 6, visit, offer its children | [7 8 9] | [1 2 3 4 5 6]
|
||||
7 | poll 7, visit, offer its children | [8 9] | [1 2 3 4 5 6 7]
|
||||
8 | poll 8, visit, offer its children | [9] | [1 2 3 4 5 6 7 8]
|
||||
9 | poll 9, visit, offer its children | [] | [1 2 3 4 5 6 7 8 9]
|
||||
@@ -0,0 +1,20 @@
|
||||
Morris inorder on the sample tree, step by step
|
||||
===============================================
|
||||
|
||||
step | action | threads (from node) | output so far
|
||||
1 | at 1: thread 5 -> 1, go left | [5] | []
|
||||
2 | at 2: thread 4 -> 2, go left | [5 4] | []
|
||||
3 | at 4: no left child, visit, go right | [5 4] | [4]
|
||||
4 | at 2: thread from 4 found, remove it, visit, go right | [5] | [4 2]
|
||||
5 | at 5: thread 7 -> 5, go left | [5 7] | [4 2]
|
||||
6 | at 7: no left child, visit, go right | [5 7] | [4 2 7]
|
||||
7 | at 5: thread from 7 found, remove it, visit, go right | [5] | [4 2 7 5]
|
||||
8 | at 1: thread from 5 found, remove it, visit, go right | [] | [4 2 7 5 1]
|
||||
9 | at 3: no left child, visit, go right | [] | [4 2 7 5 1 3]
|
||||
10 | at 6: thread 8 -> 6, go left | [8] | [4 2 7 5 1 3]
|
||||
11 | at 8: no left child, visit, go right | [8] | [4 2 7 5 1 3 8]
|
||||
12 | at 6: thread from 8 found, remove it, visit, go right | [] | [4 2 7 5 1 3 8 6]
|
||||
13 | at 9: no left child, visit, go right | [] | [4 2 7 5 1 3 8 6 9]
|
||||
|
||||
tree before: 1(2(4,5(7,-)),3(-,6(8,9)))
|
||||
tree after : 1(2(4,5(7,-)),3(-,6(8,9)))
|
||||
@@ -0,0 +1,14 @@
|
||||
Sample tree: size, height and diameter
|
||||
======================================
|
||||
|
||||
nodes : 9
|
||||
leaves : 4
|
||||
height, counted in nodes : 4
|
||||
height, counted in edges : 3
|
||||
height via level order (BFS) : 4
|
||||
diameter, counted in nodes : 7
|
||||
diameter, counted in edges : 6
|
||||
one longest path : [7, 5, 2, 1, 3, 6, 8]
|
||||
height of the subtree at node : {1=4, 2=3, 3=3, 4=1, 5=2, 6=2, 7=1, 8=1, 9=1}
|
||||
node visits, naive O(n^2) way : 26
|
||||
node visits, one-pass way : 9
|
||||
@@ -0,0 +1,11 @@
|
||||
Diameter: node visits, naive vs one pass (counted, not timed)
|
||||
=============================================================
|
||||
|
||||
tree nodes naive visits one-pass diameter
|
||||
sample (9) 9 26 9 7
|
||||
perfect, 4 levels 15 49 15 7
|
||||
perfect, 7 levels 127 769 127 13
|
||||
perfect, 10 levels 1023 9217 1023 19
|
||||
left-skewed 100 100 5050 100 100
|
||||
left-skewed 1000 1000 500500 1000 1000
|
||||
left-skewed 2000 2000 2001000 2000 2000
|
||||
@@ -0,0 +1,21 @@
|
||||
Constructing a tree from two traversals
|
||||
=======================================
|
||||
|
||||
inorder : [4, 2, 5, 1, 6, 3]
|
||||
preorder : [1, 2, 4, 5, 3, 6]
|
||||
|
||||
from inorder + preorder, one line per recursive call:
|
||||
root 1 inorder slice [0..5] left size 3, right size 2
|
||||
root 2 inorder slice [0..2] left size 1, right size 1
|
||||
root 4 inorder slice [0..0] left size 0, right size 0
|
||||
root 5 inorder slice [2..2] left size 0, right size 0
|
||||
root 3 inorder slice [4..5] left size 1, right size 0
|
||||
root 6 inorder slice [4..4] left size 0, right size 0
|
||||
result : 1(2(4,5),3(6,-))
|
||||
postorder of the result: [4, 5, 2, 6, 3, 1]
|
||||
|
||||
from inorder + postorder the same tree comes back: 1(2(4,5),3(6,-))
|
||||
|
||||
two different trees with the same preorder AND postorder:
|
||||
1(2,-) preorder [1, 2] postorder [2, 1]
|
||||
1(-,2) preorder [1, 2] postorder [2, 1]
|
||||
@@ -0,0 +1,8 @@
|
||||
Inorder of a binary search tree is sorted
|
||||
=========================================
|
||||
|
||||
inserted in this order : [50, 30, 70, 20, 40, 60, 80]
|
||||
tree : 50(30(20,40),70(60,80))
|
||||
preorder : [50, 30, 20, 40, 70, 60, 80]
|
||||
inorder : [20, 30, 40, 50, 60, 70, 80]
|
||||
postorder : [20, 40, 30, 60, 80, 70, 50]
|
||||
@@ -0,0 +1,19 @@
|
||||
JVM: Eclipse Adoptium 25.0.5, ThreadStackSize=1024 KB, cores=2
|
||||
Built a left-skewed tree with 1000000 nodes using a loop (no recursion).
|
||||
|
||||
Depth reached by a recursive probe before StackOverflowError (3 attempts in one JVM):
|
||||
attempt 1: StackOverflowError after reaching depth 21258
|
||||
attempt 2: StackOverflowError after reaching depth 29514
|
||||
attempt 3: StackOverflowError after reaching depth 29514
|
||||
|
||||
RecursiveTraversals.inorder on the same tree:
|
||||
caught java.lang.StackOverflowError
|
||||
stack trace length: 1024 frames (the JVM truncates traces; the real depth is larger)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
...
|
||||
|
||||
IterativeTraversals.inorder on the same tree:
|
||||
visited 1000000 nodes, first=1000000, last=1, no error
|
||||
@@ -0,0 +1,19 @@
|
||||
JVM: Eclipse Adoptium 25.0.5, ThreadStackSize=512 KB, cores=2
|
||||
Built a left-skewed tree with 1000000 nodes using a loop (no recursion).
|
||||
|
||||
Depth reached by a recursive probe before StackOverflowError (3 attempts in one JVM):
|
||||
attempt 1: StackOverflowError after reaching depth 5175
|
||||
attempt 2: StackOverflowError after reaching depth 12829
|
||||
attempt 3: StackOverflowError after reaching depth 13130
|
||||
|
||||
RecursiveTraversals.inorder on the same tree:
|
||||
caught java.lang.StackOverflowError
|
||||
stack trace length: 1024 frames (the JVM truncates traces; the real depth is larger)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
...
|
||||
|
||||
IterativeTraversals.inorder on the same tree:
|
||||
visited 1000000 nodes, first=1000000, last=1, no error
|
||||
@@ -0,0 +1,19 @@
|
||||
JVM: Eclipse Adoptium 25.0.5, ThreadStackSize=4096 KB, cores=2
|
||||
Built a left-skewed tree with 1000000 nodes using a loop (no recursion).
|
||||
|
||||
Depth reached by a recursive probe before StackOverflowError (3 attempts in one JVM):
|
||||
attempt 1: StackOverflowError after reaching depth 51051
|
||||
attempt 2: StackOverflowError after reaching depth 127818
|
||||
attempt 3: StackOverflowError after reaching depth 127818
|
||||
|
||||
RecursiveTraversals.inorder on the same tree:
|
||||
caught java.lang.StackOverflowError
|
||||
stack trace length: 1024 frames (the JVM truncates traces; the real depth is larger)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
at com.ankurm.interviewlab.bintree.RecursiveTraversals.inorder(RecursiveTraversals.java:20)
|
||||
...
|
||||
|
||||
IterativeTraversals.inorder on the same tree:
|
||||
visited 1000000 nodes, first=1000000, last=1, no error
|
||||
Reference in New Issue
Block a user