Saturday, 15 August 2015

c - Using Pointer to Pointer of Struct while adding nodes in the binary tree -


I want to know why we use, indicating indicator to the pointer when inserting nodes in the binary tree. But, while crossing the binary tree, we can see the tree in the root node by simply simple indicator. But why insert node?

Can anybody help me understand the reason or reference link, why it is the indicator for the indicator.

  / * This program cleans up all the three methods of * # TRUSTALLL & lt; Stdio.h & gt; #to & lt include, stdlib.h & gt; / * We basically describe how a particular node looks in a binary tree .... Every node in the tree has three main elements, left children, right kids, and data include I * / Struct treenode {int data ; Straight Turn Node * Left child; Straight Turn Node * Right Shirts; }; Zero Inner (Straight TreeNode * BT); Zero Preder (Straight TreeNode * BT); Zero Post Order (Straight TreeNode * BT); Enter Entry (Straight TreeNode ** BT, Int number); Main () {int num, element; Trynnode * bt; Int i; Printf ("Enter the number of elements inserted in the tree"); Scanf ("% d", and number); Printf ("Enter the elements inserted inside the tree"); (I = 0; i & lt; num; i ++) for {scanf ("% d", and element); Insert (& amp; bt, element); Printf ("\ n"); } Printf ("Order Trader \ n"); Inorder (BT); Printf ("Pre-Order Traversal \ n"); Preorder (BT); Printf ("Post Order Traveler \ n"); Posthumous (BT); Return 0; } Int insert (struct treeNode ** bt, int num) {if (* bt == null) {* bt = malloc (sizeof (struct treeNode)); (* BT) - & gt; LeftChild = Zero; (* BT) - & gt; Data = number; (* BT) - & gt; RightChild = zero; Return; } Other {/ * * / if (num & lt; (* bt) - & gt; Data) {insert (& ((* bt) -> leftChild), num); } Else {insert (& ((* bt) -> Right Chald), num); }} Return; } Zero Inner (Straight TreeNode * BT) {if (BT! = Null) {// Left Node Inner (BT-> Left Child) Process; Print data of / * parent node * / // printf ("% d", bt-> data); / * Correct node procedure * / inner (bt-> right-handed); }} Zero preorder (struct TreeNode * bt) {if (BT) {// parent node first process on printf ("% d", BT-> data); // Left node process pre-order (BT> leftChild); // Pre-node Process Pre-Order (BT> RightChild); }} Zero Post Order (Straight TreeNode * BT) {if (BT) {// process left child post order (BT-> left child); // Process the right child post order (BT-> right child); // parent node printf process ("% d", bt-> data); }}    

"I want to know why we use it While pointing to the pointer when inserting the nodes in the binary tree, however, while crossing the binary tree, we just add the ordinary point to the root node, but why inserting the node? "

We do not really need the code to answer if you modify in an external function in C Not want) data type (if you must know the data. Just like:

  main () {int x = 2; Change_me (x); Printf ("% d \ n", x); // print2} zero change_me (int x) {x ++; }   

has no meaning You are receiving a local copy of (in this example) of the occultation, any changes made in the price are within the local area if you want you need an address to transmit those changes back to the calling function :

  main () {int x = 2; Change_me (& amp; x); Printf ("% d \ n", x); // print 3} zero change_me (int * x) {(* x) ++; }   

This applies to the indicator. In the example of a linked list, if I want to honor print , then I need to cross the tree and read the data. is. I do not have to change anything, that's just the indicator. However, if I want to modify the tree:

  the straight node {int val; Straight node * next; }; Main () {struct node * head = malloc (sizeof (struct node)); Head-gt; Val = 3; Insert_a_node_in_front (head); } Insert_a_node_in_front (node ​​* ptr) {struct node * temp = ptr; Ptr = Molec (size (structure node)); PTR- & gt; Val = 5; PTR- & gt; Next = temporary; }   

OK, what do you think? We did not actually include that node because the value of head has never changed. It still indicates the original node with val == 3 . The reason is the same as before, we have tried to change the value of the local copy of the parameter. If we want to prevent the changes, it needs the address of the original copy:

  insert_a_node_in_front (& amp; head); } Insert_a_node_in_front (node ​​** ptr) {struct node * temp = (* ptr); (* Ptr) = Molec (size (structure node)); (* Ptr) - & gt; Val = 5; (* Ptr) - & gt; Next = temporary; }    

No comments:

Post a Comment