I'm creating a React app that has a "SplitPane" component that is basically a div split horizontally or vertcally (The divider in the middle can be dragged for resizing):
____ _________ _________
| | | | | | | |
|____| |____|____| |__|______|
| |
|____|
These can be nested recursively, forming structure like the following:
_____________
| |___| |
| |___|___|
|_____|_____|_|
| | |
|_______|_____|
|_______|_____|
Or any other such arbitrary combination.
I would like the user to be able to reasonably switch between the cells with the arrow keys, but being that this is a component tree, it will look something like this in memory:
H
/ \
H V
/ \ / \
* * H *
/ \
* H
/ \
* *
Where:
- 'H' := horizontally split panes
- 'V' := vertically split panes
- '*' := leaf component
I would like to know if there is any sort of algorithm that can be used to easily traverse this tree to get the top, bottom, left, or right component which could be several branches away on the component tree.
Let's use three data structures:
parent,first_childandleveldata structures.The
parentdata structure, keeps track of theparentof theithnode.The
first_childdata structure, keeps track of the first child of theithnode.The
leveldata structure, keeps track of the nodes in theithlevel.Consider the following tree :
level[0] = {a}, level[1] = {b, c}parent[b] = parent[c] = afirst_child[a] = bWhen the up arrow is pressed, we go to the parent of the current node, when down arrow is pressed we go to the child, and when left and right arrow is pressed, we go to the left and right node in the particular level.
Note: Nodes in
level[i]can be stored in a sorted order and use binary search to find the neighbors(left and right) efficiently. If we assign thekeyvalue of the react component with an increasing value each time, it'll be sorted by default.