Week 13 - Item 2 - Routes found in a binary tree sorting tree

Posted by ExuLt4Nt on Sun, 05 Jul 2020 18:15:05 +0200

/*                          
 * Copyright (c) 2017, Computer College, Yantai University                      
 * All right reserved.                          
 *File name: BST            
 *Author: Yin Na                         
 *Completion date: Dec. 7, 2017                          
 *Version number: v1.0                         
 *                         
 *Problem Description: The path found in the binary tree sort tree   
 *Input Description: Standard Function Input                       
 *Program Output: Create and Find Binary Trees 
*/         

Description of the problem:

Designs an algorithm that outputs the path through which a keyword is searched when it is searched in a binary sort.

The algorithm designed for this project is reflected in the function int SearchBST(...) and void SearchResult().

#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef int KeyType;                    //Define keyword type
typedef char InfoType;
typedef struct node                     //Record type
{
    KeyType key;                        //Key Items
    InfoType data;                      //Other data fields
    struct node *lchild,*rchild;        //Left and right child pointer
} BSTNode;
int path[MaxSize];                      //Global variable to hold path
void DispBST(BSTNode *b);               //Function Description
int InsertBST(BSTNode *&p,KeyType k)    //Insert a node with the key k into the BST with *p as the root node
{
    if (p==NULL)                        //The original tree is empty and the newly inserted record is the root node
    {
        p=(BSTNode *)malloc(sizeof(BSTNode));
        p->key=k;
        p->lchild=p->rchild=NULL;
        return 1;
    }
    else if (k==p->key)
        return 0;
    else if (k<p->key)
        return InsertBST(p->lchild,k);  //Insert into the left subtree of *p
    else
        return InsertBST(p->rchild,k);  //Insert into the right subtree of *p
}
BSTNode *CreatBST(KeyType A[],int n)
//Build a binary sorting tree from keywords in array A
{
    BSTNode *bt=NULL;                   //Initially bt is an empty tree
    int i=0;
    while (i<n)
        InsertBST(bt,A[i++]);       //Insert A[i] into Binary Sort Tree T
    return bt;                          //Returns the root pointer of the established binary sort tree
}

//Find in the binary sort tree, record the passed nodes in the path, return the value to find the subscript of the node stored in the path
int SearchBST(BSTNode *bt,KeyType k,KeyType path[],int i)
{
    if (bt==NULL)
        return i;
    else if (k==bt->key)    //Node found
    {
        path[i+1]=bt->key;  //Output its path
        return i+1;
    }
    else
    {
        path[i+1]=bt->key;
        if (k<bt->key)
            SearchBST(bt->lchild,k,path,i+1);  //Recursive search in left subtree
        else
            SearchBST(bt->rchild,k,path,i+1);  //Recursive Find in Right Subtree
    }
}

//Find and display routes
void SearchResult(BSTNode *bt, int k1)
{
    int r, j;
    r = SearchBST(bt,k1,path,-1);
    for (j=0; j<=r; j++)
        printf("%3d",path[j]);
    printf("\n");
}

void DispBST(BSTNode *bt)
//Output binary sort tree bt in parentheses
{
    if (bt!=NULL)
    {
        printf("%d",bt->key);
        if (bt->lchild!=NULL || bt->rchild!=NULL)
        {
            printf("(");
            DispBST(bt->lchild);
            if (bt->rchild!=NULL) printf(",");
            DispBST(bt->rchild);
            printf(")");
        }
    }
}

int main()
{
    BSTNode *bt;
    KeyType k1=65, k2=32;
    int a[]= {43,91,10,18,82,65,33,59,27,73},n=10;
    printf("Created BST tree:");
    bt=CreatBST(a,n);
    DispBST(bt);
    printf("\n");
    printf("  lookup%d Keyword:",k1);
    SearchResult(bt,k1);
    printf("  lookup%d Keyword:",k2);
    SearchResult(bt,k2);
    return 0;
}
Run Results