Data Structure Experiment-Basic Operation of Tree-Binary Tree

Keywords: Programming

I. The Purpose and Requirements of the Experiment

1. Understanding the basic concepts and characteristics of binary trees
2. Grasp the chain storage structure of binary tree
3. Grasp the basic operation of binary tree

2. Experimental content (or experimental principle, experimental topology)

1. Implement the following operation of the binary tree, the binary tree as shown in the following figure. (implemented with binary chain storage structure)
(1) output binary tree b;
(2) Output C node's left and right child node values;
(3) Depth of output binary tree;
(4) Number of nodes that output binary tree b;
(5) Number of leaf nodes of output binary tree b.

3. Experimental design scheme (including experimental steps, design ideas, algorithmic description or development process, etc.)

According to the title, in order to create two. cpp files more conveniently, btree.h is used to store functions of basic operations, and main.cpp is used to write main functions to call other functions. The basic contents are as follows:
(1) Basic operation functions in btree.h (see appendix for detailed code):

(1) Create a binary tree CreateBTNode(*b,*str): Generate the corresponding binary chain storage structure based on the binary tree bracket representation string str, the root of which is * B.
(2) FindNode(BTNode *b,ElemType x): Find the node whose data field value is x in binary tree b, and return the pointer to the node.
(3) Find the left child node LchildNode(BTNode *p): Find the left child node of the node * P in the binary tree.
(4) Find the right child node RchildNode(BTNode *p): Find the right child node of the node * P in the binary tree.
(5) Get the height of BTNode Depth (BTNode * b): Get the height of the binary tree b, if the binary tree is empty, its height is 0; otherwise, its height is equal to the maximum height of the left subtree and the right subtree plus 1.
(6) Output Binary Tree DispBTNode(BTNode *b): Output a Binary Tree in parentheses.
(7) Find the number of nodes Nodes(BTNode *b): Find the number of nodes of binary tree B.
(8) LeafNodes(BTNode *b): the number of leaf nodes in binary tree B.
(9) Destroy the binary tree Destroy BTNode (BTNode*&b): Destroy the binary tree B.

(2) main.cpp
The basic operation functions are invoked according to the questions in turn and the easy-to-understand statement output is written.

4. Experimental results (including design results, test data, operation results, etc.)

The results are as follows:

5. Summary of the experiment (including harvest, experience, precautions, problems and solutions, suggestions, etc.)

This experiment is done by referring to the code in the book. I found that my programming ability still needs to be improved, and I also learned a lot. First of all, we know that the chain storage structure of binary tree is to store a binary tree with a linked list, and each node in the binary tree is stored with a node in the linked list. Then we learned the basic operation algorithm of binary tree, and gained a lot.

Appendix (including works, flow charts, source programs and command lists, etc.)

btree.h

