二叉树的各种操作

  1 #include<stdio.h>
  2 #include "stdlib.h"
  3 #include<iostream>
  4 #include<stack>
  5 #include<queue>
  6 using namespace std;
  7
  8 //节点定义
  9 typedef struct biNode{
 10     char val;
 11     biNode* left;
 12     biNode* right;
 13 }biNode;
 14
 15 //创建一棵树
 16 void createBiTree(biNode* &root){
 17     char c;
 18     cin >> c;
 19     if(c == ‘#‘){
 20         root = NULL;
 21     }else{
 22        if( !(root = (biNode*)malloc(sizeof(biNode))) ) exit(OVERFLOW); //分配存储空间
 23        root->val = c;
 24        //生成根节点
 25        createBiTree(root->left);            //生成左子树
 26        createBiTree(root->right);           //生成右子树
 27     }
 28 }
 29
 30 //前序遍历(根左右)递归
 31 void preSeqOrderRec(biNode *root){
 32     if(root == NULL){
 33         return ;
 34     }
 35     printf("%c ",root->val);
 36     preSeqOrderRec(root->left);
 37     preSeqOrderRec(root->right);
 38 }
 39
 40 //前序遍历(根左右)非递归
 41 void preSeqOrderNoRec(biNode *root){
 42     if(root == NULL)
 43         return;
 44     stack<biNode*> st;
 45     while(root || !st.empty()){
 46         while(root){
 47             st.push(root);
 48             printf("%c ",root->val);
 49             root = root->left;
 50         }
 51         root = st.top();
 52         st.pop();
 53         root = root->right;
 54     }
 55 }
 56
 57 //中序遍历(左根右)递归
 58 void inSeqOrderRec(biNode *root){
 59     if(root == NULL){
 60         return ;
 61     }
 62     inSeqOrderRec(root->left);
 63     printf("%c ",root->val);
 64     inSeqOrderRec(root->right);
 65 }
 66
 67 //中序遍历(左根右)非递归
 68 void intSeqOrderNoRec(biNode *root){
 69     if(root == NULL)
 70         return;
 71     stack<biNode*> st;
 72     while(root || !st.empty()){
 73         while(root){
 74             st.push(root);
 75             root = root->left;
 76         }
 77         root = st.top();
 78         st.pop();
 79         printf("%c ",root->val);
 80         root = root->right;
 81     }
 82 }
 83
 84 //后序遍历(左右根)递归
 85 void postSeqOrderRec(biNode *root){
 86     if(root == NULL){
 87         return ;
 88     }
 89     postSeqOrderRec(root->left);
 90     postSeqOrderRec(root->right);
 91     printf("%c ",root->val);
 92 }
 93
 94 //后序遍历(左右根)非递归
 95 void postSeqOrderNoRec(biNode *root){
 96     if(root == NULL){
 97         return ;
 98     }
 99     stack<biNode*> st;
100     biNode* p;
101     p = root;
102     do
103     {
104        while (p){//左子树进栈
105         st.push(p);
106         p = p->left;
107        }
108        while (!st.empty() && st.top()->right == p){
109         p = st.top();    //栈顶结点出栈
110         st.pop();
111         printf("%c ",p->val);
112        }
113        if (!st.empty())
114         p = st.top()->right; //开始遍历右子树
115     }while(!st.empty());
116 }
117
118 //层序遍历
119 void levelSeqVisit(biNode *root){
120     if(root == NULL)
121         return ;
122     queue<biNode*> que;
123     que.push(root);
124     biNode *p;
125     while(!que.empty()){
126         p = que.front();
127         que.pop();
128         printf("%c ",p->val);
129         if(p->left)
130             que.push(p->left);
131         if(p->right)
132             que.push(p->right);
133     }
134 }
135
136 //所有节点个数
137 int getNodeCount(biNode* root){
138     if(root == NULL)
139         return 0;
140     int count = 1;
141     count += getNodeCount(root->left);
142     count += getNodeCount(root->right);
143     return count;
144 }
145
146 //得到叶子节点个数
147 int getLeafCount(biNode* root){
148     if(root == NULL)
149         return 0;
150     int count = 0;
151     if(root->left == NULL && root->right == NULL){
152         count ++;
153     }else{
154         count += getLeafCount(root->left);
155         count += getLeafCount(root->right);
156     }
157     return count;
158 }
159
160 //树的高度
161 int getDepth(biNode *root){
162     if(root == NULL)
163         return 0;
164     int leftDepth = getDepth(root->left);
165     int rightDepth = getDepth(root->right);
166     return leftDepth>rightDepth ? (leftDepth+1) : (rightDepth+1);
167 }
168
169 //某节点所在层次
170 int getLevelByNode(biNode *root,char toFind,int &count){
171
172     if(root==NULL || toFind==NULL)
173         return 0;
174     if(root->val == toFind ){
175         count++;
176         return 1;
177     }
178     if(getLevelByNode(root->left,toFind,count) || getLevelByNode(root->right,toFind,count)){
179         count++;
180         return 1;
181     }
182
183     return 0;
184 }
185
186 //是否为平衡树
187 bool isBalanceTree(biNode* root){
188     if(root == NULL)
189         return false;
190     int leftDepth  = getDepth(root->left);
191     int rightDepth = getDepth(root->right);
192     if(leftDepth-rightDepth>1 || rightDepth-leftDepth<-1)
193         return false;
194     return true;
195 }
196
197 //是否为平衡树(优化版)
198 bool isBalanceTreeOptimize(biNode* root, int* Depth){
199     if(root == NULL){
200         *Depth = 0;
201         return true;
202     }
203     int left=0,right=0;
204     if(isBalanceTreeOptimize(root->left,&left) && isBalanceTreeOptimize(root->right,&right)){
205         int diff = left - right;
206         if(diff<=1 && diff>=-1){
207             *Depth = 1 + (left>right ? left : right);
208             return true;
209         }
210     }
211     return false;
212 }
213
214 //是否为完全二叉树
215 bool isCompleteTree(biNode *root){
216     queue<biNode*> que;
217     que.push(root);
218     biNode* p;
219     bool isFirstLeaf = false;
220     while(!que.empty()){
221         p = que.front();
222         que.pop();
223         //第一个叶子节点
224         if(p->left==NULL && p->right==NULL){
225             isFirstLeaf = true;
226         }
227         bool LOrR  = p->left != NULL || p->right != NULL;
228         bool LAndR = p->left != NULL && p->right != NULL;
229         //第一个叶子节点之后的节点,都是叶子节点
230         if(isFirstLeaf && LOrR){
231             return false;
232         }
233         //第一个叶子之前的节点,都有两个孩子
234         if(!isFirstLeaf && !LAndR){
235             return false;
236         }
237         if(p->left != NULL)
238             que.push(p->left);
239         if(p->right != NULL)
240             que.push(p->right);
241     }
242     return true;
243 }
244
245 //是否满二叉树
246 bool isFullTree(biNode* root){
247     if(root==NULL){
248         return true;
249     }
250     int lDepth = getDepth(root->left);
251     int rDepth = getDepth(root->right);
252     int diff = lDepth - rDepth;
253     if(diff==0 && isFullTree(root->left) && isFullTree(root->right)){
254         return true;
255     }
256     return false;
257 }
258
259 int main(){
260      biNode* T;
261
262      //ab#c##d##
263      printf("\n创建树:\n");
264      createBiTree(T);
265
266      printf("\n前序遍历(递归):\n");
267      preSeqOrderRec(T);
268      printf("\n前序遍历(非递归):\n");
269      preSeqOrderNoRec(T);
270
271      printf("\n中序遍历(递归):\n");
272      inSeqOrderRec(T);
273      printf("\n中序遍历(非递归):\n");
274      intSeqOrderNoRec(T);
275
276      printf("\n后序遍历(递归):\n");
277      postSeqOrderRec(T);
278      printf("\n后序遍历(非递归):\n");
279      postSeqOrderNoRec(T);
280
281
282      printf("\n节点数为:\n%d",getNodeCount(T));
283      printf("\n叶子节点数为:\n%d",getLeafCount(T));
284
285      printf("\n树的高度为:\n%d",getDepth(T));
286
287      printf("\n层序遍历:\n");
288      levelSeqVisit(T);
289
290      int level = 0 ;
291      getLevelByNode(T,‘d‘,level);
292      printf("\n某个节点所在层次:\n%d",level);
293
294      printf("\n是否为平衡数:%d\n",isBalanceTree(T));
295
296      int depth = 0;
297      bool isBanlance = isBalanceTreeOptimize(T,&depth);
298      printf("\n是否为平衡树优化版:%d,且高度为:%d\n",isBanlance,depth);
299
300      printf("\n是否为完全二叉树:%d\n",isCompleteTree(T));
301
302      printf("\n是否为满二叉树:%d\n",isFullTree(T));
303
304      printf("\n");
305 }
时间: 2024-08-09 10:29:43

