Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree with parent pointers VS zippers #11

Closed
stefnotch opened this issue Oct 31, 2022 · 8 comments
Closed

Tree with parent pointers VS zippers #11

stefnotch opened this issue Oct 31, 2022 · 8 comments
Labels

Comments

@stefnotch
Copy link
Owner

stefnotch commented Oct 31, 2022

I think zippers are neater, see also https://blog.yaakov.online/red-green-trees/

@stefnotch
Copy link
Owner Author

82c2345 still has the Tree with parent pointers design

@stefnotch
Copy link
Owner Author

Zippers are nice so far

@stefnotch
Copy link
Owner Author

See also #20 and readme

@stefnotch
Copy link
Owner Author

Everyone uses zippers or red-green trees

@stefnotch
Copy link
Owner Author

stefnotch commented Nov 25, 2022

Neato, we could have constant time comparisons for zipper equality (given non-pathological trees)
Though a simple very nested fraction gives us an exponential growth of the number so that's not so cool in the worst case. We can survive like 32 nested fractions.

// get a unique value for a given zipper
current = this
zipperValue = this.indexInParent + 1; // 0 equals end, so we pretend there's an unreachable child at the far left
while(this.parent != null) {
  current = this.parent
  zipperValue = zipperValue * (current.length+1) + current.indexInParent
}
return zipperValue; // wait wtf is that a unique encoding?


// turn the value back into a zipper
fromZipperValue(zipperValue, root) {
  current = root
  while(zipperValue > 0) {
    index = (zipperValue mod (current.length+1)) - 1;
    zipperValue = floor(zipperValue / (current.length+1))
    current = current.children[index]
  }
}

There's a smarter way of doing it, see #30

@stefnotch stefnotch reopened this Jan 30, 2023
@stefnotch
Copy link
Owner Author

Now I'm thinking about redesigning this in Rust.
API comes first, and the internal structure should be an implementation detail. I want the zippers to be the public API.

@stefnotch
Copy link
Owner Author

@stefnotch
Copy link
Owner Author

stefnotch commented Feb 8, 2023

Having parent references (including the zipper or red-green node types) is much easier when you have a homogeneous tree. Then you don't have to deal with cases like "this table cell node can only have a table as its parent". (See also #21 (comment) )

As such, not giving a table some funny "table-row" and "table-cell-elements" makes it easier to deal with.
Similarly, rows can have any child they want. So the Row<T> design gets dropped.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant