binary search tree · doxygen: binary search tree by milind padalkar. 5.1 bst.c file reference 17...

22
Binary Search Tree 1.0 Generated by Doxygen 1.7.1 Mon Jun 6 2011 16:12:39

Upload: others

Post on 21-Feb-2021

11 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Binary Search Tree1.0

Generated by Doxygen 1.7.1

Mon Jun 6 2011 16:12:39

Page 2: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1
Page 3: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Contents

1 Binary Search Tree Program 1

1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1

2 Data Structure Index 3

2.1 Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

3 File Index 5

3.1 File List . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

4 Data Structure Documentation 7

4.1 BST Struct Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4.1.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

4.1.2 Field Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4.1.2.1 data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4.1.2.2 left . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4.1.2.3 right . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

5 File Documentation 9

5.1 bst.c File Reference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

5.1.1 Detailed Description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

5.1.2 Function Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

5.1.2.1 createNewNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

5.1.2.2 deleteNode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

5.1.2.3 main . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

5.1.2.4 traverseBST . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5.1.3 Variable Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

5.1.3.1 root . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17

Page 4: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1
Page 5: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Chapter 1

Binary Search Tree Program

1.1 Introduction

A simple program to create a Binary Search Tree. This will also make me comfortable with using Doxygen.

This is really comfortable. I can also generate a custom list as follows.

• Item1

• Item2

• Item3

• Item4

I can also add formulae like the following one.√(x2 − x1)2 + (y2 − y1)2

I can also insert an image as follows.

Figure 1.1: image-binary_tree

And also create external links like this.

Page 6: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

2 Binary Search Tree Program

Author

Milind G. Padalkar

Date

26-Oct-2010

Doxygen: Binary Search Tree by Milind Padalkar

Page 7: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Chapter 2

Data Structure Index

2.1 Data Structures

Here are the data structures with brief descriptions:

