We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- Data Structures
- Trees
- Tree : Top View
- Discussions
Tree : Top View
Tree : Top View
Sort by
recency
|
952 Discussions
|
Please Login in order to post a comment
This question is so terribly defined it makes me wanna p*** in the cereal of whoever made this question. What defines a view of a tree as being top down? Well in the problem description its defined in one single example EXCEPT THE F****** EXAMPLE DOESN'T COVER 80% OF HOW THE VIEW IS DEFINED.
Assuming all branches are evenly spaced: i.e. a left branch is 1 unit down and 1 unit to the left of its parent, the highest branch to be left or right of the root and all of its proceeding children will be what you could see. For example, if the root node has a left child, the left child and all of its left children are what you can see. The highest/first child to be right of the root and all of its right sided children would be visible as well.
If you had: 1 \ 15 / 3 / \ 2 4 Then you could see 15 and all of its proceeding right children, you could also see two since its left of 1 and all of its left children. Since all diagnal lines you could form all have a slope of 1 or -1, you could never have a situation where you could see one of four's imaginarily infine right sided children because 15 set the line of sight at a higher point. Same logic follows that four's imaginarily infine left sided children could never enter the line of sight of root because 2 already determined roots left sided line of sight.
I made one other assumption that branches / connections between branches were opaque. I wrote code based on these priciples. F*** this problem.
`def topView(root, direction="Root"):
""" Node is defined as self.left (the left child of the node) self.right (the right child of the node) self.info (the value of the node) """ def tree_Coords(root, d, radius, level): if not root: return if level not in d.keys(): d[level] = dict() d[level].update({radius: root.info}) tree_Coords(root.left, d, radius-1, level+1) tree_Coords(root.right, d, radius+1, level+1)
def topView(root): radius = 0 level = 0 if not root: return d = dict() tree_Coords(root, d, radius, level) res = dict()
Title: Why is my top view function returning incorrect results for a binary search tree?
Hi everyone,
I’m trying to implement the top view of a binary search tree in Java, but my output isn’t matching my expectations. After debugging my code, I suspect I may not fully understand the concept of the top view, and I’m getting an incorrect result.
Problem:
I’ve inserted nodes into a binary search tree in the following order:
My Approach:
I’m using the following code to calculate the top view:
Issue:
When I run the program with the above input, I get the following output:
However, the expected output should be:
Thank you in advance for your help!
This version should make your issue and expectations clear to others, inviting them to help you debug the code further.
Hello everyone!
I've struggles with solving this problem.
1) I can't understand the input provided, I assume it's a node with data, left and right properties.
2) And this is my javascript solution which prints nothing: