Home > Back-end >  Binary tree search
Binary tree search

Time:12-02

#include
#include

# define ENDKEY 0

Typedef int KeyType;

Typedef struct node
{
KeyType key;/* the value of the keyword */
Lchild struct node * and * rchild;/* pointer */
} BSTNode, * BSTree;

Void InsertBST (BSTree * BST, KeyType key)
/* if there is no key words in the binary sort tree is equal to the key elements, insert the element */
{
BSTree s;
If (* BST==NULL)/* recursive end conditions */
{
S=(BSTree) malloc (sizeof (BSTNode));/* to apply for a new node s */
S - & gt; Key=key;
S - & gt; Lchild=NULL;
S - & gt; Rchild=NULL;
* BST=s;
}
The else
If (key & lt; (* BST) - & gt; Key)
InsertBST (& amp; (* (BST) - & gt; Lchild), key);/* insert s left subtree */
The else
If (key & gt; (* BST) - & gt; Key)
InsertBST (& amp; (* (BST) - & gt; Rchild), key);/* insert s right subtree */
}

Void CreateBST BSTree * (BST)
/* from the keyboard input element value, create the corresponding binary sort tree */
{
KeyType key;
* BST=NULL;
The scanf (" % d ", & amp; Key);
While (key!=ENDKEY)/* ENDKEY for custom constants */
{
InsertBST (BST, key);
The scanf (" % d ", & amp; Key);
}
}
//first order traversing binary tree
Void PreOrder (BSTree root)
/* first sequence traversing binary tree, the root for the pointer to the binary tree root node */
{
If (root!=NULL)
{
Printf (" % d ", root - & gt; Key);/* */output nodes
PreOrder (root - & gt; Lchild);/* sequence traversal first left subtree */
PreOrder (root - & gt; Rchild);/* first sequence traversal right subtree */
}
}

//recursive
BSTree SearchBST (BSTree BST, KeyType key)
//in the hand the BST refers to binary sort tree, the recursive search for a keyword is the key element, if this succeeds, returns pointer to the element nodes, otherwise returns null pointer
{
if (! BST)
return NULL;
The else
If (BST - & gt; Key==key)
Return the BST.//find success
The else
If (BST - & gt; The key & gt; Key)
Return SearchBST (BST - & gt; Lchild, key);//in the left subtree continue to search for
The else
Return SearchBST (BST - & gt; Rchild, key);//in the right subtree continue to find
}

//non-recursive
BSTree SearchBST (BSTree BST, KeyType key)
/* in the hand the BST refers to binary sort tree BST, find the key words is the key node, if this succeeds, returns pointer to the element nodes, otherwise returns null pointer */
{
BSTree q;
Q=BST;
While (q)
{
If (q - & gt; Key==key)
The return of q;/* find success */
If (q - & gt; The key & gt; Key)
Q=q - & gt; Lchild;/* to look for in the left subtree */
The else
Q=q - & gt; Rchild;/* to look for in the right subtree */
}
return NULL;/* * lookup failure/
SearchBST}/* */




Void main ()
{
BSTree T;
int k;
Int q;
BSTree result;
Printf (" establish a binary sort tree, please input sequence: \ n ");
CreateBST (& amp; T);
Printf (" output sequence traversal sequence is: the first ");
PreOrder (T);
Printf (" \ n please enter to find elements: ");
fflush(stdin);
The scanf (" % d ", & amp; K);
Q=SearchBST (T, k);
If (q!=NULL)
Printf (" to find elements for % d \ n ", q);
The else
Printf (" was not found! \n");

} where is wrong, how to find the position in which one

CodePudding user response:

Suggest you carefully think about q!=nullptr, your final output is what things
  • Related