Binary tree traversal applicationTwo

Recursive algorithm deletes all leaf nodes of binary tree

If the binary tree is empty, NULL is returned;
if the binary tree has only one node, delete
it directly; otherwise, the left and right subtrees are recursively.

void Del(BiTNode *&t)
{
	if(t==NULL) return;
	if(t->lcild==NULL&&t->rchild==NULL)
	{
		delete t;
		t=NULL;
	}
	else
	{
		Del(t->lchild);
		Del(t->rchild);
	}
}

Recursive algorithm for maximum value of each node element of binary tree

Let max be the preset maximum value, the initial value is 0, determine whether there is a larger value than it and modify it.

void MaxValue(BiTNode *t,DataType &max)
{
	if(t!=NULL)
	{
		if(t->data>max)
		{
			max=t->data;
		}
		MaxValue(t->lchild,max);
		MaxValue(t->rchild,max);
	}
}

Because in the recursive statement, the maximum value is returned by the function parameter table, so max is a reference parameter.

Recursive algorithm exchange left and right children of binary tree

First recursively swap the left and right subtrees, and finally exchange the left and right children of the root

void exchange(BiTNode *t)
{
	if(t==NULL) return;
	exchange(t->lchild);
	exchange(t->rchild);
	BiTNode *p;
	p=t->lchild;
	t->lchild=t->rchild;
	t->child=p;
}

Construct Binary Tree from Preorder Sequence and Midorder Sequence

Let pre-sequence sequence pre [s1,…, t2], mid-sequence sequence in [s2,…, t2], initially s1 = s2 = 0, t1 = t2 = n; establish root node with pre [s1], search for in The position i of [s1] = pre [s1] divides the middle sequence into two subsequences, in [s2, ... i-1] and in [i + 1, ... t2], and then recursively constructs the left and right subtrees.

void createBiTree(BiTNode *t,DataType pre[], DataType in[], int s1,int s2,int t1,int t2)
{//pre存放前序序列,in存放中序序列
    int i;
    t = (BiTNode*)malloc(sizeof(BiTNode));
    t->data = pre[s1]; //前序序列的第一个元素一定是根节点
    for(i=s2; i<t2; i++)
    {
        if(in[i]==pre[s1])
            break;
    }
    //使用递归,分别插入左子树和右子树
    createBiTree(t->lchild,pre,in,s1+1,s1+i-s2,s2,i-1);
    createBiTree(t->rchild,pre,in,s1+i-s2+1,t1,i+1,t2);
}

Construct a binary tree from a sequence sequence and a sequence sequence

Similar to constructing a binary tree from a pre-order sequence and a mid-order sequence, but the root node is post [t1], which is the last bit of the post-order sequence

void createBiTree(BiTNode *t,DataType post[], DataType in[], int s1,int s2,int t1,int t2)
{
    int i;
    t = (BiTNode*)malloc(sizeof(BiTNode));
    t->data = post[t1]; //后序序列的最后一个元素一定是根节点
    for(i=s2; i<t2; i++)
    {
        if(in[i]==post[t1])
            break;
    }
    //使用递归,分别插入左子树和右子树
    createBiTree(t->lchild,pre,in,s1+i-s2,t1-1,i+1,t2);
    createBiTree(t->rchild,pre,in,s1,s1+i-s2-1,s2,i-1);
}

Use preorder traversal to find the kth node of preorder traversal

Join the counter count, record the node number while visiting

BiTNode* Pre_Find_k(BiTNode t,int &count,int k)
{
	if(t!==NULL)
	{
		count++;
		if(count==k) return t;
		BiTNode *p;
		p=(BiTNode*)malloc(sizeof(BiTNode));
		if((p=Pre_Find_k(t->lchild,count,k))!=NULL) return p;
		else return (Pre_Find_k(t->rchild,count,k));
	}
	else return NULL;
} 
Published 28 original articles · won praise 2 · Views 3259

Guess you like

Origin blog.csdn.net/Maestro_T/article/details/84190546