数据结构——二叉树(Binary Trees)
非线性数据结构
树的密度=结点数/高度
二叉树类
#pragma once class stnode
{
public:
int nodeValue; // node data stnode *left, *right, *parent; // child pointers and pointer to the node's parent // constructor
stnode (const int item, stnode *lptr = NULL, stnode*rptr = NULL, stnode *pptr = NULL):
nodeValue(item), left(lptr), right(rptr), parent(pptr)
{}
}; class stree
{
public:
stree(); // constructor. initialize root to NULL and size to 0
~stree(); // destructor
bool insert(const int item);
void Output(); private:
stnode *root; // pointer to tree root
int treeSize; // number of elements in the tree
stnode *creatSTNode(const int item, stnode *lptr, stnode *rptr, stnode*pptr);
}; stnode * stree::creatSTNode (const int item, stnode *lptr, stnode *rptr, stnode *pptr)
{
stnode*newNode; // initialize the data and all pointers
newNode = new stnode (item, lptr, rptr, pptr); return newNode;
}
完全二叉树(complete tree):
所有非叶子节点有两个子结点或一个左子结点。按从左到右顺序建树。
包含n个元素的完全二叉树 h=(int)(log2(n))
遍历:
1、层次遍历
按层从左到右遍历。
//层序遍历二叉树
void stree::LevelByLevel(stnode *root)
{
std::queue<stnode*> q;//建队
q.push(root);//根节点入队
stnode *cur;
while(!q.empty())
{
cur=q.front(); //获得队列的首元素
q.pop(); //首元素出队
temp.Format("%d ",cur->nodeValue); //输出结点的值
str+=temp; if(cur->left!=NULL) //若结点的左子树不空
{
q.push(cur->left);
}
if(cur->right!=NULL)//若结点的右子树不空
{
q.push(cur->right);
}
}
}
2、中序遍历(LDR)
先访问左结点数据,直到左节点为空则访问中间(父结点)数据,再访问右子结点数据。
盗一张百度的图:
3、前序遍历(DLR)
先访问父结点数据,再访问左子结点,最后右子结点。到达即访问,根结点在遍历的第一个。
上图的前序遍历结果为:ABDGJEHCFI
4、后序遍历(LRD)
先访问左子结点数据,再访问右子结点,最后父结点。根结点在遍历的最后一个。
上图的前序遍历结果为:JGDHEBIFCA
树的递归
1、递归遍历叶子结点
void CountLeaf(tnode<T> *t,int &count)
{
if(t!=NULL)
{
if(t->left==NULL&&t->right==NULL)
count++;
CountLeaf(t->left,count);
CountLeaf(t->right,count);
}
}
2、树的高度
int depth(tnode<T> *t)
{
int depthleft,depthright,depthval;
if(t==NULL)
depthval=-;
else
{
depthleft=depth(t->left);
depthright=depth(t->right);
depthval=+(depthleft>depthright? depthleft:depthright);
}
return depthval;
}
3、删除整树
void deleteTree(tnode<T> *t)
{
if(t!=NULL)
{
deleteTree(t->left);
deleteTree(t->right);
delete t;
}
}
树形输出:
#include <iomanip> // for setw()
#include <strstream> // for format conversion
#include <string> // node data formatted as a string
#include <queue>
#include <utility> using namespace std; class tnodeShadow
{
public:
string nodeValueStr; // formatted node value
int level,column;
tnodeShadow *left, *right; tnodeShadow ()
{}
};
/*
tnodeShadow *buildShadowTree(AVLnode *t, int level, int& column);
void displayTree(int maxCharacters);
void deleteShadowTree(tnodeShadow *t);
*/
tnodeShadow *AVLtree::buildShadowTree(AVLnode *t, int level, int& column)
{
tnodeShadow *newNode = NULL;
char text[];
ostrstream ostr(text,); if (t != NULL)
{
newNode = new tnodeShadow; tnodeShadow *newLeft = buildShadowTree(t->left, level+, column);
newNode->left = newLeft; ostr << t->nodeValue << ends;
newNode->nodeValueStr = text;
newNode->level = level;
newNode->column = column; column++; tnodeShadow *newRight = buildShadowTree(t->right, level+, column);
newNode->right = newRight;
} return newNode;
} void AVLtree::displayTree(int maxCharacters)
{
string label;
int level = , column = ;
int colWidth = maxCharacters + ; int currLevel = , currCol = ; if (treeSize == )
return; tnodeShadow *shadowRoot = buildShadowTree(root, level, column); tnodeShadow *currNode; queue<tnodeShadow *> q; q.push(shadowRoot); while(!q.empty())
{
currNode = q.front();
q.pop(); if (currNode->level > currLevel)
{
currLevel = currNode->level;
currCol = ;
cout << endl;
} if(currNode->left != NULL)
q.push(currNode->left); if(currNode->right != NULL)
q.push(currNode->right); if (currNode->column > currCol)
{
cout << setw((currNode->column-currCol)*colWidth) << " ";
currCol = currNode->column;
}
cout << setw(colWidth) << currNode->nodeValueStr;
currCol++;
}
cout << endl; deleteShadowTree(shadowRoot);
} void AVLtree::deleteShadowTree(tnodeShadow *t)
{
if (t != NULL)
{
deleteShadowTree(t->left);
deleteShadowTree(t->right);
delete t;
}
}
数据结构——二叉树(Binary Trees)的更多相关文章
- [Swift]LeetCode823. 带因子的二叉树 | Binary Trees With Factors
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...
- 数据结构-二叉树(Binary Tree)
#include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...
- 算法与数据结构基础 - 二叉树(Binary Tree)
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...
- [LeetCode] Merge Two Binary Trees 合并二叉树
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- [Swift]LeetCode617. 合并二叉树 | Merge Two Binary Trees
Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...
- [Swift]LeetCode894. 所有可能的满二叉树 | All Possible Full Binary Trees
A full binary tree is a binary tree where each node has exactly 0 or 2 children. Return a list of al ...
- [Swift]LeetCode951. 翻转等价二叉树 | Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left a ...
- [LeetCode] Binary Trees With Factors 带因子的二叉树
Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...
- LeetCode 617. Merge Two Binary Trees合并二叉树 (C++)
题目: Given two binary trees and imagine that when you put one of them to cover the other, some nodes ...
- 17. Merge Two Binary Trees 融合二叉树
[抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...
随机推荐
- HDU1046:Gridland
Problem Description For years, computer scientists have been trying to find efficient solutions to d ...
- ExtJS002Window创建
Ext.onReady(function () { Ext.create('Ext.window.Window', { title: 'window', width: 400, height: 300 ...
- 开始学习Lucene
最近百度的魏则西事件闹的沸沸扬扬,突然有个想法:是否百度的中文搜索目前还没有人能挑战它的地位呢? 哈哈,想的太多了,正巧毕业设计就和搜索有关,当时只是大致了解了概念:如分词.排序.索引.爬虫等,并以此 ...
- mac的svn之cornerstone简易教程
链接地址:http://jingyan.baidu.com/article/9989c74612a55af648ecfef2.html 背景: 关于cornerstone的介绍很少: 这里介绍mac的 ...
- MicroStrategy笔试
1. coding判定二叉树是否是有序二叉树 2. 一个有序数组A(buffer足够大),和一个有序数组B,设计算法,merge两个数组后有序,不使用任何额外的内存空间 3. 100个点灯问题,初始状 ...
- mysql中varchar最长多少
4.0版本以下,varchar(20),指的是20字节,如果存放UTF8汉字时,只能存6个(每个汉字3字节) 5.0版本以上,varchar(20),指的是20字符,无论存放的是数字.字母还是UTF8 ...
- ajax Session失效如何跳转到登录页面
在Struts应用中,我们发出的请求都会经过 相应的拦截器进行相关处理,一般都会有一个用户登录拦截(Session失效拦截):一般请求的话,如果Session失效时,我们会跳到登录页面,可是如果我们采 ...
- 帝国cms7.0判断字段为空,显示为不同
在一些情况下,我们的字段会留空,但是又不希望内容模板上只显示空.比如,在后台,如添加作者为小明,则显示小明,不填作者则显示"佚名",我们可以用以下代码. <? if($nav ...
- ubutun 下webalizer 分析Apache日志
http://www.webalizer.org/ 配置Webalizer 我们可以通过命令行配置Webalizer,也可以通过配置文件进行配置.下面将重点介绍使用配置文件进行配置,该方法使用形式比 ...
- HDU Computer Transformation1041 题解
Problem Description A sequence consisting of one digit, the number 1 is initially written into a com ...