它是一个寻找二叉搜索树的最小值的程序。
Begin Declare nd as a structure. Declare d to the integer datatype. Declare pointer lt to the structure nd type. Declare pointer lt to the structure nd type. Declare function new_nd() to the structure nd type. Declare d to the integer datatype. Pass them as a parameter. Declare pointer nd to the structure nd type. Initialize nd = (struct nd*) malloc(sizeof(struct nd)). nd->d = d. nd->lt = NULL. nd->rt = NULL. return(nd). End Begin Declare function add_node() to the structure nd type. Declare pointer nd to the structure nd type. Declare d to the integer datatype. Pass them as a parameter. if (nd == NULL) then return(new_nd(d)). else if (d <= nd->d) then nd->lt = add_node(nd->lt, d). else nd->rt = add_node(nd->rt, d). return nd. End Begin Declare minimum_val() function to the integer datatype. Declare pointer nd to the structure nd type. Pass it as a parameter. Declare pointer cur to the structure nd type. Initialize cur = nd. while (cur->lt != NULL) do cur = cur->lt. return(cur->d). Declare pointer root to the structure nd type. Initialize root = NULL. root = add_node(root, 54). add_node(root, 32). add_node(root, 25). add_node(root, 45). add_node(root, 65). add_node(root, 75). Print "The Minimum value of the given binary search tree is: ". Print the minimum value of binary tree. getchar(). End.
#include <bits/stdc++.h> using namespace std; struct nd { int d; struct nd* lt; struct nd* rt; }; struct nd* new_nd(int d) { struct nd* nd = (struct nd*) malloc(sizeof(struct nd)); nd->d = d; nd->lt = NULL; nd->rt = NULL; return(nd); } struct nd* add_node(struct nd* nd, int d) { if (nd == NULL) return(new_nd(d)); else { if (d <= nd->d) nd->lt = add_node(nd->lt, d); else nd->rt = add_node(nd->rt, d); return nd; } } int minimum_val(struct nd* nd) { struct nd* cur = nd; while (cur->lt != NULL) { cur = cur->lt; } return(cur->d); } int main() { struct nd* root = NULL; root = add_node(root, 54); add_node(root, 32); add_node(root, 25); add_node(root, 45); add_node(root, 65); add_node(root, 75); cout << "The Minimum value of the given binary search tree is: " << minimum_val(root); getchar(); return 0; }
输出结果
The Minimum value of the given binary search tree is: 25