关于树的常见操作-C++面试

  1 #include <iostream>
  2 using namespace std;
  3 
  4 //树的存储结构与设计
  5 struct BitNode
  6 {
  7     int data;
  8     BitNode* leftChild;
  9     BitNode* rightChild;
 10     BitNode()
 11     {
 12         leftChild = NULL;
 13         rightChild = NULL;
 14     }
 15     BitNode(int x) :data(x), leftChild(NULL), rightChild(NULL)
 16     {
 17 
 18     }
 19 };
 20 //树的先序遍历
 21 void PreOrder(BitNode* T)
 22 {
 23     if (T == NULL)
 24     {
 25         return;
 26     }
 27     cout << T->data;
 28     PreOrder(T->leftChild);
 29     PreOrder(T->rightChild);
 30     return;
 31 
 32 }
 33 //树的中序遍历
 34 void InOrder(BitNode* T)
 35 {
 36     if (T == NULL)
 37     {
 38         return;
 39     }
 40     InOrder(T->leftChild);
 41     cout << T->data;
 42     InOrder(T->rightChild);
 43     return;
 44 }
 45 //树的后序遍历
 46 void PostOrder(BitNode* T)
 47 {
 48     if (T == NULL)
 49     {
 50         return;
 51     }
 52     PostOrder(T->leftChild);
 53     PostOrder(T->rightChild);
 54     cout << T->data;
 55 }
 56 //求树的叶子结点个数
 57 int sum = 0;
 58 int CountLeafNum(BitNode* T)
 59 {
 60     
 61     if (T == NULL)
 62     {
 63         return 0;
 64     }
 65     if (T->leftChild == NULL && T->rightChild == NULL)
 66     {
 67         sum++;
 68     }
 69     CountLeafNum(T->leftChild);
 70     CountLeafNum(T->rightChild);
 71     return sum;
 72 }
 73 //求树的叶子结点个数-两个参数
 74 void CountLeafNum(BitNode* T, int* sum)
 75 {
 76     if (T == NULL)
 77     {
 78         return;
 79     }
 80     if (T->leftChild == NULL && T->rightChild == NULL)
 81     {
 82         *sum = *sum + 1; //等价于(*sum)++
 83     }
 84     CountLeafNum(T->leftChild, sum);
 85     CountLeafNum(T->rightChild, sum);
 86 }
 87 //求树的深度
 88 int DepthOfTree(BitNode* T)
 89 {
 90     int depthLeft = 0;
 91     int depthRight = 0;
 92     int depth = 0;
 93     if (T == NULL)
 94         return 0;
 95     depthLeft = DepthOfTree(T->leftChild);
 96     depthRight = DepthOfTree(T->rightChild);
 97     depth = 1 + ((depthLeft >depthRight)? depthLeft : depthRight);
 98     return depth;
 99 }
100 //复制二叉树--用递归的思想,必须先根据返回值,定义新的变量类型用于递归的返回值赋值
101 BitNode* Copy(BitNode* T)
102 {
103     if (T == NULL)
104     {
105         return NULL;
106     }
107     //用的前序遍历思想
108     BitNode* newTreeLeft = Copy(T->leftChild);
109     BitNode* newTreeRight = Copy(T->rightChild);
110     
111     //建立一个结点
112     BitNode* newNode = new BitNode();
113     if (newNode == NULL)
114     {
115         return NULL;
116     }
117     newNode->data = T->data;
118     newNode->leftChild = newTreeLeft;
119     newNode->rightChild = newTreeRight;
120     return newNode;
121 }
122 int main()
123 {
124     BitNode nodeA(1);
125     BitNode nodeB(2);
126     BitNode nodeC(3);
127     BitNode nodeD(4);
128     BitNode nodeE(5);
129     BitNode nodeF(6);
130 
131     nodeA.leftChild = &nodeB;
132     nodeA.rightChild = &nodeC;
133     nodeB.leftChild = &nodeD;
134     nodeB.rightChild = &nodeE;
135     nodeC.leftChild = &nodeF;
136 
137     //前序遍历二叉树
138     cout << "前序遍历二叉树 " << ":";
139     PreOrder(&nodeA);
140     cout << endl; 
141     //中序遍历二叉树
142     cout << "中序遍历二叉树 " << ":";
143     InOrder(&nodeA);
144     cout << endl;
145     //后序遍历二叉树
146     cout << "后序遍历二叉树 " << ":";
147     PostOrder(&nodeA);
148     cout << endl;
149 
150     //树的结点个数
151     int leafCount = CountLeafNum(&nodeA);
152     cout << "树的结点个数:" << leafCount << endl;
153 
154     //树的深度
155     int depth = DepthOfTree(&nodeA);
156     cout << "树的深度:" << depth << endl;
157 
158     //赋值一棵树
159     BitNode* newNode = Copy(&nodeA);
160     //后序遍历二叉树
161     cout << "后序遍历二叉树 " << ":";
162     PostOrder(newNode);
163     cout << endl;
164 
165     system("pause");
166     return 0;
167 }
在代码的世界尽情的翱翔吧!
原文地址:https://www.cnblogs.com/maleyang/p/7384364.html