1. Find tree Creation (Createtree)
Suppose you have the following array 4,1,45,78,345,23,12,3,6,21
First select 4 as root, and then traverse the remaining digits, if greater than or equal to 4 is placed on the right side of 4, less than 4 put to the left of 4, and finally built into the tree: All left children are smaller than the parent node, all right children are greater than or equal to the parent node. The following figure:
2. Traverse the Find tree (DisplayTree)
The tree is traversed in the left and right order, and the result is: 1,3,4,5,12,21,23,45,78,345, the result of the traversal is a numbered number that is already sorted.
3. Finding nodes in the tree (Searchtree)
Starts at the root node, finds the right side of the root node if it is greater than or equal to the root node, and if the root node is small, finds the left side of the roots until the node is found.
For example, to find 12:
Bigger than 4, go right;
Smaller than 45, go left;
Smaller than 23, go left;
Found 12
4. Deleting nodes in the tree (Deletenode)
This is the most complex, because the tree is rebuilt after the node is deleted, which involves a lot of things:
A. The node to be deleted has no left or right child, there are parent nodes.
If the left child of the parent node is to be deleted, the left child pointer of the parent node is set to NULL, and the right child pointer of the parent node is set to null if the node to be deleted is the right child of the parent node. Finally, delete node.
B. The node to be deleted has no left or right child, no parent node (that is, root node).
The root node is set to NULL to delete node.
C. Node to delete has left child no right child, parent node
If the left child of the parent node is to be deleted, the left child of the parent node is set to the left child of node, and if node is the right child of the parent node, the right child pointer of the parent node is set to the left child of node to be deleted. Finally, delete node.
D. Node to be deleted has left child without right child, no parent node
The left child of node that will be deleted is set to the root node and delete node.
E. Node you want to delete has right child, no left child, parent node
If the left child of the parent node is to be deleted, the left child of the parent node is set to the right child to be deleted, and if node is the right child of the parent node, the right child pointer of the parent node is set to the right child of node to be deleted. Finally, delete node.
F. Node to be deleted has right child no left child, no parent node
The right child of node that will be deleted is set to the root node and delete node.
G. Node to be deleted all children have, have parent nodes
The right child who will be deleted node is inserted into the left child. If the left child of the parent node is to be deleted, the left child of the parent node is set to the left child of node, and if node is the right child of the parent node, the right child pointer of the parent node is set to the left child of node to be deleted. Finally, delete node.
H. Node to be deleted all children have, no parent node will be deleted node's right child inserted into the left child, the parent node is modified to be deleted node's left child, delete node nodes.
The C code is as follows:
#include <stdio.h> #include <stdlib.h> #define SIZE typedef struct tagnode{int value;
struct tagnode* left;
struct tagnode* right;
}treenode;
Print array void displayarray (int array[],int size) {printf ("The array is:");
int i;
for (i=0;i<size;i++) {printf ("%d", array[i]);
} printf ("\ n");
}//In the left-right order, walk the Tree void DisplayTree (treenode* node) {if (node = = NULL) return;
if (node->left! = NULL) {displaytree (node->left);
} printf ("%d", node->value);
if (node->right! = NULL) {displaytree (node->right);
}}//Find out if there is a node in the tree with node Vlaue treenode* searchtree (treenode* node, int value) {if (Node->value = = value) {
return node; }else if (Node->value > value) {if (node->left! = NULL) {return Searchtree (
Node->left, value); }else{return NULL; }}else{if (node->right! = NULL) {return Searchtree (Node->right, Val
UE);
}else{return NULL;
}}}//Find the node in the tree with node as Vlaue, parent of the node to find. Dir 1 indicates that the left node of the parent node is a lookup result//dir 2 indicates that the right node of the parent node is the lookup result treenode* searchtreewithparent (treenode* node, treenode**
Parent, int* dir, int value) {if (Node->value = = value) {return node;
}else if (Node->value > value) {if (node->left! = NULL) {*dir = 1;
*parent = node;
Return Searchtreewithparent (Node->left, parent, dir, value);
}else{return NULL;
}}else{if (node->right! = NULL) {*dir = 2;
*parent = node; Return Searchtreewithparent (node->right, parent, dir, value);
}else{return NULL; }}}//The INode is inserted into the tree with node root as void Insertnode (treenode* node, treenode* iNode) {if (Inode->value >=
Node->value && node->right! = NULL) {Insertnode (node->right, INode);
Return } if (Inode->value < Node->value && Node->left! = NULL) {Insertnode (node->l
EFT, INode);
Return } if (Inode->value >= node->value && node->right = = NULL) {node->right = in
Ode } if (Inode->value < node->value && Node->left = = NULL) {node->left = INode
;
The node void Deletenode (treenode** root, int value) {treenode* = NULL is removed from the tree rooted as root;
int dir =-1; TreeNode* Deletenode = searchtreewithparent (*root,&parent,&dir,value);
if (Deletenode = = NULL) {printf ("%s\n", "Node not found");
}else{if (Deletenode->left = = NULL && Deletenode->right = = null) {//A in the corresponding description if (parent = NULL) {if (dir = = 1) Parent
->left = NULL;
else parent->right = NULL;
}else{//b *root = NULL in the corresponding description; }}else if (deletenode->left! = null && Deletenode->right = = null) {/ The c if (parent! = NULL) {if (dir = = 1) in the corresponding description paren
T->left = deletenode->left; else parent->right = deletenode->left;
}else{//d *root = deletenode->left; in the corresponding description }}else if (deletenode->left = = NULL && deletenode->right! = null) {/ /corresponding description of E if (parent! = NULL) {if (dir = = 1) paren
T->left = deletenode->right;
else Parent->right = deletenode->right;
}else{//f *root = deletenode->right; in the corresponding description
}}else{Insertnode (deletenode->left,deletenode->right);
G if (Parent! = NULL) in the corresponding description {if (dir = = 1)
Parent->left = deletenode->left; ElsE parent->right = deletenode->left;
}else{//h *root = deletenode->left; in the corresponding description
}} free (Deletenode);
Deletenode = NULL;
}}//Use the number in array arrays to create a tree rooted root node, void Createtree (treenode** root, int array[], int size) {int i;
*root = (treenode*) malloc (sizeof (TreeNode));
(*root)->value = array[0];
(*root)->left = NULL;
(*root)->right = NULL;
for (i=1;i<size;i++) {treenode* child = (treenode*) malloc (sizeof (TreeNode));
Child->value = Array[i];
Child->left = NULL;
Child->right = NULL;
Insertnode (*root, child);
}}//delete tree void DeleteTree (treenode* node) {if (node = NULL) return with node as root; if (node->left! = NULL) {DeleteTree (nodE->left);
} if (node->right! = NULL) {deletetree (node->right);
} if (node->left = = NULL && Node->right = = null) {Free (node);
node = NULL;
}} int main (int argc, char* argv[]) {int array[size] = {4,1,45,78,345,23,12,3,6,21};
Displayarray (array,size);
TreeNode *root = NULL;
Createtree (&root, array, SIZE);
printf ("The Tree Is (left->middle->right):");
DisplayTree (root);
printf ("\ n");
int value = Atoi (argv[1]);
treenode* parent = NULL;
int dir =-1;
printf ("Search value%d:", value);
if (Searchtree (root,value) = NULL) {printf ("%s\n", "exist");
}else{printf ("%s\n", "not exist");
} printf ("Delete value:%d", value);
Deletenode (&root,value);
printf ("\ n"); printf ("The Tree is (left->middle-> right): ");
DisplayTree (root);
printf ("\ n");
DeleteTree (root);
return 0; }