Binary Search Tree Operations

Source: Internet
Author: User

This article learns: two forks search (sort) The normal operation of the tree

Include: Insert, Delete, find, pre-sequence traversal, middle sequence traversal, post-order traversal, tree maximum depth, minimum depth, maximum value, minimum and number of nodes.


You need to understand the definition of binary search tree first:

A binary search tree is either an empty tree or a two-fork tree with the following properties:
(1) The Joz tree is not empty, then the value of all nodes on the left subtree is less 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) The left and right sub-trees are also two-fork sorting trees respectively;
(4) There are no nodes with key values equal.



Figure 1

1, is a binary search tree!


The program is parsed as follows:

(0) Prerequisite Preparation:

Made a structure, easy to operate.

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >struct Node {int value;struct node*left;struct node*right;}; typedef struct Node Node;typedef struct node*pnode;</span>

(1) Insert function Insert_node (pnode *n int value)

Idea: The parameter passed into the insertion function is (1) the pointer to the tree (2) the value to insert

If the tree is empty, create a new tree so that its root (root) is the value to be inserted

If the tree is not empty, then the value is compared to the root of the original tree (relative root), and if it is smaller than that, it must be inserted into the left subtree, and if larger than it, it must be inserted into the right subtree. This is where recursion is used, the pointer to Zuozi is changed to the parameter value, the call to the Insert_node function is made, and the right subtree is the same.

Notice here the binary search tree (4), if the value to be inserted already exists in tree, then do nothing, directly return.


C Language code:

(1) Insert node function

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >void Insert_node (pnode* n,int value) {if (*n = = NULL) {new_node (n,value);} else if ((*n)->value = = value) {return;} else if (value < (*n)->value) {Insert_node (& ((*n)->left), value);} else {Insert_node (& (*n)->right, value);}} </span>
(2) New node function

<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >void New_node (pnode* n,int value) {*n = (pnode) malloc (sizeof (node)), if (*n! = NULL) {(*n)->value = value; (*n)->l EFT = null; (*n)->right = null;}} </span>

(2) Delete Delet_node (pnode* n,int value)
This is one of the most troublesome operations! There are 4 scenarios to consider because of the deletion.

Ideas:

(1) If the node to be deleted is a leaf node: for example, I want to delete 10 (or 30, or 50)


Figure 1

Set the pointer to 20, which points to the node 10, to null, and then release it.

(2) If the node to be deleted has only the right node: I want to delete the 20


Figure 2

Replace the 30 node with the 20 position and release it.

(3) If the node to be deleted has only the left node: Same as (2) (cannot.) )

(4) If the node to be deleted has both a left node (left subtree) and a node (subtree): I'm going to delete 20.


Figure 1

You can find the maximum value in the left subtree of the 20 node or find the smallest value in the right subtree, replace the 20 position, release it, and then adjust the tree.

The first case can be combined with the second case during the encoding process.

The parameter of the incoming delete function is (1) the pointer to the tree (2) the value to delete


C Language code:

(1) Decide which one to delete

void Delet_node (pnode* n,int value) {if (*n = = NULL) {return;} if ((*n)->value = = value) {deletenode (n);} else if (value < (*n)->value) {Delet_node (& ((*n)->left), value);} else {Delet_node (& (*n)->right, value);}}
If the tree is non-empty, compare the current node to the node to be deleted and, if unequal, make the left and right comparisons, if it is equal to delete work.

(2) Judging if the deletion

void Deletenode (pnode* n) {Pnode temp =  null;if (n = = NULL) {return;} if ((*n)->right = = NULL) {temp = *n;*n = (*n)->left;free_node (n);} else if ((*n)->left = = NULL) {temp = *n;*n = (*n)->right;free_node (n);} else {for (temp = = (*n)->right;temp->left!=null;temp = temp->left); temp->left = (*n)->left;temp = (*n); *n = (*n)->right;free_node (&temp);}}
This is the implementation of the Code, which is discussed in 4 scenarios (consolidated into 3).

(3) Official deletion

void Free_node (pnode* n) {if (*n!=null) {free (*n); *n = NULL;}}
Here is the real delete node operation.


(3) Find Find_node (pnode n,int value)

With the basis of the above two functions, it is not very difficult to write a lookup, or to use recursion. Note, however, that the first two do not need to return a value, so it is set to void type, but the lookup will have a return value, whether it is null, so the function return type is Pnode

C Language code:

Pnode Find_node (pnode n,int value) {if (n = = null) {return null;} else if (N->value ==value) {return n;} else if (value <= n->value) {return find_node (n->left,value);} else {return Find_node (N->right,value);}}


