二叉树宽度高度和判断是否相等
来源:互联网 发布:r230清零软件图解 编辑:程序博客网 时间:2024/06/01 21:31
1.二叉树的高度
2.二叉树的宽度
3.比较两个二叉树是否相等
数据结构的定义
先定义一个简单的二叉树,由于只是演示,所以定义得比较简单.
1 #include <stdio.h>
2
3 #define MAX(x,y) ((x)>(y)?(x):(y))
4
5 //define a binary search tree
6 typedef struct BNode
7 {
8 int val;
9 struct BNode *left, *right;
10 }BNode,*BTree;
二叉树节点的值为val,另外为了比较方便还定义了一个宏MAX.
12 // insert a node to binary tree
13 // we assume that no duplicated elements
14 void BTreeInsert(BTree *bt, int val)
15 {
16 BNode *p = *bt,*cur = *bt;//p is a parent node of cur
17 while (cur != NULL)
18 {//find the position to insert node val
19 p = cur;
20 if ( val < cur->val )
21 cur = cur->left;
22 else
23 cur = cur->right;
24 }
25 BNode *n = malloc(sizeof(BNode));
26 n->val = val;
27 n->left = n->right = NULL;
28 if (p == NULL)
29 *bt = n;// the tree is empty
30 else
31 {
32 if (val < p->val)
33 p->left = n;
34 else
35 p->right = n;
36 }
37 }//BTreeInsert
还定义了一个函数BTreeInsert用来帮助创建二叉树.
二叉树的高度
基本方法:二叉树,分别求出左右子数的高度,然后取左右子树高度的最大数值,再加上1,就是二叉树的高度.
由于该问题又被划分为两个性质一样的子问题,因此很容易导致递归.
39 //get the depth of a BST
40 int BTreeDepth(BTree bt)
41 {
42 if (bt != NULL)
43 {
44 int dep_left = BTreeDepth(bt->left);
45 int dep_right = BTreeDepth(bt->right);
46 return MAX(dep_left,dep_right)+1;
47 }
48 return 0;
49 }//BTreeDepth
二叉树的宽度
基本方法:左右子树的宽度相加,于是就得到二叉树的宽度.
66 //get the width of a BST
67 int BTreeWidth(BTree bt)
68 {
69 if (bt != NULL)
70 {
71 if ((bt->left == bt->right) && (bt->left == NULL))
72 return 1;// bt is a leaf
73 else
74 return BTreeWidth(bt->left) + BTreeWidth(bt->right);
75 }
76 else
77 return 0;
78 }//BTreeWidth
79
二叉树的比较
如果我们认为左右子树位置很重要,也就是说把左右子树交换以后,我们认为它和原来的子树不一样了,那么只需要比较一次就可以了。
51 //compare 2 binary tree, if bt1 is equal bt2
52 //return 1, or return 0
53 int BTreeCompare(BTree bt1, BTree bt2)
54 {
55 if ((bt1==bt2) && (bt1==NULL)) // both bt1 and bt2 are empty
56 return 1;
57 else if ((bt1 != NULL) && (bt2 != NULL)) // none of bt1 and bt2 is empty
58 {
59 return BTreeCompare(bt1->left, bt2->left)
60 && BTreeCompare(bt1->right, bt2->right);
61 }
62 else // one of bt1 and bt2 is empty
63 return 0;
64 }
如果我们认为左右子树位置不重要,也就是说把左右子树交换以后,我们认为它和原来的子树还是一样的,那么我们还好多加判断.把其中一个子树左右位置交换以后再比较.那么原来的程序需要有一些改动.
59-60行需要改成以下内容就可以了。
59 return (BTreeCompare(bt1->left, bt2->left) && BTreeCompare(bt1->right, bt2->right))
60 || (BTreeCompare(bt1->left, bt2->right) && BTreeCompare(bt1->right, bt2->left));
- 二叉树宽度高度和判断是否相等
- 二叉树(二) 求二叉树高度,根据先序和中序构建二叉树,判断二叉树是否是完全二叉树,判断两棵树是否相等
- 关于二叉树的宽度,高度,相等
- 关于二叉树的宽度,高度,相等
- 判断二叉树是否相等
- 判断二叉树是否相等:
- 判断二叉树是否相等
- 二叉树宽度和高度
- 判断两棵二叉树是否相等
- 判断两棵二叉树是否相等
- 判断两棵二叉树是否相等
- 判断2棵二叉树是否相等
- 【数据结构】判断两个二叉树是否相等
- LeeCode 判断两个二叉树是否相等
- 判断两棵二叉树是否相等
- 判断两颗棵二叉树是否相等
- 判断两棵二叉树是否相等
- 判断两棵二叉树是否相等
- 在Eclipse添加Android兼容包( v4、v7 appcompat )
- visual studio 2005 ASP.net 断点调试方法
- 最大的子序列和问题
- Android 获取assets绝对路径
- GDB使用介绍
- 二叉树宽度高度和判断是否相等
- UVA253 - Cube painting
- 在 Visual Studio 2012 中创建 ASP.Net Web Service
- 在Fragment中使用Loader机制介绍
- poj 2431 Expedition
- Python中*args 和**kwargs的用法
- 剑指offer 面试题6 重建二叉树
- Angular.js的使用原因
- Android动态加载oat文件总结一