Whether the judgment of C + + algorithm is a balanced binary tree to find the image of binary tree

Source: Internet
Author: User
Tags diff

1: Determine whether the binary tree is balanced:

Method 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 FAL Se;return isbalanced (proot->m_pleft) &&isbalanced (proot->m_pright);} /* Above method: In seeking the depth of the left and right subtree of the node to traverse through the tree, again to determine the balance of the sub-tree time to traverse the tree structure, resulting in multiple traversal! */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+ (NLEFTD Epth > Nrightdepth? nleftdepth:nrightdepth); return true;} Else{return false;}} BOOL IsBalanced3 (btree* proot) {int depth = 0;return IsBalanced3 (proot, depth);}


2: Find the image of the binary tree

/* Image of Binary tree: Method 1: The pre-order iterates through each node, and if the traversed node has child nodes, it swaps its two child nodes. (Swap the Saozi right subtree first, and then mirror the Saozi right subtree) Method 2: If the binary tree is not empty, seek the mirror of the Saozi right subtree, and then swap the Saozi right subtree */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);p root->m_pleft = Pright;proot->m_pright = Pleft;return pRoot;}


Complete test Code:

BalanceOfBT.cpp: Defines the entry point of the console application. #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;}};/ /Two insert implementation of the fork Tree 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;} Method 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 FAL Se;return isbalanced (proot->m_pleft) &&isbalanced (proot->m_pright);} /* Above method: In seeking the depth of the left and right sub-tree of the node to traverse through the tree, again to determine the balance of the sub-tree time to traverse the tree structure, resulting in multiple traversal! */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+ (NLEFTD Epth > Nrightdepth? nleftdepth:nrightdepth); return true;} Else{return false;}} BOOL IsBalanced3 (btree* proot) {int depth = 0;return IsBalanced3 (proot, depth);} /* Image of Binary tree: Method 1: The pre-order iterates through each node, and if the traversed node has child nodes, it swaps its two child nodes. (Swap the Saozi right subtree first, and then mirror the Saozi right subtree) Method 2: If the binary tree is not empty, seek the mirror of the Saozi right subtree, and then swap the Saozi right subtree */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);p root->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<< "original tree:" <<endl; Printprev (m_proot);cout<<endl<< "mirrored tree:" <<endl; Mirror (M_proot);//mirror2 (M_proot);  Printprev (M_proot); /*btree* M_proot2 = new BTree, insert (3,m_proot2), insert (6,m_proot2), insert (1,m_proot2), insert (2,M_PROOT2); Nsert (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;} 


Whether the judgment of C + + algorithm is a balanced binary tree to find the image of binary tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.