#include <stdio.h>
#include <malloc.h>
#define MaxSize 100
typedef char ElemType;
//Definition of node type BTNode in binary chain 
typedef struct node
{
	ElemType data;				//data elements
	struct node *lchild;		//Point to the left child node
	struct node *rchild;		//Point to the right child node
} BTNode;
void CreateBTNode(BTNode *&b,char *str)		//Creating Binary Chains from str Strings
{
	BTNode *St[MaxSize],*p=NULL;
	int top=-1,k,j=0;  
	char ch;
	b=NULL;				//The initial time of the binary tree is empty
	ch=str[j];
	while (ch!='\0')	//str Unscanned Loop
	{
   	   	switch(ch) 
		{
		case '(':top++;St[top]=p;k=1; break;		//Left node
		case ')':top--;break;
		case ',':k=2; break;                      	//Right node
		default:p=(BTNode *)malloc(sizeof(BTNode));
			p->data=ch;p->lchild=p->rchild=NULL;
		         	if (b==NULL)                    //p points to the root node of the binary tree
						b=p;
					else  							//Binary tree root node has been established
					{	
						switch(k) 
						{
						case 1:St[top]->lchild=p;break;
						case 2:St[top]->rchild=p;break;
						}
					}
		}
		j++;
		ch=str[j];
	}
}
BTNode *FindNode(BTNode *b,ElemType x)	//Returns a pointer to a node whose data field is x
{
	BTNode *p;
	if (b==NULL)
	     return NULL;
	else if (b->data==x)
	     return b;
	else
	{	
		p=FindNode(b->lchild,x);
		if (p!=NULL) 
			return p;
		else 
			return FindNode(b->rchild,x);
	}
}
BTNode *LchildNode(BTNode *p)	//Returns the left child node pointer of the * p node
{
    return p->lchild;
}
BTNode *RchildNode(BTNode *p)	//Returns the pointer to the right child node of the * p node
{
    return p->rchild;
}
int BTNodeDepth(BTNode *b)	//Finding the Depth of Binary Tree b
{
   	int lchilddep,rchilddep;
   	if (b==NULL) 
		return(0); 							//The height of an empty tree is 0.
   	else  
	{	
		lchilddep=BTNodeDepth(b->lchild);	//Find the height of left subtree as lchilddep
	  	rchilddep=BTNodeDepth(b->rchild);	//Find the height of right subtree as rchilddep
		return (lchilddep>rchilddep)? (lchilddep+1):(rchilddep+1);
   	}
}
void DispBTNode(BTNode *b)	//Output binary trees in parentheses
{
	if (b!=NULL)
	{
		printf("%c",b->data);
		if (b->lchild!=NULL || b->rchild!=NULL)
		{
			printf("(");
			DispBTNode(b->lchild);
			if (b->rchild!=NULL) printf(",");
			DispBTNode(b->rchild);
			printf(")");
		}
	}
}

int Nodes(BTNode *b)	//Finding the Number of Nodes of Binary Tree b
{
	int num1,num2;
    if (b==NULL) 
		return 0;
    else if (b->lchild==NULL && b->rchild==NULL) 
		return 1;
    else
    {
        num1=Nodes(b->lchild);
        num2=Nodes(b->rchild);
        return (num1+num2+1);
	}
}
int LeafNodes(BTNode *b)	//Finding the Number of Leaf Nodes of Binary Tree b
{
	int num1,num2;
    if (b==NULL) 
		return 0;
    else if (b->lchild==NULL && b->rchild==NULL) 
		return 1;
    else
    {
        num1=LeafNodes(b->lchild);
        num2=LeafNodes(b->rchild);
        return (num1+num2);
	}
}
void DestroyBTNode(BTNode *&b)  //Destroying Binary Trees
{
	if (b!=NULL)
	{
		DestroyBTNode(b->lchild);
		DestroyBTNode(b->rchild);
		free(b);
	}
}

main.cpp

#include <stdio.h>
#include "btree.h"
int main()
{	BTNode *b,*p,*lp,*rp;
	CreateBTNode(b,"A(B(D(,G)),C(E,F))");
	printf("The basic operations of a binary tree are as follows:\n");
	printf("  (1)Output Binary Tree:");
    DispBTNode(b);printf("\n");
	printf("  (2)C node:");
	p=FindNode(b,'C');
	if (p!=NULL)
	{	lp=LchildNode(p);
		if (lp!=NULL) 
			printf("The left child is%c ",lp->data);
		else
			printf("No left child ");
		rp=RchildNode(p);
		if (rp!=NULL)
			printf("The right child is%c",rp->data);
		else
			printf("No right child ");
	}
	printf("\n");
	printf("  (3)Two fork tree b Depth:%d\n",BTNodeDepth(b));
	printf("  (4)Two fork tree b Number of nodes:%d\n",Nodes(b));
	printf("  (5)Two fork tree b Number of leaf nodes:%d\n",LeafNodes(b));
	printf("  (6)Release Binary Tree b\n");
	DestroyBTNode(b);
	return 0;
}

Posted by Wolfsoap on Thu, 26 Sep 2019 05:57:25 -0700