Binary tree can be used to save the pointer to the storage array can also be used, when using an array should be complete binary tree.
Stored in the array in a manner (that is a root node), the current node is x, the left child the child is 2 * x, the n-th layer 2 ^ n-1 nodes. Is determined to be empty, the subscript is greater than the total number of nodes, it is determined whether or subtree with 2 * x n and judgment.
In pointer storage, remember to initialize the root node * root = NULL, about all of the new sub-tree nodes are NULL
root = NULL, root-> lchild = NULL, root-> rchild = NULL, they do not represent the point.
Defined when the pointer in the content it points to when the normal edit the line (always points A, A which element is changed), if the modify its content (that is, modify the object pointer, by the point A becomes point B) to use references
Inside the change (node * & root), to this format, do not forget or pointer, to have a *, when calling directly change (root) on it.
#include<stdio.h> struct node{ int data; node*lchild; node*rchild; }; Node * the root = NULL; // do not exist prior contribution, initially empty Node * the newNode ( int V) { Node * Node = New Node; Node->data=v; Node->lchild=NULL; Node->rchild=NULL; return Node; } // create a new node void Search (the root Node *, int X, int NewData) { if(root==NULL) return ; if(root->data==x) root->data=newdata; search(root->lchild,x,newdata); search(root->rchild,x,newdata); } void insert(node*&root,int x) { if(root==NULL) { root=newNode(x); return; } IF (directory root-> Data == . 1 ) // what conditions inserted in the left subtree INSERT (directory root-> lchild, X); the else INSERT (the root -> rchild, X); } node*Create(int data[],int n) { node*root=NULL; for(int i=0;i<n;i++) insert(root,data[i]); return root; }
Several common pointer operation code above.