二叉树的各种操作的相关文章

二叉树的相关操作

#include<stdio.h> #include<malloc.h> #define MAXSIZE 20 typedef char TEelemtype; typedef struct BiTNode{ TEelemtype data; struct BiTNode *lchild,*rchild; }BiTNode,*BiTree; //队列的方式 typedef struct queueelem { BiTNode* b[MAXSIZE]; int front,rear;

二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: 1 typedef struct TreeNode{ 2 int data; 3 struct TreeNode *left; 4 struct TreeNode *right; 5 }TreeNode; 2.创建根节点: 1 TreeNode *creatRoot(){ 2 TreeNode * root =(TreeNode *)malloc(sizeof(TreeNode)); 3 if(NULL=

java实现二叉树的常见操作

本文转自:红客联盟 解释:程序调用自身的编程技巧叫做递归. 程序调用自身的编程技巧称为递归( recursion).递归做为一种算法在程序设计语言中广泛应用. 一个过程或函数在其定义或说明中有直接或间接调用自身的一种方法,它通常把一个大型复杂的问题层层转化为一个与原问题相似的规模较小的问题来求解,递归策略只需少量的程序就可描述出解题过程所需要的多次重复计算,大大地减少了程序的代码量.递归的能力在于用有限的语句来定义对象的无限集合. 递归的三个条件: 边界条件 递归前进段 递归返回段 当边界条件不

二叉树的简单操作

#define _CRT_SECURE_NO_WARNINGS #define m 100 typedef char DataType; typedef struct Node /*二叉链表的结构体*/ { DataType data; struct Node * LChild; struct Node * RChild; }BiTNode, *BiTree; #define Queue_Size 100 typedef BiTree QueueElement; typedef struct /

数据结构开发(24):二叉树中属性操作、层次遍历与典型遍历

0.目录 1.二叉树中属性操作的实现 2.二叉树结构的层次遍历 3.二叉树的典型遍历方式 4.小结 1.二叉树中属性操作的实现 二叉树的属性操作: 二叉树中结点的数目: 定义功能:count(node) 在 node 为根结点的二叉树中统计结点数目 在BTree.h中实现统计结点数目: protected: int count(BTreeNode<T>* node) const { int ret = 0; if( node != NULL ) { ret = count(node->l

数据结构开发(25):二叉树中属性操作、层次遍历与典型遍历

0.目录 1.二叉树的比较与相加 2.二叉树的线索化实现 3.二叉树的经典面试题分析 3.1 单度结点删除 3.2 中序线索化二叉树 4.小结 1.二叉树的比较与相加 二叉树的克隆操作: SharedPointer< BTree<T> > clone() const 克隆当前树的一份拷贝 返回值为堆空间中的一棵新二叉树 ( 与当前树相等 ) 二叉树的克隆: 定义功能:clone(node) 拷贝 node 为根结点的二叉树 ( 数据元素在对应位置相等 ) 在BTree.h中实现二叉

二叉树的其他操作

之前实现过二叉树的创建,非递归遍历和递归遍历.现在添加一些其他的操作,包括: 销毁一棵树 计算树的深度(高度) .计算叶子节点的个数 计算所有节点的个数 复制二叉树 具体见代码: #include <stdio.h> #include <stdlib.h> typedef struct Node {     int data;     struct Node* lchild;     struct Node* rchild; }Node; //创建树 Node* create_tr

二叉树的一系列操作

//二叉树学习过程中的问题和代码集合//按先序序列创建二叉树//树的高度//求树的结点数//求二叉树第K层的节点个数//求二叉树中叶子节点的个数//求二叉树中节点的最大距离//两结点最低公共祖先//判断二叉树是不是平衡二叉树//释放树空间 //感谢:http://blog.csdn.net/luckyxiaoqiang/article/details/7518888#topic1 #include<iostream> #include<stack> #include<queu

第六十五课 二叉树中属性操作的实现

递归功能函数: 1 int count(BTreeNode<T>* node) const 2 { 3 return (node != NULL) ? (count(node->left) + count(node->right) + 1) : 0; 4 } 功能函数如下: 1 int height(BTreeNode<T>* node) const 2 { 3 int ret = 0; 4 5 if( node != NULL ) 6 { 7 int lh = hei

有关二叉树的部分操作

struct TreeNode{ ElemtType val; TreeNode *left,*right; }; 1.判定一棵二叉树是否是完全二叉树 借助于层次遍历的算法,将所有结点入队列,包括空结点.出队遇到空结点时,查看其后是否有非空结点,若有,则不是完全二叉树. bool isComplete(TreeNode* root){ TreeNode* Q[MaxSize]; int front = -1, rear = -1; if (!root) return true; Q[++rear