[C] Implementation of binary tree - Meow Meow’s Growth Story

Baozi, don’t you like it? Don't you want to comment? Don't you want to collect it?

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me.

Table of contents

Preface

Binary tree definition

special binary tree

Properties of binary trees (super important)

Code

Binary tree exercises

Summarize


Preface

Binary trees are implemented in C language, and some exercises will be added to consolidate the practice. So, let’s get started! Meow~


Binary tree definition

A binary tree is a finite set of nodes, which is:

  1. or empty
  2. It consists of a root node plus two binary trees, also known as the left subtree and the right subtree.

Notice:

  • There is no node with degree greater than2 in a binary tree
  • The subtrees of a binary tree can be divided into left and right subtrees, and the order cannot be reversed, so the binary tree is an ordered tree.

The trees in the following pictures are all binary trees.


special binary tree

  1. Full Binary Tree: A binary tree. If the number of nodes in each layer reaches the maximum, then the binary tree is a full binary tree. That is to say, if the number of levels of a binary tree is K and the total number of nodes is 2^k, then it is a full binary tree. (full binary tree
  2. Complete Binary Tree: A complete binary tree is a very efficient data structure. A complete binary tree is derived from a full binary tree. For a binary tree with depth K and n nodes, it is called a complete binary tree if and only if each node corresponds one-to-one with the nodes numbered from 1 to n in the full binary tree with depth K. It should be noted that a full binary tree is a special kind of complete binary tree.

as the picture shows:


Properties of binary trees (super important)

  1. If the number of levels of the root node is 1, then there are at most on the ith level of a non-empty binary tree  2^(i-1) nodes.
  2. If the number of levels of the root node is 1, thenthe maximum number of nodes of a binary tree with depth h is   2^h-1 .
  3. For any binary tree, if the degree is 0, the number of leaf nodes is n0   , The number of branch nodes with degree 2 is n2   1, then n0=n2 +,
  4. If the number of levels of the root node is 1, the depth of a full binary tree with n nodes, h=log2(n+1) (ps: log is based on 2, n+1 is the logarithm)
  5. For a complete binary tree with n nodes, if all nodes are numbered starting from 0 in array order from top to bottom, left to right, then for the node with serial number i:
  • i>0, i The parent number of the position node: (i-1)/2; i=0, i is the root node number, there is no parent node
  • Young2i+1<n,Leo 孭子 ordinal:2i+1 2i+1>=nNever leave the child
  • young2i+2<n, right child ordinal:2i+2 2i+2>=nNever say no right child

Code

typedef char BTDataType;

typedef struct BinaryTreeNode
{
	BTDataType _data;
	struct BinaryTreeNode* _left;
	struct BinaryTreeNode* _right;
}BTNode;

// 通过前序遍历的数组"ABD##E#H##CF##G##"构建二叉树
BTNode* BinaryTreeCreate(BTDataType* a, int n, int* pi);
// 二叉树销毁
void BinaryTreeDestory(BTNode** root);
// 二叉树节点个数
int BinaryTreeSize(BTNode* root);
// 二叉树叶子节点个数
int BinaryTreeLeafSize(BTNode* root);
// 二叉树第k层节点个数
int BinaryTreeLevelKSize(BTNode* root, int k);
// 二叉树查找值为x的节点
BTNode* BinaryTreeFind(BTNode* root, BTDataType x);
// 二叉树前序遍历 
void BinaryTreePrevOrder(BTNode* root);
// 二叉树中序遍历
void BinaryTreeInOrder(BTNode* root);
// 二叉树后序遍历
void BinaryTreePostOrder(BTNode* root);
// 层序遍历
void BinaryTreeLevelOrder(BTNode* root);
// 判断二叉树是否是完全二叉树
int BinaryTreeComplete(BTNode* root);
#include "BTree.h"
#include "queue.h" 
#include "stack.h"

BTNode *BinaryTreeCreate(BTDataType * src, int n, int* pi)
{
	if (*pi >= n || src[*pi] == '#')
	{
		(*pi)++;
		return NULL;
	}

	BTNode * cur = (BTNode *)malloc(sizeof(BTNode));
	cur->_data = src[*pi];
	(*pi)++;

	cur->_left = BinaryTreeCreate(src, n, pi);
	cur->_right = BinaryTreeCreate(src, n, pi);

	return cur;
}

void BinaryTreePrevOrder(BTNode* root)
{
	if (root)
	{ 
		putchar(root->_data);
		BinaryTreePrevOrder(root->_left);
		BinaryTreePrevOrder(root->_right);
	}
}

void BinaryTreeInOrder(BTNode* root)
{
	if (root)
	{
		BinaryTreeInOrder(root->_left);
		putchar(root->_data);
		BinaryTreeInOrder(root->_right);
	}
}

void BinaryTreePostOrder(BTNode* root)
{
	if (root)
	{
		BinaryTreePostOrder(root->_left);
		BinaryTreePostOrder(root->_right);
		putchar(root->_data);
	}
}

void BinaryTreeDestory(BTNode** root)
{
	if (*root)
	{
		BinaryTreeDestory(&(*root)->_left);
		BinaryTreeDestory(&(*root)->_right);
		free(*root);
        *root = NULL;
	}
}

void BinaryTreeLevelOrder(BTNode* root)
{
	Queue qu;
	BTNode * cur;

	QueueInit(&qu);

	QueuePush(&qu, root);

	while (!QueueIsEmpty(&qu))
	{
		cur = QueueTop(&qu);

		putchar(cur->_data);

		if (cur->_left)
		{
			QueuePush(&qu, cur->_left);
		}

		if (cur->_right)
		{
			QueuePush(&qu, cur->_right);
		}

		QueuePop(&qu);
	}

	QueueDestory(&qu);
}

int BinaryTreeComplete(BTNode* root)
{
	Queue qu;
	BTNode * cur;
	int tag = 0;

	QueueInit(&qu);

	QueuePush(&qu, root);

	while (!QueueIsEmpty(&qu))
	{
		cur = QueueTop(&qu);

		putchar(cur->_data);

		if (cur->_right && !cur->_left)
		{
			return 0;
		}

		if (tag && (cur->_right || cur->_left))
		{
			return 0;
		}

		if (cur->_left)
		{
			QueuePush(&qu, cur->_left);
		}

		if (cur->_right)
		{
			QueuePush(&qu, cur->_right);
		}
		else
		{
			tag = 1;
		}

		QueuePop(&qu);
	}

	QueueDestory(&qu);
	return 1;
}

Binary tree exercises

965. Single value binary tree

Simple

197

related business

If each node of a binary tree has the same value, then the binary tree issingle-valued a binary tree.

Returns true only if the given tree is a single-valued binary tree; otherwise, returns false.

Example 1:

Import:[1,1,1,1,1,null,1]
Exit: true

Example 2:

Import:[2,2,2,5,2]
Output:false
Presentation
  1. The range of node numbers for a given tree is [1, 100].
  2. The value of each node is an integer, ranging from [0, 99] .
    bool isUnivalTree(struct TreeNode* root){
    if(!root)
    {
        return true;
    }
    if(root->left)
    {
        if(root->val!=root->left->val || !isUnivalTree(root->left))
        {
            return false;
        }
    }
    if(root->right)
    {
        if(root->val!=root->right->val || !isUnivalTree(root->right))
        {
            return false;
        }
    }
    return true;
    }

    100. The same tree

    Simple

    1.1K

    related business

    Give you the root nodes of two binary trees p and q , write a function to check whether the two trees are the same.

    Two trees are considered identical if they are structurally identical and have nodes with the same values.

    Example 1:

    Import:p = [1,2,3], q = [1,2,3]
    Exit: true
    

    Example 2:

  • Import:p = [1,2], q = [1,null,2]
    Output:false
    

    Example 3:

    Import:p = [1,2,1], q = [1,1,2]
    Output:false
    

    hint:

  1. The number of nodes on both trees is within the range [0, 100] 
  • -104 <= Node.val <= 104
    bool isSameTree(struct TreeNode* p, struct TreeNode* q){
    if(p==NULL&&q==NULL)
    return true;
    else if(p==NULL||q==NULL)
    {
    return false;
    }
    else if(p->val!=q->val)
    {
        return false;
    }
    else{
        return  isSameTree(p->left,q->left)&& isSameTree(p->right,q->right);
    }
    
    }

    101. Symmetric Binary Tree

    Simple

    2.6K

    related business

    Give you the root node of a binary tree root , check whether it is axially symmetrical.

    Example 1:

  1. Import:root = [1,2,2,3,4,4,3] Export:true

  2. Example 2:

    Import:root = [1,2,2,null,3,null,3]
    Output:false
    

    hint:

  3. The number of nodes in the tree is within the range [1, 1000] 
  4. -100 <= Node.val <= 100
 bool check(struct TreeNode* p,struct TreeNode* q)
 {
     if(p==NULL&&q==NULL)
     return true;
     if(p==NULL||q==NULL)
     return false;
     if(p->val==q->val)
     return check(p->left,q->right)&&check(p->right,q->left);
     else
     return false;
}
bool isSymmetric(struct TreeNode* root){
     return check(root,root);
}

Summarize

Come on, come on, smile, you are awesome today, meow~


Baozi, don’t you like it? Don't you want to comment? Don't you want to collect it?

Finally, follow me, follow me, follow me, you will see more interesting blogs! ! !

Meow meow meow, you are really important to me.

Guess you like

Origin blog.csdn.net/ormstq/article/details/133649990