I was trying to learn Binary search tree,I have one doubt related to BST insertion.This is not my code I have taken this from http://cslibrary.stanford.edu/110/BinaryTrees.html
struct node* insert(struct node* node, int data) {
// 1. If the tree is empty, return a new, single node
if (node == NULL) {
return(newNode(data));
}
else {
// 2. Otherwise, recur down the tree
if (data <= node->data) node->left = insert(node->left, data);
else node->right = insert(node->right, data);
return(node); // return the (unchanged) node pointer-->THIS LINE
}
}
My doubt As mentioned in the code I don't understand that why root doesn't get changed on insertion(last line).Why it is the same root everytime ?
recursive call in this code doesn't affect root node because you send root node
at first time ( at that time root is NULL) and will enter the if condition
otherwise will not affect root consider the following tree and call
2 -- (call insert and gave it root node, data -4)
/ \
1 10
/
5
first call will check if root == NULL
---this if false
will test whatever -4 greater or smaller than 2 and will make recursive call on left node
2
/ \
1-- 10 (call insert and gave it left child of root node, data -4)
/
5
and this node again not NULL will make anther recursive call of left of left of root this node is NULL
2
/ \
1 10
/ /
NULL 5 (call insert and gave it left child of left child root node, data -4)
here will create new node and with returning will assign this node to left of left of root and return pointer on it to first call
2
/ \
1 10
/ /
-4 5
just ...
my advice read about recursive functions good before studying BST
If you have a BST and want to insert the stream 3, 2, 8, 5, 7 you will do as follows:
3
3
/
2
3
/ \
2 8
3
/ \
2 8
/
5
3
/ \
2 8
/
5
\
7
As you can see the root never changes. Each element you insert gets added as a leaf in the correct position.
Related
Here I have a dp-on-tree problem needs helped. So, the tree has N node and N - 1 edges, and a leaf-to-leaf path is a path from a leaf-node to another leaf-node on tree. Therefore, if the tree has M leaf, the total path will be M * (M + 1) / 2.
How can I find the node which has maximum leaf-to-leaf path go through it? For example, if I have the tree like this, the answer will be node 2. All the paths go through node 2 are: {1 -> 6, 1 -> 7, 6 -> 7, 1 -> 4, 4 -> 6, 4 -> 7, 1 -> 5, 5 -> 6, 5 -> 7}.
I think it's a dp-on-tree problem, but I can't find out the function and dp-formula.
Thks you so much! Also, pls show me some line of code if available.
Example Here
Given a suitable struct node { int id; node *left, *right;} and pre-created tree:
std::tuple<node*, int> max_candidate{nullptr, -1};
void report(node *root, int paths) {
if (paths > std::get<1>(max_candidate)) {
max_candidate = {root, paths};
}
}
int visit(node *root) {
if (!root)
return 0;
if (!root->left && !root->right)
return 1;
int leaves_left = visit(root->left);
int leaves_right = visit(root->right);
report(root, leaves_left * leaves_right);
return leaves_left + leaves_right;
}
I need to construct a tree given its depth and postorder traversal, and then I need to generate the corresponding preorder traversal. Example:
Depth: 2 1 3 3 3 2 2 1 1 0
Postorder: 5 2 8 9 10 6 7 3 4 1
Preorder(output): 1 2 5 3 6 8 9 10 7 4
I've defined two arrays that contain the postorder sequence and depth. After that, I couldn't come up with an algorithm to solve it.
Here's my code:
int postorder[1000];
int depth[1000];
string postorder_nums;
getline(cin, postorder_nums);
istringstream token1(postorder_nums);
string tokenString1;
int idx1 = 0;
while (token1 >> tokenString1) {
postorder[idx1] = stoi(tokenString1);
idx1++;
}
string depth_nums;
getline(cin, depth_nums);
istringstream token2(depth_nums);
string tokenString2;
int idx2 = 0;
while (token2 >> tokenString2) {
depth[idx2] = stoi(tokenString2);
idx2++;
}
Tree tree(1);
You can do this actually without constructing a tree.
First note that if you reverse the postorder sequence, you get a kind of preorder sequence, but with the children visited in opposite order. So we'll use this fact and iterate over the given arrays from back to front, and we will also store values in the output from back to front. This way at least the order of siblings will come out right.
The first value we get from the input will thus always be the root value. Obviously we cannot store this value at the end of the output array, as it really should come first. But we will put this value on a stack until all other values have been processed. The same will happen for any value that is followed by a "deeper" value (again: we are processing the input in reversed order). But as soon as we find a value that is not deeper, we flush a part of the stack into the output array (also filling it up from back to front).
When all values have been processed, we just need to flush the remaining values from the stack into the output array.
Now, we can optimise our space usage here: as we fill the output array from the back, we have free space at its front to use as the stack space for this algorithm. This has as nice consequence that when we arrive at the end we don't need to flush the stack anymore, because it is already there in the output, with every value where it should be.
Here is the code for this algorithm where I did not include the input collection, which apparently you already have working:
// Input example
int depth[] = {2, 1, 3, 3, 3, 2, 2, 1, 1, 0};
int postorder[] = {5, 2, 8, 9, 10, 6, 7, 3, 4, 1};
// Number of values in the input
int n = sizeof(depth)/sizeof(int);
int preorder[n]; // This will contain the ouput
int j = n; // index where last value was stored in preorder
int stackSize = 0; // how many entries are used as stack in preorder
for (int i = n - 1; i >= 0; i--) {
while (depth[i] < stackSize) {
preorder[--j] = preorder[--stackSize]; // flush it
}
preorder[stackSize++] = postorder[i]; // stack it
}
// Output the result:
for (int i = 0; i < n; i++) {
std::cout << preorder[i] << " ";
}
std::cout << "\n";
This algorithm has an auxiliary space complexity of O(1) -- so not counting the memory needed for the input and the output -- and has a time complexity of O(n).
I won't give you the code, but some hints how to solve the problem.
First, for postorder graph processing you first visit the children, then print (process) the value of the node. So, the tree or subtree parent is the last thing that is processed in its (sub)tree. I replace 10 with 0 for better indentation:
2 1 3 3 3 2 2 1 1 0
--------------------
5 2 8 9 0 6 7 3 4 1
As explained above, node of depth 0, or the root, is the last one. Let's lower all other nodes 1 level down:
2 1 3 3 3 2 2 1 1 0
-------------------
1
5 2 8 9 0 6 7 3 4
Now identify all nodes of depth 1, and lower all that is not of depth 0 or 1:
2 1 3 3 3 2 2 1 1 0
-------------------
1
2 3 4
5 8 9 0 6 7
As you can see, (5,2) is in a subtree, (8,9,10,6,7,3) in another subtree, (4) is a single-node subtree. In other words, all that is to the left of 2 is its subtree, all to the right of 2 and to the left of 3 is in the subtree of 3, all between 3 and 4 is in the subtree of 4 (here: empty).
Now lets deal with depth 3 in a similar way:
2 1 3 3 3 2 2 1 1 0
-------------------
1
2 3 4
5 6 7
8 9 0
2 is the parent for 2;
6 is the parent for 8, 8, 10;
3 is ahe parent for 6,7;
or very explicitly:
2 1 3 3 3 2 2 1 1 0
-------------------
1
/ / /
2 3 4
/ / /
5 6 7
/ / /
8 9 0
This is how you can construct a tree from the data you have.
EDIT
Clearly, this problem can be solved easily by recursion. In each step you find the lowest depth, print the node, and call the same function recursively for each of its subtrees as its argument, where the subtree is defined by looking for current_depth + 1. If the depth is passed as another argument, it can save the necessity of computing the lowest depth.
I am doing a simple binary search tree implementation in C++. I found that it works for most test cases but I am confused with a test case where I create a tree and add 1,2,3,4,5,6,7,8,9,10 in that order. The inorder traversal comes out to be 1,10,2,3,4,5,6,7,8,9. My understanding is that the inorder traversal will print the elements in sorted order which would be 1,2,3,4,5,6,7,8,9,10. However, either this assumption is incorrect or my code is printing an incorrect output. Please let me know if my output is correct or incorrect, and why if it is correct. Thank you.
If you are not sure about how in order binary tree traversal works then look at the below tree and the explanation.
(NOTE: I have this done only for 5 number's).
/* Constructed binary tree is
1
/ \
2 3
/ \
4 5
*/
Step 1 Creates an empty stack: S = NULL
Step 2 sets current as address of root: current -> 1
Step 3 Pushes the current node and set current = current->left until current is NULL
current -> 1
push 1: Stack S -> 1
current -> 2
push 2: Stack S -> 2, 1
current -> 4
push 4: Stack S -> 4, 2, 1
current = NULL
Step 4 pops from S
a) Pop 4: Stack S -> 2, 1
b) print "4"
c) current = NULL /*right of 4 */ and go to step 3
Since current is NULL step 3 doesn't do anything.
Step 4 pops again.
a) Pop 2: Stack S -> 1
b) print "2"
c) current -> 5/*right of 2 */ and go to step 3
Step 3 pushes 5 to stack and makes current NULL
Stack S -> 5, 1
current = NULL
Step 4 pops from S
a) Pop 5: Stack S -> 1
b) print "5"
c) current = NULL /*right of 5 */ and go to step 3
Since current is NULL step 3 doesn't do anything
Step 4 pops again.
a) Pop 1: Stack S -> NULL
b) print "1"
c) current -> 3 /*right of 5 */
Step 3 pushes 3 to stack and makes current NULL
Stack S -> 3
current = NULL
Step 4 pops from S
a) Pop 3: Stack S -> NULL
b) print "3"
c) current = NULL /*right of 3 */
Traversal is done now as stack S is empty and current is NULL.
Sorry for my bad english.
I need to print the levels of a general tree or n-ary tree.
The struct of the tree is:
struct GTnode{
int data;
nodeGT *fc; //first child
nodeGT *nb; //next brother
}
The difficulty of the algorithm is when you have 2 different node in the same level and each one have a child.
Editing
If I have this tree for example:
1
2 7 8
3 6 9 12
4 5 10 11
I have to print:
1
2 7 8
3 6 9 12
4 5 10 11
Each endl represents a different level in the tree
Editing
An idea of my code is the next:
void printLevel(GTnode *root){
GTnode *aux = root;
if(root != NULL){
cout<<aux->data;
printLevel(aux->nb);
cout<<end; //Print the space between levels
printLevel(aux->fc);
}
}
I know this is wrong but is just an idea of what I have.
You need to do a level-order/breadth-first traversal of the tree (wp). To do that you need a queue. Put the root in the queue. Then do this until the queue is empty: Take the first out, add its ->fc to the queue and go to its ->nb (go through all the ->nb's until there are nomore and each time you add its ->fc to the queue).
I am constructing a binary search tree of size 7 and height 3. I only have to hard-code it, not generate it through a function.
This is the tree that I have hard-coded
Node (Node (Node (Empty, 0, Empty), 1,
Node (Empty, 3, Empty)), 5
Node (Empty, 7, Node (8, 9, Empty))))
What I want is for Node 9 to have two children (8 and Empty). However, I keep on getting an error on 8 that says "this expression has type int but an expression of was expected of type int tree". How can I correct this?
Thanks!
You cannot write 8 for the leaf. You have to write Node (Empty, 8, Empty).
type tree = Empty | Node of tree * int * tree
(* the tree
5
/ \
/ \
1 7
/ \ \
0 3 9
/
8
*)
let t =
Node (
Node (Node (Empty, 0, Empty),
1,
Node (Empty, 3, Empty)),
5,
Node (
Empty,
7,
Node (Node (Empty, 8, Empty),
9,
Empty)
)
)
(* With an auxliary function we can do this to get the same tree: *)
let leaf k = Node (Empty, k, Empty)
let t' =
Node (
Node (leaf 0, 1, leaf 3),
5,
Node (Empty, 7, Node (leaf 8, 9, Empty)))
The first element of a Node has to be a tree, not something else like an int.
Therefore, you can't put 8 in a place where a tree is expected. Probably, you meant to have Node (Empty, 8 Empty) instead of 8.