Binary Tree image, binary tree image

Source: Internet
Author: User

Binary Tree image, binary tree image
Question: complete a function and enter a binary tree. The function outputs its image.

The binary tree node is defined as follows:

struct BinaryTreeNode{    int              m_nValue;    BinaryTreeNode*  m_pLeft;    BinaryTreeNode*  m_pRight;};

1. Recursive Implementation

2. Loop implementation

1 # include <stdio. h> 2 # include <stack> 3 4 struct BinaryTreeNode 5 {6 int m_nValue; 7 BinaryTreeNode * m_pLeft; 8 BinaryTreeNode * m_pRight; 9}; 10 11 BinaryTreeNode * CreateBinaryTreeNode (int value) 12 {13 BinaryTreeNode * pNode = new BinaryTreeNode (); 14 pNode-> m_nValue = value; 15 pNode-> m_pLeft = NULL; 16 pNode-> m_pRight = NULL; 17} 18 19 void ConnectTreeNodes (BinaryTreeNode * pParent, BinaryTree Node * pLeft, 20 BinaryTreeNode * pRight) 21 {22 if (pParent! = NULL) 23 {24 pParent-> m_pLeft = pLeft; 25 pParent-> m_pRight = pRight; 26} 27} 28 29 void PrintTreeNode (BinaryTreeNode * pNode) 30 {31 if (pNode! = NULL) 32 {33 printf ("value of this node is: % d. \ n", pNode-> m_nValue); 34 35 if (pNode-> m_pLeft! = NULL) 36 printf ("value of its left child is: % d. \ n ", pNode-> m_pLeft-> m_nValue); 37 else 38 printf (" left child is null. \ n "); 39 40 if (pNode-> m_pRight! = NULL) 41 printf ("value of its right child is: % d. \ n ", pNode-> m_pRight-> m_nValue); 42 else 43 printf (" right child is null. \ n "); 44} 45 else 46 {47 printf (" this node is null. \ n "); 48} 49 printf (" \ n "); 50} 51 52 void PrintTree (BinaryTreeNode * pRoot) 53 {54 PrintTreeNode (pRoot ); 55 56 if (pRoot! = NULL) 57 {58 if (pRoot-> m_pLeft! = NULL) 59 PrintTree (pRoot-> m_pLeft); 60 if (pRoot-> m_pRight! = NULL) 61 PrintTree (pRoot-> m_pRight); 62} 63} 64 65 void DestroyTree (BinaryTreeNode * pRoot) 66 {67 if (pRoot! = NULL) 68 {69 BinaryTreeNode * pLeft = pRoot-> m_pLeft; 70 BinaryTreeNode * pRight = pRoot-> m_pRight; 71 72 delete pRoot; 73 pRoot = NULL; 74 75 DestroyTree (pLeft); 76 DestroyTree (pRight); 77} 78} 79 80 // method 1 recursively implement 81 void inclurecursively (BinaryTreeNode * pNode) 82 {83 if (pNode = NULL) 84 return; 85 if (pNode-> m_pLeft = NULL & pNode-> m_pRight = NULL) 86 return; 87 88 BinaryTreeNode * pTemp = pNode-> m_pLeft; 89 pNode-> m_pLeft = pNode-> m_pRight; 90 pNode-> m_pRight = pTemp; 91 92 if (pNode-> m_pLeft) 93 bytes recursively (pNode-> m_pLeft); 94 if (pNode-> m_pRight) 95 bytes recursively (pNode-> m_pRight ); 96} 97 98 // 99 void MirrorIteratively (BinaryTreeNode * pRoot) 100 {101 if (pRoot = NULL) 102 return; 103 104 std :: stack <BinaryTreeNode *> stackTreeNode; 105 stackTreeNode. push (pRoot); 106 107 while (stackTreeNode. size ()> 0) 108 {109 BinaryTreeNode * pNode = stackTreeNode. top (); 110 stackTreeNode. pop (); 111 112 BinaryTreeNode * pTemp = pNode-> m_pLeft; 113 pNode-> m_pLeft = pNode-> m_pRight; 114 pNode-> m_pRight = pTemp; 115 116 if (pNode-> m_pLeft) 117 stackTreeNode. push (pNode-> m_pLeft); 118 if (pNode-> m_pRight) 119 stackTreeNode. push (pNode-> m_pRight); 120} 121} 122 123 // test the Complete Binary Tree: Except for leaf nodes, other nodes have two subnodes: 124/8125 // \ 126/6 10127 // \/\ 128/5 7 9 11129 130 int main () 131 {132 BinaryTreeNode * pNode8 = CreateBinaryTreeNode (8); 133 BinaryTreeNode * pNode6 = CreateBinaryTreeNode (6); 134 BinaryTreeNode * pNode10 = CreateBinaryTreeNode (10 ); 135 BinaryTreeNode * pNode5 = round (5); 136 BinaryTreeNode * pNode7 = round (7); 137 BinaryTreeNode * pNode9 = round (9); 138 BinaryTreeNode * pNode11 = CreateBinaryTreeNode (11 ); 139 140 ConnectTreeNodes (pNode8, pNode6, pNode10); 141 ConnectTreeNodes (pNode6, pNode5, pNode7); 142 ConnectTreeNodes (pNode10, pNode9, pNode11); 143 144 PrintTree (pNode8 ); 145 146 printf ("===== repeated recursively =====\ n"); 147 bytes recursively (pNode8); 148 PrintTree (pNode8 ); 149 150 printf ("===== MirrorIteratively =====\ n"); 151 MirrorIteratively (pNode8); 152 PrintTree (pNode8); 153 154 DestroyTree (pNode8 ); 155}

 

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.