Tree data structure [closed] - data-structures

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Needs details or clarity Add details and clarify the problem by editing this post.
Improve this question
A b tree is a generalized binary tree . How ?

A binary tree is a tree in which each node has at most 2 children. A b-tree of order m is a tree in which
Each node has at most m children.
Each internal node (not the root or a leaf) has at least ⌈m/2⌉ children. (⌈x⌉ means the ceiling of x, the least integer not less than x.)
Each non-leaf node (parent and all internal nodes) has at least 2 children.
All leaves appear on the same level.
(B tree nodes also have keys, but this is not directly part of the tree structure and does not concern us in this question.)
So some b-trees are binary trees. Every b-tree of order 2 is a binary tree. Some b-trees of higher order are binary trees if they happen not to have any nodes with more than 2 children.
b-trees of orders 5 and greater could be binary trees only if they are just a parent and two children, which are leaves. If a tree of order 5 or greater had any internal nodes, that node would be required to have at least ⌈5/2⌉ = 3 children, so it could not be a binary tree. b-trees of orders 3 and 4 could have internal nodes and still be binary trees.
The concepts of binary tree and b-tree overlap, but neither is a subset of the other in the sense that all requirements of one would satisfy the other. For the most part in programming, you are not going to mix uses of routines for binary trees and other routines for b-trees based on just how the current tree happens to be filled and arranged; on a particular set of data being managed, you would be working entirely with binary tree routines or entirely with b-tree routines.

Related

Binary Tree Puzzle [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Recently I came across this interview puzzle.
Can Someone Explain me what this puzzle means..
You are given a binary tree. A light source is placed on the right of the tree. Print the list of all the nodes over which the light is falling directly.
Which Nodes will be shielded ? It's obvious the right nodes will get printed but if the left subtree is greater, then we have to include a couple of extra test cases , right?
Correct me , If I'm wrong!
You are correct. The rightmost subtree should be printed, and possibly some nodes of the tree that in the left subtree. In fact the rightmost node of every level of the tree should be printed. In the following example the nodes marked with an x should be the output
x
/ \
o x
/ \
o x
You could realize this with recursive backtracking, by traversing the tree and always taking the right path and print out the nodes you traversed.
If you reach a node withoud any children you go back to the parent node and continue on its left path until you reach a deeper level than before (here you start printing them out again) or until you reached a node wihtout children.
If you repreat this you will eventually get all the nodes that are touched by the light

Will an AVL tree ever need multiple rebalances? [duplicate]

This question already has answers here:
More than one rotation needed to balance an AVL Tree?
(2 answers)
Closed 8 years ago.
Assume I have a balanced AVL tree and after an ADD, it becomes unbalanced. Will an AVL tree always be rebalanced by a one single or one double rotation, or is there a case that would require more rotations?
A single rebalancing will always be sufficient to restore the balance invariant, assuming the balance invariant was satisfied prior to the insertion.
The AVL invariant is that any node has child depths which differ by at most 1. After a single insertion, the child depths can differ by at most 2. A single traversal down the path to the inserted node, rotating as necessary, is capable of resolving the imbalance.

Algorithms with identical code that can achieve different useful goals when applied to different containers [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Breadth-first search and depth-first search are two algorithms that are the same, except for what they do, and the data structure they use.
Breadth-first search:
q := queue
q.append(root node of tree)
while q is not empty:
n := q.pop()
if n is the node being searched for:
return n
if n has children:
c := children of node
for i in c:
q.push(i)
Depth-first search:
s := stack
s.append(root node of tree)
while s is not empty:
n := s.pop()
if n is the node being searched for:
return n
if n has children:
c := children of node
for i in c:
s.push(i)
Are there any other algorithms (or data structures) that work like this?
One that comes to mind is Dijkstra's graph search algorithm and A*. They're essentially the same except for how the next iteration of the loop is chosen - Dijkstra's is breadth first - allowing you to find the closest target and A* is heuristic depth first allowing you to find one target with the least iterations (shortest amount of wall-clock time).
In fact, I've seen both implemented with exactly the same code allowing you to run either A* or Dijkstra (or indeed D* or other algorithms) by supplying the appropriate callback for selecting the next node in the list.
There is a continuous family of algorithms, Prim-Dijkstra, that depend on a parameter in the interval [0,1]. When parameter is 0 you get Prim algorithm; when parameter is 1 you get Dijkstra algorithm.
Prim-Dijkstra is useful what you are computing a sub-tree of a graph with 2 conflicting optimization goals: minimization of maximal distance from "source" vertex to multiple "drain" vertices and minimizing the total length of the tree. In practical applications you often want to bound both quantities, so you perform Prim-Dijkstra with an intermediate parameter.

building binary tree out of a given traversal [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I was solving my homework for the Data Structures and Algorithms course that I came across this question :
"Given two methods of traversing which can be pre-order and post-order , pre-order and in-order , post-order and in-order , how many binary trees can we extract ?"
Now I know that you certainly cannot find the binary tree from only one traversal order , but which of those two traversals would give you only one binary tree ? and how ? and the ones that do not exemplify one binary tree , how many binary trees do they exemplify and how can we count that number ?
I believe this source is useful. A pre and a post order traversal of a tree is not enough to uniquely reconstruct it without further restrictions. However an algorithm is shown how to reconstruct a tree from its post order and inorder travesal and as the last case is somewhat symmetric I believe this is algorithmic prove that a tree can be reconstructed uniquely from its inorder and any of the other traversals.

Can we have a red-black tree without any red nodes? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I wonder whether a red black tree should have at least one red node. Also, given a BST, if we can convert it into an RBT, is there a unique way to turn this tree into a red-black tree?
A quick glance at the properties of a red-black tree shows that there is no requirement for any node to be red. The only way red nodes come about is through property 5:
Every simple path from a given node to any of its descendant leaves contains the same number of black nodes.
This property is also satisfied by any perfect binary tree, so every perfect binary search tree with only black nodes is also a red-black tree. (I'm not sure if the textbook red-black tree algorithms ever produce these, though.)
Also, given a BST, if we can convert it into an RBT, is there a unique way to turn this tree into a red-black tree?
There is no single unique RBT for an arbitrary BST; there are always multiple equivalent RBTs, except for very shallow trees.

Resources