1:判断是否为平衡二叉树:
//方法1: int TreeDepth(BTree* pRoot) { if (pRoot == NULL) return 0; int nLeftDepth = TreeDepth(pRoot->m_pLeft); int nRightDepth = TreeDepth(pRoot->m_pRight); return (nLeftDepth > nRightDepth)? (nLeftDepth+1):(nRightDepth+1); } bool IsBalanced(BTree* pRoot) { if (pRoot == NULL) return true; int nLeftDepth = TreeDepth(pRoot->m_pLeft); int nRightDepth = TreeDepth(pRoot->m_pRight); int diff = nRightDepth - nLeftDepth; if (diff > 1 || diff < -1) return false; return IsBalanced(pRoot->m_pLeft)&&IsBalanced(pRoot->m_pRight); } /* 上面的方法:在求该节点的左右子树的深度的时候遍历一遍树,再次判断子树的平衡性的时候又要遍历 一遍树结构,造成遍历多次! */ bool IsBalanced3(BTree* pRoot, int& depth) { if(pRoot== NULL) { depth = 0; return true; } int nLeftDepth; bool bLeft= IsBalanced3(pRoot->m_pLeft, nLeftDepth); int nRightDepth; bool bRight = IsBalanced3(pRoot->m_pRight, nRightDepth); if (bLeft && bRight && abs(nLeftDepth - nRightDepth) <=1) { depth = 1+(nLeftDepth > nRightDepth ? nLeftDepth : nRightDepth); return true; } else { return false; } } bool IsBalanced3(BTree* pRoot) { int depth = 0; return IsBalanced3(pRoot, depth); }
2:求二叉树的镜像
/* 求二叉树的镜像: 方法1: 前序遍历每个节点,如果遍历到的节点有子节点,就交换它的两个子节点。(先交换左子树和右子树,再对左子树和右子树进行镜像操作) 方法2: 如果二叉树不为空,求左子树和右子树的镜像,然后再交换左子树和右子树 */ void Mirror(BTree* &pRoot) { if(pRoot == NULL) return; if(pRoot->m_pLeft ==NULL && pRoot->m_pRight == NULL) return; BTree* pTemp = pRoot->m_pLeft; pRoot->m_pLeft = pRoot->m_pRight; pRoot->m_pRight = pTemp; if(pRoot->m_pLeft) Mirror(pRoot->m_pLeft); if(pRoot->m_pRight) Mirror(pRoot->m_pRight); } BTree* Mirror2(BTree* pRoot) { if(pRoot == NULL) return NULL; BTree* pLeft = Mirror2(pRoot->m_pLeft); BTree* pRight = Mirror2(pRoot->m_pRight); pRoot->m_pLeft = pRight; pRoot->m_pRight = pLeft; return pRoot; }
完整测试代码:
// BalanceOfBT.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> using namespace std; class BTree { public: int m_nValue; BTree* m_pLeft; BTree* m_pRight; BTree(int m):m_nValue(m) { m_pLeft = m_pRight = NULL; } }; //二叉树的插入实现 void Insert(int value, BTree* &root) { if (root == NULL) { root = new BTree(value); } else if(value < root->m_nValue) Insert(value,root->m_pLeft); else if(value > root->m_nValue) Insert(value,root->m_pRight); else ; } //方法1: int TreeDepth(BTree* pRoot) { if (pRoot == NULL) return 0; int nLeftDepth = TreeDepth(pRoot->m_pLeft); int nRightDepth = TreeDepth(pRoot->m_pRight); return (nLeftDepth > nRightDepth)? (nLeftDepth+1):(nRightDepth+1); } bool IsBalanced(BTree* pRoot) { if (pRoot == NULL) return true; int nLeftDepth = TreeDepth(pRoot->m_pLeft); int nRightDepth = TreeDepth(pRoot->m_pRight); int diff = nRightDepth - nLeftDepth; if (diff > 1 || diff < -1) return false; return IsBalanced(pRoot->m_pLeft)&&IsBalanced(pRoot->m_pRight); } /* 上面的方法:在求该节点的左右子树的深度的时候遍历一遍树,再次判断子树的平衡性的时候又要遍历 一遍树结构,造成遍历多次! */ bool IsBalanced3(BTree* pRoot, int& depth) { if(pRoot== NULL) { depth = 0; return true; } int nLeftDepth; bool bLeft= IsBalanced3(pRoot->m_pLeft, nLeftDepth); int nRightDepth; bool bRight = IsBalanced3(pRoot->m_pRight, nRightDepth); if (bLeft && bRight && abs(nLeftDepth - nRightDepth) <=1) { depth = 1+(nLeftDepth > nRightDepth ? nLeftDepth : nRightDepth); return true; } else { return false; } } bool IsBalanced3(BTree* pRoot) { int depth = 0; return IsBalanced3(pRoot, depth); } /* 求二叉树的镜像: 方法1: 前序遍历每个节点,如果遍历到的节点有子节点,就交换它的两个子节点。(先交换左子树和右子树,再对左子树和右子树进行镜像操作) 方法2: 如果二叉树不为空,求左子树和右子树的镜像,然后再交换左子树和右子树 */ void Mirror(BTree* &pRoot) { if(pRoot == NULL) return; if(pRoot->m_pLeft ==NULL && pRoot->m_pRight == NULL) return; BTree* pTemp = pRoot->m_pLeft; pRoot->m_pLeft = pRoot->m_pRight; pRoot->m_pRight = pTemp; if(pRoot->m_pLeft) Mirror(pRoot->m_pLeft); if(pRoot->m_pRight) Mirror(pRoot->m_pRight); } BTree* Mirror2(BTree* pRoot) { if(pRoot == NULL) return NULL; BTree* pLeft = Mirror2(pRoot->m_pLeft); BTree* pRight = Mirror2(pRoot->m_pRight); pRoot->m_pLeft = pRight; pRoot->m_pRight = pLeft; return pRoot; } void PrintPrev(BTree* pRoot) { if(pRoot == NULL) return; cout<<pRoot->m_nValue<<" "; PrintPrev(pRoot->m_pLeft); PrintPrev(pRoot->m_pRight); } int _tmain(int argc, _TCHAR* argv[]) { BTree* m_pRoot = new BTree(9); Insert(3,m_pRoot); Insert(6,m_pRoot); Insert(1,m_pRoot); Insert(2,m_pRoot); Insert(5,m_pRoot); Insert(8,m_pRoot); Insert(7,m_pRoot); Insert(10,m_pRoot); cout<<IsBalanced(m_pRoot)<<endl; cout<<"原来的树:"<<endl; PrintPrev(m_pRoot); cout<<endl<<"镜像的树:"<<endl; Mirror(m_pRoot); //Mirror2(m_pRoot); PrintPrev(m_pRoot); /* BTree* m_pRoot2 = new BTree(96); Insert(3,m_pRoot2); Insert(6,m_pRoot2); Insert(1,m_pRoot2); Insert(2,m_pRoot2); Insert(5,m_pRoot2); Insert(8,m_pRoot2); Insert(7,m_pRoot2); Insert(10,m_pRoot2); int depth; cout<<endl<<IsBalanced3(m_pRoot2)<<endl; */ getchar(); return 0; }
时间: 2024-12-10 13:08:30