(4So far, we've solved the most difficult three operations in the two-fork search tree, namely, inserting, deleting, and searching, and here are just a few of the other things that are easy to do, giving all the code directly.

#include <stdio.h> #include <malloc.h> #include <stdlib.h>struct node {int value;struct node*left; struct node*right;}; typedef struct Node Node;typedef struct node*pnode;void new_node (pnode* n,int value) {*n = (pnode) malloc (sizeof (node)); (*n! = NULL) {(*n)->value = value; (*n)->left = null; (*n)->right = null;}} void Free_node (pnode* n) {if (*n!=null) {free (*n); *n = NULL;}} void Deletenode (pnode* n) {Pnode temp = null;if (n = = NULL) {return;} if ((*n)->right = = NULL) {temp = *n;*n = (*n)->left;free_node (n);} else if ((*n)->left = = NULL) {temp = *n;*n = (*n)->right;free_node (n);} else {for (temp = = (*n)->right;temp->left!=null;temp = temp->left); temp->left = (*n)->left;temp = (*n); *n = (*n)->right;free_node (&temp);}} Pnode Find_node (pnode n,int value) {if (n = = null) {return null;} else if (N->value ==value) {return n;} else if (value <= n->value) {return find_node (n->left,value);} else {return Find_node (N->right,value);}} VoiD insert_node (pnode* N,int value) {if (*n = = NULL) {new_node (n,value);} else if ((*n)->value = = value) {return;} else if (value < (*n)->value) {Insert_node (& ((*n)->left), value);} else {Insert_node (& (*n)->right, value);}} void Delet_node (pnode* n,int value) {if (*n = = NULL) {return;} if ((*n)->value = = value) {deletenode (n);} else if (value < (*n)->value) {Delet_node (& ((*n)->left), value);} else {Delet_node (& (*n)->right, value);}} void Pre_order_traversal (Pnode n) {if (n! = NULL) {printf ("%d", n->value); Pre_order_traversal (N->left); Pre_order_traversal (N->right);}} void In_order_traversal (Pnode n) {if (n! = NULL) {in_order_traversal (n->left);p rintf ("%d", n->value); in_order_ Traversal (n->right);}} void Post_order_traversal (Pnode n) {if (n! = NULL) {post_order_traversal (n->left); Post_order_traversal (n->right);p rintf ("%d", N->value);}} int get_max_depth (pnode n) {int left = 0;int right = 0;if (n = = NULL) {return 0;} if (n->left! = NULL) {left=1 + get_max_depth (n->left);} if (n->right! = NULL) {right=1 + get_max_depth (n->right);} Return (Left > Right left:right);} int get_min_depth (pnode n) {int left = 0;int right = 0;if (n = = NULL) {return 0;} if (n->left! = NULL) {left = 1 + get_min_depth (n->left);} if (n->right! = NULL) {right = 1 + get_min_depth (n->right);} Return (Left > right)? Right:left;} int Get_max_value (pnode N) {if (n = = NULL) {return 0;} if (n->right = = NULL) {return n->value;} else {return get_max_value (n->right);}} int Get_min_value (pnode N) {if (n = = NULL) {return 0;} if (N->left = = NULL) {return n->value;} else {return get_min_value (n->left);}} int Get_num_nodes (pnode n) {int left = 0;int right = 0;if (n = = NULL) {return 0;} if (n->left! = NULL) {left = 1 + get_num_nodes (n->left);} if (n->right! = NULL) {right = 1 + get_num_nodes (n->right);} return left+right+1;} int main () {char buf[50];int option; Pnode tree = NULL; Pnode node = null;while (1) {printf ("\ n-----------------------\ n ");p rintf (" Options are:\n\n ");p rintf (" 0 exit\n ");p rintf (" 1 Insert node\n ");p rintf (" 2 delet No de\n ");p rintf (" 3 Find node\n ");p rintf (" 4 Pre Order traversal\n ");p rintf (" 5 in order traversal\n ");p rintf (" 6 Post O Rder traversal\n ");p rintf (" 7 Max depth\n ");p rintf (" 8 min depth\n ");p rintf (" 9 Max Value\n ");p rintf (" Ten Min Value\n ") ;p rintf ("one Node count\n\n");p rintf ("\ n-----------------------\ n");p rintf ("Select an option:"); scanf ("%d",& option);p rintf ("\ n------------------------\ n"), if (option < 0 | | option > One) {printf ("Invalid option!"); Continue;} Switch (option) {Case 0:return 0;case 1:printf ("Enter number to insert:") scanf ("%d", &option);p rintf ("\ n"); Inser T_node (&tree,option); Break;case 2:printf ("Enter number to Delet:"), scanf ("%d", &option);p rintf ("\ n"); Delet_node (&tree,option); Break;case 3:printf ("Enter number to find:") scanf ("%d", &option);p rintf ("\ n"); node = Find_node (tree,option); if (noDe! = NULL) {printf ("Found node\n\n");} else {printf ("couldn ' t find node\n\n");} Break;case 4:printf ("Pre Order Traversal:");p rintf ("\ n");    Pre_order_traversal (tree); break;    Case 5:printf ("In order Traversal:");    printf ("\ n");    In_order_traversal (tree);    Break    Case 6:printf ("Post Order Traversal:");    printf ("\ n");    Post_order_traversal (tree);    Break    Case 7:printf ("Max depth is%d\n\n", get_max_depth (tree));    Break    Case 8:printf ("Min depth is%d\n\n", get_min_depth (tree));    Break    Case 9:printf ("Max value is%d\n\n", get_max_value (tree));    Break    Case 10:printf ("Min value is%d\n\n", get_min_value (tree));    Break    Case 11:printf ("Node count is%d\n\n", get_num_nodes (tree)); Break;}} return 0;}


A lot of mistakes ~




Binary Search Tree Operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.