非线性数据结构

二叉搜索树(Binary Search Tree)

树的密度=结点数/高度

二叉树类

 #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)的更多相关文章

  1. [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 ...

  2. 数据结构-二叉树(Binary Tree)

    #include <stdio.h> #include <string.h> #include <stdlib.h> #define LIST_INIT_SIZE ...

  3. 算法与数据结构基础 - 二叉树(Binary Tree)

    二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如Leet ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] Binary Trees With Factors 带因子的二叉树

    Given an array of unique integers, each integer is strictly greater than 1. We make a binary tree us ...

  9. 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 ...

  10. 17. Merge Two Binary Trees 融合二叉树

    [抄题]: Given two binary trees and imagine that when you put one of them to cover the other, some node ...

随机推荐

  1. Cocos2d-X 动作展示《一》

    因为Cocos2d-X中的动作较多,我将全部的动作制作成了一个滚动视图.每一个滚动视图上都有动作名,单击滚动视图就能够展示对应的动作 程序效果图: 使用滚动视图实现动作切换 动作展示 程序代码: 首先 ...

  2. Python BeautifulSoup4 使用指南

    前言: 昨天把传说中的BeautifulSoup4装上了,还没有装好的童鞋,请看本人的上一篇博客: Python3 Win7安装 BeautifulSoup,依照里面简单的步骤就能够把Beautifu ...

  3. 生产环境提升rman备份速度----启动块跟踪

    生产环境提升rman备份速度----启动块跟踪 [环境] AIX(5300-08).oracle10g(10.2.0.1.0-64bit) [目标] 因为生产环境数据量较大,欲加快rman备份的速度 ...

  4. linux进程之fork 和 exec函数

    ---恢复内容开始--- fork函数 该函数是unix中派生新进程的唯一方法. #include <unistd.h> pid_t   fork(void); 返回: (调用它一次, 它 ...

  5. POJ 3268 Silver Cow Party 正反图最短路

    题目:click here 题意: 给出n个点和m条边,接着是m条边,代表从牛a到牛b需要花费c时间,现在所有牛要到牛x那里去参加聚会,并且所有牛参加聚会后还要回来,给你牛x,除了牛x之外的牛,他们都 ...

  6. Java学习之抽象类的总结

    抽象类的特点:1,方法只有声明没有实现时,该方法就是抽象方法,需要被abstract修饰,抽象方法必须定义在抽象类中,该类必须也被abstract修饰.2,抽象类不可以被实例化.为什么?因为调用抽象方 ...

  7. AngularJS的启动引导过程

    原文:http://www.angularjs.cn/A137?utm_source=ourjs.com 目录: 引导之前 自动引导启动框架 手工引导启动框架 引导第1步:创建注入器 引导第2步:创建 ...

  8. Hello China操作系统STM32移植指南(三)

    移植到STM32的源代码,可从下列链接下载: http://download.csdn.net/detail/hellochina15/7049909 包含两个包:一个是移植前的Hello China ...

  9. HDU 2393 Higher Math

    #include <cstdio> #include <string> using namespace std; void swap(int& a,int& b ...

  10. JAVA GUI学习 - JSplitPane分屏组件学习

    public class JSplitPaneKnow extends JFrame { JSplitPane jSplitPane; JPanel jPanelRed; JPanel jPanelB ...