Basic operations on Binary Search Tree (BST) in C Language
We explained Binary Tree in the previous blog. This time, we will implement the Binary Search Tree (Binary Search Tree), also known as Binary Sort Tree ). Therefore, BST is short for BST. The binary search tree is defined as follows:
1. If the left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of its root node;
2. If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
3. Left and Right Subtrees are also Binary Decision Trees;
An important feature of the binary sorting tree is that the central order traversal is an incremental sequence.
(1) node Construction
typedef int elemType;typedef struct BTNode{ int data; struct BTNode *lChild; struct BTNode *rChild;}BiTNode,*BiTree;
(2) create a binary tree
The process of creating a binary sort tree is a process of constantly inserting nodes, and the most important thing is to find the proper location for insertion.
// Create a binary search tree/*** input-1, which is a process of continuous insertion */int CreateBinarySearchTree (BiTree * T) {printf ("Enter the sequence of numbers for creating the Binary Search Tree: \ n"); int num; scanf ("% d", & num); while (num! =-1) {Insert (T, num); scanf ("% d", & num);} printf ("% s function execution, binary Search Tree created \ n ",__ FUNCTION _); return 1 ;}
(3) Insert a node
// Insert node void Insert (BiTree * T, int x) {// here create a node BiTNode * pInsert = (BiTNode *) malloc (sizeof (BiTNode) to be inserted )); pInsert-> data = x; pInsert-> lChild = NULL; pInsert-> rChild = NULL; if (* T) = NULL) {* T = pInsert ;} if (* T)-> lChild = NULL & x <(* T)-> data) {(* T)-> lChild = pInsert ;} if (* T)-> rChild = NULL & x> (* T)-> data) {(* T)-> rChild = pInsert ;} // recursively implement if (x <(* T)-> data) {Insert (& (* T)-> lChild, x);} if (x> (* T) -> data) {Insert (& (* T)-> rChild, x) ;}return ;}
(4) Middle-order traversal and first-order traversal of the tree
The first-order traversal + middle-order traversal of a tree can uniquely identify a tree. And the central traversal of the binary sorting tree must be an incremental sequence. This method can be used to verify whether a binary sorting tree is correct.
// Traverse the binary search tree in the middle order // print an incremental void MiddleOrder (BiTree T) {if (T = NULL) {return ;} else {MiddleOrder (T-> lChild); printf ("% d", T-> data); MiddleOrder (T-> rChild );}} // traverse the Binary Search Tree in the First Order // because the first order traversal + the middle order traversal can uniquely identify a tree, etc., you can verify whether the tree is correct void PreOrder (BiTree T) {if (T = NULL) {return;} else {printf ("% d", T-> data); PreOrder (T-> lChild ); preOrder (T-> rChild );}}
(5) Search for elements
// Search for a value // 1 indicates that the value is found. 0 indicates that the BiTNode * SearchValue (BiTree T, int x) is not found. {if (T = NULL) {return 0;} else {if (x <T-> data) {// query the left subtree SearchValue (T-> lChild, x );} else if (x> T-> data) {// query the right subtree SearchValue (T-> rChild, x );} else {// locate the value printf ("memory address of this value: % p \ n", T); return T ;}} return NULL ;}
(6) Delete an element
// Delete an element BiTree * DeleteValue (BiTree * T, int x) {// you must first find the node and save the parent node BiTNode * searchNode of the node; biTNode * parentNode; // initially all points to the root node searchNode = * T; parentNode = * T; while (searchNode-> data! = X) {if (searchNode-> lChild = NULL & searchNode-> rChild = NULL) {// It is already a leaf node, but this value is not found yet, indicates that no node printf ("% s FUNCTION execution, this value does not exist at all, delete failed \ n" ,__ FUNCTION _); return T ;} if (x <searchNode-> data) {parentNode = searchNode; searchNode = searchNode-> lChild;} else if (x> searchNode-> data) {parentNode = searchNode; searchNode = searchNode-> rChild;} else {break;} if (searchNode-> lChild = NULL & searchNo De-> rChild = NULL) {// is the leaf node if (* T)-> lChild = NULL & (* T)-> rChild = NULL) {// is the root node free (* T); * T = NULL;} else {// not the root node, is a common leaf node // you need to determine whether the node to be deleted is the left child of the parent node or the right child if (searchNode-> data <parentNode-> data) {// left child parentNode-> lChild = NULL;} else {// right child parentNode-> rChild = NULL;} free (searchNode); searchNode = NULL ;} return T;} if (searchNode-> lChild! = NULL & searchNode-> rChild = NULL) {// There is a left subtree and no right subtree. // direct the pointer of the parent node to the right subtree, then release yourself. // first, you need to determine whether the current node is the parent node's left child or right child if (searchNode-> data <parentNode-> data) {// left child parentNode-> lChild = searchNode-> lChild;} else {// right child parentNode-> rChild = searchNode-> lChild;} free (searchNode ); searchNode = NULL; return T;} if (searchNode-> lChild = NULL & searchNode-> rChild! = NULL) {// No left subtree, right subtree if (searchNode-> data <parentNode-> data) {// left child parentNode-> lChild = searchNode-> rChild;} else {// right child parentNode-> rChild = searchNode-> rChild;} free (searchNode ); searchNode = NULL; return T;} if (searchNode-> lChild! = NULL & searchNode-> rChild! = NULL) {// The node to be deleted contains both the left child and the Right child./*** algorithm: The left and right subtree of the delete node p are not empty. Find the successor y of p. Because y does not have the left subtree, you can delete y and make y's parent node the parent node of y's right subtree, replace the value of p with the value of y. How to Find the successor node to delete the node, including the parent node of the successor Node !! */BiTNode * nextNode; // find the successor node of the node to be deleted. BiTNode * nextParentNode; // find the parent node nextParentNode = searchNode of the successor node of the node to be deleted; nextNode = searchNode-> rChild; while (nextNode-> lChild! = NULL) {nextParentNode = nextNode; nextNode = nextNode-> lChild;} // determine the value of nextNode and nextParentNode, because you need to determine whether the node is the parent's left or right child if (nextNode-> data <nextParentNode-> data) {// The left child nextParentNode-> lChild = nextNode-> rChild;} else {// The right child nextParentNode-> rChild = nextNode-> rChild ;} // Replace the value searchNode-> data = nextNode-> data; // Delete the next node free (nextNode); nextNode = NULL; return T ;}
(7) Test code
Int main (int argc, const char * argv []) {BiTree tree; // This is a reference for passing CreateBinarySearchTree (& tree); MiddleOrder (tree ); printf ("\ n"); printf ("Enter the element to be searched:"); int searchValue; scanf ("% d", & searchValue); SearchValue (tree, searchValue); printf ("Enter the element to be deleted:"); int deleteValue; scanf ("% d", & deleteValue); DeleteValue (& tree, deleteValue ); printf ("first-order traversal:"); PreOrder (tree); printf ("\ n middle-order traversal:"); MiddleOrder (tree ); // print and check printf ("\ n"); return 0 ;}