Home > Back-end >  Binary search trees
Binary search trees

Time:12-04

# include & lt; Stdio. H>
#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;


Printf (" establish a binary sort tree, please input sequence: \ n ");
CreateBST (& amp; T);
Printf (" output sequence traversal sequence is: the first ");
PreOrder (T);
//find
Printf (" \ n please enter to find elements: ");
The scanf (" % d ", & amp; K);
fflush(stdin);
SearchBST (T, k);
If (SearchBST (T, k)!=NULL)
Printf (" this element!" );
The else
Printf (" was not found! \n");

} where is wrong,
  • Related