Found this tutorial code. i cant understand how function inorder() is traversing through binary tree. (i am a beginner, i understand how recursive functions work , but this code has got me confused) L68.
// C++ program to insert element in Binary Tree
#include <iostream>
#include <queue>
#include<conio.h>
usingnamespace std;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct Node {
int data;
Node* left;
Node* right;
};
/* Function to create a new node */
Node* CreateNode(int data)
{
Node* newNode = new Node();
if (!newNode) {
cout << "Memory error\n";
return NULL;
}
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
/* Function to insert element in binary tree */
Node* InsertNode(Node* root, int data)
{
// If the tree is empty, assign new node address to root
if (root == NULL) {
root = CreateNode(data);
return root;
}
// Else, do level order traversal until we find an empty
// place, i.e. either left child or right child of some
// node is pointing to NULL.
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* temp = q.front();
q.pop();
if (temp->left != NULL)
q.push(temp->left);
else {
temp->left = CreateNode(data);
return root;
}
if (temp->right != NULL)
q.push(temp->right);
else {
temp->right = CreateNode(data);
return root;
}
}
}
/* Inorder traversal of a binary tree */
void inorder(Node* temp)
{
if (temp == NULL)
return;
inorder(temp->left);
cout << temp->data << ' ';
inorder(temp->right);
}
// Driver code
int main()
{
Node* root = CreateNode(10);
root->left = CreateNode(11);
root->left->left = CreateNode(7);
root->right = CreateNode(9);
root->right->left = CreateNode(15);
root->right->right = CreateNode(8);
cout << "Inorder traversal before insertion: ";
inorder(root);
cout << endl;
int key = 12;
root = InsertNode(root, key);
cout << "Inorder traversal after insertion: ";
inorder(root);
cout << endl;
return 0;
}
Well, if you are at any node that currently has some assigned data (i.e. isn't NULL) then you would output, in order:
everything smaller than the node's value ...
... then the node's value ...
... then everything larger than the node's value
Grim diagram follows:
|
|
NODE
/ | \
/ value \
/ \
left right
/ \
-------------- ----------------
| Everything | | Everything |
| less than | | greater than |
| value | | value |
-------------- ----------------
i cant understand how function inorder() is traversing through binary tree
The best advice I can provide (and for any linked data structure) is to draw a correctly populated tree on paper with each node showing the left, right and data. Then to follow the code manually - recording variable values etc on paper as the computer would execute the code. Then you can see how it works (or any other linked structure). The same for inserting/removing nodes et al. When I was studying AVL trees for my degree years ago, I found this invaluable to my understanding.
I mentally convert the recursion to english, or of very confusing stuff, will actually translate it on paper (usually the confusing ones are just someone being snarky and not production code. Weird recursion to show off is a thing online, but rarely done in practice).
so, if you go left as far as you can and then print, what do you get? (7)
then go right one time.
if you go left as far as you can and then print, what do you get? (8)
go right one time (null, return to last unprinted value, 9)
print the 9, go right one time (null, return to last unprinted value, 10)
print 10, go right one time
go left as far as you can... (can't)
print 11 go right one time...
go left as far as you can...
etc
The way to read a recursive function is to NOT FOLLOW THE RECURSIVE CALL. Just assume that the recursive call does what the function is supposed to do. inorder is supposed to print the element values in order. So, reading the code, it says "first print the left side in order, then print the current value, then print the right side in order". That is clearly correct.
I remember this technique being called Method99, the idea being that you imagine a function is called with value/size 100, and the "recursive" calls are all handled with calls to Method99, which properly handles values/sizes of 99 or less. You need to create your function to be able to handle the "size 100" case, using the prewritten Method99 to handle the smaller cases.
So merge sort is something like:
1 2 3 4 5 6 7 8 9 10 11 12 13
MergeSort:
if size is less than some small size:
// sort with insertion sort (or some other simple sort)
...
otherwise:
// sort first half
Method99(first half of data)
// sort second half
Method99(second half of data)
// merge the halves together
...
oid inorder(Node* temp)
{
if (temp == NULL)
return;
cout<<"\nEntering: "<<temp->data;
inorder(temp->left);
cout<<"\nPrint: "<<temp->data;
inorder(temp->right);
cout<<"\nLeaving: "<<temp->data;
}
@seeplus could you show me any other way for function inorder() other than recursion!!
@lastchance thankx for the help , i am still going through your code.
both createNode function are same , however in the later code we havent assigned nullptr to the node pointers . How is it so?
and also if we remove this newNode->left = newNode->right = nullptr; it doesnt make any difference.
however removing this line makes defference if we declare new node likeNode* newNode=new Node instead of Node* newNode=new Node();
so adding fuction call operator at the end is the reason but how?