zoukankan      html  css  js  c++  java
  • 在二元树中找出和为某一值的所有路径

        具体实现代码如下:

    BSTree.h具体内容:

    #ifndef _BSTREE_H_
    #define _BSTREE_H_
    typedef struct _tagBSTreeNode
    {
    	int m_nVal;
    	_tagBSTreeNode * m_pLeft;
    	_tagBSTreeNode * m_pRight;
    }BSTreeNode;
    extern BSTreeNode * pHead;
    //追加树结点
    int AppendNode(BSTreeNode **, int nNum);
    //打印树结点
    void PrintTree(BSTreeNode ** ppTree);
    //释放树
    void Clear(BSTreeNode ** ppTree);
    //转换为有序双链表
    void InOrderTree(BSTreeNode ** ppTree);
    //销毁链表
    void ClearList(BSTreeNode * pHead);
    //打印链表
    void PrintList(BSTreeNode * pHead);
    //打印路径
    void PrintPath(BSTreeNode * pHead, int nSum);
    //打印和路径
    void PrintSumPath(BSTreeNode ** ppTree, int nLayer, int nPathSum);
    #endif

    BSTree.cpp的内容:

    #include <stdlib.h>
    #include <stdio.h>
    #include "BSTree.h"
    
    //追加树结点
    int AppendNode(BSTreeNode ** ppTree, int nNum)
    {
    	BSTreeNode * pNewNode = NULL;
    	if (!ppTree)
    		return 0;
    	if (*ppTree == NULL)
    	{
    		pNewNode = (BSTreeNode *)malloc(sizeof(BSTreeNode));
    		if (!pNewNode)
    			return 0;
    		pNewNode->m_nVal = nNum;
    		pNewNode->m_pLeft = NULL;
    		pNewNode->m_pRight = NULL;
    		*ppTree = pNewNode;
    		return 1;
    	}
    	if ((*ppTree)->m_nVal > nNum)
    	{
    		AppendNode(&((*ppTree)->m_pLeft), nNum);
    	}
    	else if ((*ppTree)->m_nVal < nNum)
    	{
    		AppendNode(&((*ppTree)->m_pRight), nNum);
    	}
    	else
    	{
    		printf("树结点已经存在.
    ");
    		return 0;
    	}
    	return 1;
    }
    //打印树结点
    void PrintTree(BSTreeNode ** ppTree)
    {
    	if (!ppTree)
    		return;
    	if (*ppTree == NULL)
    	{
    		return;
    	}
    	if ((*ppTree)->m_pLeft)
    		PrintTree(&((*ppTree)->m_pLeft));
    	if (*ppTree)
    		printf("%d", (*ppTree)->m_nVal);
    	if ((*ppTree)->m_pRight)
    		PrintTree(&((*ppTree)->m_pRight));
    }
    
    //释放树
    void Clear(BSTreeNode ** ppTree)
    {
    	BSTreeNode * pLeftTree = NULL;
    	BSTreeNode * pRightTree = NULL;
    	if (!ppTree)
    		return;
    	if (*ppTree == NULL)
    		return;
    	if ((*ppTree)->m_pLeft)
    	{
    		pLeftTree = (*ppTree)->m_pLeft;
    		Clear(&pLeftTree);
    	}
    	if ((*ppTree)->m_pRight)
    	{
    		pRightTree = (*ppTree)->m_pRight;
    		Clear(&pRightTree);
    	}
    	if (*ppTree)
    	{
    		free(*ppTree);
    		*ppTree = NULL;
    	}
    	return;
    }
    
    //以左孩子为前驱
    //以右孩子为后继
    BSTreeNode * pHead = NULL;
    
    void AppendNode2List(BSTreeNode * pInput)
    {
    	BSTreeNode * pCurNode = NULL;
    	if (pInput == NULL)
    		return;
    	if (!pHead)
    	{
    		pHead = pInput;
    		pInput->m_pLeft = NULL;
    		pInput->m_pRight = NULL;
    		return;
    	}
    	pCurNode = pHead;
    	while (pCurNode->m_pRight)
    	{
    		pCurNode = pCurNode->m_pRight;
    	}
    	pCurNode->m_pRight = pInput;
    	pInput->m_pLeft = pCurNode;
    	pInput->m_pRight = NULL;
    	return ;
    }
    
    //打印树结点
    void InOrderTree(BSTreeNode ** ppTree)
    {
    	if (!ppTree)
    		return;
    	if (*ppTree == NULL)
    	{
    		return;
    	}
    	if ((*ppTree)->m_pLeft)
    		InOrderTree(&((*ppTree)->m_pLeft));
    	if (*ppTree)
    		AppendNode2List(*ppTree);
    	if ((*ppTree)->m_pRight)
    		InOrderTree(&((*ppTree)->m_pRight));
    }
    
    void PrintList(BSTreeNode * pHead)
    {
    	BSTreeNode * pCurNode = NULL;
    	if (!pHead)
    		return;
    	pCurNode = pHead;
    	printf("
    链表数据:
    ");
    	while (pCurNode)
    	{
    		printf("%d	", pCurNode->m_nVal);
    		pCurNode = pCurNode->m_pRight;
    	}
    	return;
    }
    
    void ClearList(BSTreeNode * pHead)
    {
    	BSTreeNode * pCurNode = NULL;
    	if (!pHead)
    		return;
    	while (pHead)
    	{
    		pCurNode = pHead->m_pRight;
    		free(pHead);
    		pHead = pCurNode;
    	}
    	return;
    }
    
    //打印和路径
    void PrintSumPath(BSTreeNode ** ppTree, int nLayer, int nPathSum)
    {
    	static int Sum[256] = { 0 };
    	int nSum = 0;
    	int i = 0;
    	if (!ppTree)
    		return;
    	if (*ppTree == NULL)
    	{
    		return;
    	}
    	if (*ppTree)
    	{
    		Sum[nLayer] = (*ppTree)->m_nVal;
    	}	
    	if ((*ppTree)->m_pLeft)
    		PrintSumPath(&((*ppTree)->m_pLeft), nLayer + 1, nPathSum);
    	if ((*ppTree)->m_pRight)
    		PrintSumPath(&((*ppTree)->m_pRight), nLayer + 1, nPathSum);
    	if (((*ppTree)->m_pLeft==NULL) && ((*ppTree)->m_pRight == NULL))
    	{
    		for (i = 0; i <= nLayer; i++)
    		{
    			nSum += Sum[i];			
    		}
    		if (nSum == nPathSum)
    		{
    			printf("路径:
    ");
    			for (i = 0; i <= nLayer; i++)
    			{
    				printf("%d	", Sum[i]);
    			}
    			printf("
    ");
    		}
    	}
    	return;
    }
    main.cpp的内容:

    #define _CRTDBG_MAP_ALLOC
    #include <stdlib.h>
    #include <stdio.h>
    #include <crtdbg.h>
    #include "BSTree.h"
    
    void main()
    {
    	BSTreeNode * pTree = NULL;
    	if (AppendNode(&pTree, 10) == NULL)
    	{
    		printf("追加失败.
    ");
    		return;
    	}
    	if (AppendNode(&pTree, 5) == NULL)
    	{
    		printf("追加失败.
    ");
    		return;
    	}
    	if (AppendNode(&pTree, 12) == NULL)
    	{
    		printf("追加失败.
    ");
    		return;
    	}
    	if (AppendNode(&pTree, 4) == NULL)
    	{
    		printf("追加失败.
    ");
    		return;
    	}
    	if (AppendNode(&pTree, 7) == NULL)
    	{
    		printf("追加失败.
    ");
    		return;
    	}
    	PrintTree(&pTree);
    	PrintSumPath(&pTree, 0,22); //找出和为22的值
    	Clear(&pTree);
    	_CrtDumpMemoryLeaks();
    	system("pause");
    	return;
    }
    运行效果如图1所示:

    图1 运行效果


  • 相关阅读:
    如何在Power BI Desktop中呈现D3.js自定义图表
    在Power BI中动态嵌入网页
    Querying SQL Server Agent Job Information
    shell 切换当前路径到脚本所在路径
    洛谷 P1220 关路灯
    P7077 函数调用(CSP-S2020 T3)
    P7075 儒略日(2020CSP-S T1)
    2020CSP-S 复赛总结
    洛谷 P1886 滑动窗口 /【模板】单调队列
    洛谷P5656 【模板】二元一次不定方程(exgcd)
  • 原文地址:https://www.cnblogs.com/new0801/p/6176936.html
Copyright © 2011-2022 走看看