数据结构——二叉树(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 ...
随机推荐
- codeforces #260 DIV 2 C题Boredom(DP)
题目地址:http://codeforces.com/contest/456/problem/C 脑残了. .DP仅仅DP到了n. . 应该DP到10w+的. . 代码例如以下: #include & ...
- CodeForces 160D - Distance in Tree 树型DP
题目给了512MB的空间....用dp[k][i]代表以k为起点...往下面走(走直的不打岔)i步能有多少方案....在更新dp[k][i]过程中同时统计答案.. Program: #include& ...
- Easyui登陆页面制作
一.登陆页面HTML <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Web ...
- struts2+hibernate环境搭建
使用的是myeclipse2014,搭建比较简单,很多jar包不用自己引入,很多初始配置文件不需要自己写.后面会介绍ssh的搭建. 首先新建web project. 1.右键项目,如图所示 这个直接f ...
- Webform之FileUpload(上传按钮控件)简单介绍及下载、上传文件时图片预览
1.FileUpload上传控件:(原文:http://www.cnblogs.com/hide0511/archive/2006/09/24/513201.html) FileUpload 控件显示 ...
- Penalty
Penalty时间限制:1000 ms | 内存限制:65535 KB难度:2描述As is known to us, the penalty is vitally important to comp ...
- System.Reflection.Emit学习
C#反射发出System.Reflection.Emit学习 分享: 1 一.System.Reflection.Emit概述 Emit,可以称为发出或者产生.与Emit相关的类基本都存在于Syste ...
- 在 Windows Media Center 中观看电视
如果计算机具备了必要的硬件,则可以在电脑上使用 Windows Media Center 观看.暂停和快退直播的电视节目及录制的电视节目. 通过 Windows Media Center 观看直播电视 ...
- 宣布发布 Windows Azure SDK 2.2,正式发布 Windows Azure Backup 和 Hyper-V 恢复管理器预览版
开发人员正逐渐转向云计算,因为它具有众多优势,包括成本.自动化和让开发人员专注于应用程序逻辑的能力.我们很高兴地宣布,继推出 Visual Studio 2013 之后,今天将发布 Windows A ...
- Qt 4.6: A Quick Start to Qt Designer
Qt 4.6: A Quick Start to Qt Designer A Quick Start to Qt Designer Using Qt Designer involves four ba ...