BST (An ADT for Binary Search Tree ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

Page 8: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

4 Data Structure Index

Doxygen: Binary Search Tree by Milind Padalkar

Page 9: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Chapter 3

File Index

3.1 File List

Here is a list of all documented files with brief descriptions:

bst.c (A simple program to create a Binary Search Tree. This will also make me comfortablewith using Doxygen ) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

Page 10: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

6 File Index

Doxygen: Binary Search Tree by Milind Padalkar

Page 11: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Chapter 4

Data Structure Documentation

4.1 BST Struct Reference

An ADT for Binary Search Tree.

Collaboration diagram for BST:

BST

+ left+ data+ right

rightleft

Data Fields

• struct BST ∗ left

• int data

• struct BST ∗ right

4.1.1 Detailed Description

An ADT for Binary Search Tree. This ADT consists of a link to the left subtree, data at the current vertex& a link to the right subtree.

Definition at line 38 of file bst.c.

Page 12: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

8 Data Structure Documentation

4.1.2 Field Documentation

4.1.2.1 int data

Data at the current node of the BST.

Definition at line 40 of file bst.c.

4.1.2.2 struct BST∗ left

A pointer to the left subtree.

Definition at line 39 of file bst.c.

4.1.2.3 struct BST∗ right

A pointer to the right subtree.

Definition at line 41 of file bst.c.

The documentation for this struct was generated from the following file:

• bst.c

Doxygen: Binary Search Tree by Milind Padalkar

Page 13: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Chapter 5

File Documentation

5.1 bst.c File Reference

A simple program to create a Binary Search Tree. This will also make me comfortable with using Doxygen.

#include <stdio.h>

#include <stdlib.h>

Include dependency graph for bst.c:

bst.c

stdio.h stdlib.h

Data Structures

• struct BST

An ADT for Binary Search Tree.

Functions

• void createNewNode ()

Function to create a new node.

Page 14: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

10 File Documentation

• void traverseBST (struct BST ∗curNode)Function to traverse the BST.

• void deleteNode ()Function to delete a particular node from the BST.

• int main (int argc, char ∗∗argv)The main function. Should not be passed any arguments.

Variables

• struct BST ∗ root = NULLThis is pointer to the root node.

5.1.1 Detailed Description

A simple program to create a Binary Search Tree. This will also make me comfortable with using Doxygen.

Definition in file bst.c.

5.1.2 Function Documentation

5.1.2.1 void createNewNode ( )

Function to create a new node.

When called, this function creates a new node. The new nodes left and right pointers are set to NULL anddata is set to the value entered by the user. Once the new node is ready, it needs to be attached at the properposition in the tree. So, the root node is checked. If it is NULL, then the newly created node is assigned tothe root. IF the root is not NULL, then current node pointer is associated to the root. Further, the value inthe newly created node is checked iteratively with the value at the current node & accordingly the followingoperations are carried out.

• If newNode->data <= curNode->data : Assign newNode to curNode & break if curNode->left isNULL, else assign curNode to curNode->left i.e. move to the left subtree.

• If newNode->data > curNode->data : Assign newNode to curNode & break if curNode->right isNULL, else assign curNode to curNode->right i.e. move to the right subtree.

Parameters

NULL

Returns

void. A newly created node is attached at the proper place to the Binary Search Tree.Prints a message “New node creation & attachment successful” on the command-line.

See also

traverseBST(), deleteNode(), main().

Doxygen: Binary Search Tree by Milind Padalkar

Page 15: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

5.1 bst.c File Reference 11

Definition at line 125 of file bst.c.

{struct BST *newNode = ( struct BST * )malloc( sizeof( struct BST ) );newNode->left = NULL;newNode->right = NULL;printf( "\nEnter the value of the new node. : " );scanf( "%d", &newNode->data );struct BST *curNode;if( root==NULL ){

root = newNode;}else{

curNode = root;while( 1 ){

if( newNode->data <= curNode->data ){if( curNode->left == NULL ){

curNode->left = newNode;break;

}else{

curNode = curNode->left;}

}else{

if( curNode->right == NULL ){curNode->right = newNode;break;

}else{

curNode = curNode->right;}

}}

}printf( "\nNew node creation & attachment successful.\n" );

}

Here is the caller graph for this function:

createNewNode main

5.1.2.2 void deleteNode ( )

Function to delete a particular node from the BST.

This function will ask the user to enter a value (temp_val) to be deleted. The tree will be first searched forthis value. If found, it will lead to the the following conditions.

• A particular node has no childern. So it can be simply deleted & its parent’s reference can be set tonull.

Doxygen: Binary Search Tree by Milind Padalkar

Page 16: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

12 File Documentation

• A particular node has only child-node. In this case, the parent’s pointer is assigned to the only childof the current node.

• A particular node has both children. In this case, the parent’s pointer is assigned to the left-child’srightmost node. If this rightmost node has a left child, then it takes the position of the rightmostnode. If the rightmost node comes out to be the current node’s left child, then simply assign theparent’s pointer is directly assigned to this node & the current node can be removed.

Parameters

NULL. No parameters are passed to this function.

Returns

void. The command-line prints message on successful deletion of the given node. It also prints thecontents of the BST after deletion operation.

See also

createNewNode(), traverseBST(), main().

Definition at line 185 of file bst.c.

{int temp_val;printf( "Enter the value of the node to be deleted : " );scanf( "%d", &temp_val );struct BST *parentNode = NULL;struct BST *curNode = root ;int found = 0;while( root!=NULL ){

if( temp_val == curNode->data ){found = 1;break;

}parentNode = curNode;if( temp_val < curNode->data ){

if( curNode->left==NULL ){break;

}curNode = curNode->left;

}else{

if( curNode->right==NULL ){break;

}curNode = curNode->right;

}}if( found==0 ){

printf( "\nNode does not exist. \n" );}else{

//printf( "\nNode %d found. \n", curNode->data );if( ( curNode->left==NULL ) && ( curNode->right==NULL ) ){

if( parentNode!=NULL ){if( curNode == parentNode->left ){

parentNode->left = NULL;}else{

parentNode->right = NULL;}

Doxygen: Binary Search Tree by Milind Padalkar

Page 17: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

5.1 bst.c File Reference 13

}else{

root = NULL;}free( curNode );printf( "Node deleted successfully. Modified contents are

\n" );traverseBST( root );printf( "\n" );

}if( ( ( curNode->left!=NULL ) && ( curNode->right==NULL ) ) || (

( curNode->left==NULL ) && ( curNode->right!=NULL ) ) ){struct BST *child;if( curNode->left!=NULL ){

child = curNode->left;}else{

child = curNode->right;}if( parentNode!=NULL ){

if( curNode == parentNode->left ){parentNode->left = child;

}else{

parentNode->right = child;}

}else{

root = child;}free( curNode );printf( "Node deleted successfully. Modified contents are

\n" );traverseBST( root );printf( "\n" );

}if( ( curNode->left!=NULL ) && ( curNode->right!=NULL ) ){

struct BST *rightmpar = curNode;struct BST *rightmost = curNode->left;while( rightmost->right!=NULL ){

rightmpar = rightmost;rightmost=rightmost->right;

}if( rightmost != curNode->left ){

if( rightmost->left!=NULL ){rightmpar->right = rightmost->left;

}else{

rightmpar->right = NULL;}

}if( parentNode!=NULL ){

if( curNode == parentNode->left ){parentNode->left = rightmost;

}else{

parentNode->right = rightmost;}

}else{

root = rightmost;}if( rightmost != curNode->left ){

rightmost->left = curNode->left;}rightmost->right = curNode->right;free( curNode );

Doxygen: Binary Search Tree by Milind Padalkar

Page 18: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

14 File Documentation

printf( "Node deleted successfully. Modified contents are\n" );

traverseBST( root );printf( "\n" );

}}

}

Here is the call graph for this function:

deleteNode traverseBST

Here is the caller graph for this function:

deleteNode main

5.1.2.3 int main ( int argc, char ∗∗ argv )

The main function. Should not be passed any arguments.

This is the main function of the Binary Search Tree Creation Program.

Parameters

argc Number of arguments passed to the executable.

argv A Pointer to every string passed as the argument to the executable.

Returns

0 if program exits normally.Negative if forced to exit before reaching the end.

The value of an integer ch, which stores the choice entered by the user, is switched over a loop and thefollowing operations are performed depending on its value.

• ch = 1 => Create node operation is selected.

Doxygen: Binary Search Tree by Milind Padalkar

Page 19: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

5.1 bst.c File Reference 15

• ch = 2 => Delete node operation is selected.

• ch = 3 => Tree traversal is selected.

• ch = 4 => Exit

See also

createNewNode(), traverseBST(), deleteNode().

Definition at line 67 of file bst.c.

{

int ch;do{

printf( "\nEnter Choice:" );printf( "\n1. Create a node" );printf( "\n2. Delete a node" );printf( "\n3. Traverse the tree" );printf( "\n4. Exit\nEnter Choice : " );scanf( "%d", &ch );switch( ch ){

case 1:createNewNode();break;case 2:deleteNode();break;case 3:{

if( root==NULL ){printf( "No nodes exist.\n" );

}else{

traverseBST( root );printf( "\n" );

}}break;case 4:break;default:printf( "Invalid Choice. \n" );break;

}}while( ch!=4 );

return( 0 );}

Doxygen: Binary Search Tree by Milind Padalkar

Page 20: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

16 File Documentation

Here is the call graph for this function:

main

createNewNode

deleteNode

traverseBST

5.1.2.4 void traverseBST ( struct BST ∗ curNode )

Function to traverse the BST.

This function performs inorder traversal of the Binary Search Tree. It is a recursive function. Therefore,the argument it takes is a pointer to the starting node of the sub-tree to be traversed. Hence, when we wantto traverse the complete tree, the pointer to the root node is passed as the argument to this function.

The inorder traversal is performed as follows.

• For any given node, first visit its left sub-tree ( this is recursive ).

• When the left sub-tree is completely traversed, print the value at the current node.

• Finally, visit the right sub-tree ( even this is recursive ).

Parameters

∗curNode is Pointer to the starting node of the sub-tree / tree to be traversed.

Returns

void. The command-line prints the value at the various nodes in accordance to inorder traversal.

See also

createNewNode(), deleteNode(), main().

Definition at line 174 of file bst.c.

{if( curNode ){

traverseBST( curNode->left );printf( "%d ", curNode->data );traverseBST( curNode->right );

}}

Doxygen: Binary Search Tree by Milind Padalkar

Page 21: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

5.1 bst.c File Reference 17

Here is the caller graph for this function:

traverseBST

deleteNode

main

5.1.3 Variable Documentation

5.1.3.1 struct BST∗ root = NULL

This is pointer to the root node.

This node pointer is empty initially. The value of the first input variable is assigned to the root node. Later,other nodes are created according to the property of the Binary Search Tree.

Definition at line 48 of file bst.c.

Doxygen: Binary Search Tree by Milind Padalkar

Page 22: Binary Search Tree · Doxygen: Binary Search Tree by Milind Padalkar. 5.1 bst.c File Reference 17 Here is the caller graph for this function: 5.1.3 Variable Documentation 5.1.3.1

Index

BST, 7data, 8left, 8right, 8

bst.c, 9createNewNode, 10deleteNode, 11main, 14root, 17traverseBST, 16

createNewNodebst.c, 10

dataBST, 8

deleteNodebst.c, 11

leftBST, 8

mainbst.c, 14

rightBST, 8

rootbst.c, 17

traverseBSTbst.c, 16