非线性数据结构

二叉搜索树(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. java web分享ppt大纲 -- servlet容器简介

    今天在公司分享了java web的ppt,把ppt大纲放在这里,希望可以帮助需要的人 servlet容器简介 定义 狭义上的,servlet容器为java Web应用提供运行时环境,负责管理servl ...

  2. iOS开发笔记 基于wsdl2objc调用asp.net WebService

    1.准备 先下载待会要用到的工具 WSDL2ObjC-0.6.zip WSDL2ObjC-0.7-pre1.zip 我用的是WSDL2ObjC-0.6.zip 1.1搭建asp.net WebServ ...

  3. Undefined symbols for architecture xxx

    解决方法: "Build Settings"->"Linking"->"Other Linker Flags" add the ...

  4. Android adb 命令图解

    做了这么长时间的开发与管理,在命令上总是自见则过,往往却忽视了在其命令上的分享过程,所以现在稍微有点时间就把 其命令的相关操作来简单的扫盲一番吧,也系统通过这种方式去授之以渔而不是鱼,好了,我以图解的 ...

  5. BZOJ 2466: [中山市选2009]树( 高斯消元 )

    高斯消元解异或方程组...然后对自由元进行暴搜.树形dp应该也是可以的... ------------------------------------------------------------- ...

  6. BZOJ 1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富( dp )

    dp , dp[ i ][ j ] = max( dp[ k ][ j - 1 ] ) + G[ i ][ j ] ( i - 1 <= k <= i + 1 , dp[ k ][ j - ...

  7. iframe 自适应高度、宽度

    示例: <iframe id="zyms" frameborder="0" scrolling="yes" style="w ...

  8. php文件链接数据库基本代码

    <?php $conn=@mysql_connect("localhost","root",""); if($conn==null) ...

  9. 求1+2+...+n

    题目:求1+2+…+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 程序很简单,就看想到想不到了.悲剧,我属于后者... 算 ...

  10. Browserify: 使nodejs模块可以在浏览器下使用

    Browserify:浏览器加载Node.js模块--------------------------------------------------随着JavaScript程序逐渐模块化,在ECMA ...