非线性数据结构

二叉搜索树(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. BootStrap 智能表单系列 八 表单配置json详解

    本章属于该系列的高级部分,将介绍表单中一些列的配置 1.config列的配置: 主要用于控制布局 :config:{autoLayout:true|'1,2,2,4'} true:根据配置项最里层的数 ...

  2. Jquery简单动画的实现记录

    <div style="background:#98bf21;height:100px;width:100px;"> //从元素当前所在位置,往下消失 $(docume ...

  3. Problem C Andy's First Dictionary(set的使用)

    题目链接:Problem C 题意:输入一个文本,找出所有不同的单词,按照字典序从小到大输出,单词不区分大小写. 思路:将字母序列都存为小写,非字母的字符变成空格,然后利用stringstream实现 ...

  4. Problem A: A + B

    Problem A: A + BTime Limit: 1 Sec Memory Limit: 128 MBSubmit: 17 Solved: 10[Submit][Status][Web Boar ...

  5. 循环获取<ul>下拉列表的的值。进行对比,正确的家样式

    $(function () { //要对比的值     var mysqlanswer = $("#wds-mysqlcuranswer").val(); // topic-opt ...

  6. javascriptDOM编程艺术_学习笔记_知识点 动态创建标记

    传统技术:document.write 和 innerHTML 深入剖析DOM方法:createElement.createTextNode.appendChild 和 insertBefore   ...

  7. A Byte of Python 笔记(2)基本概念:数、字符串、转义符、变量、标识符命名、数据类型、对象

    第4章 基本概念 字面意义上的常量 如5.1.23.9.23e-3,或者 'This is a string'."It's a string!" 字符串等 常量,不能改变它的值 数 ...

  8. BootStrap学习2 typeahead

    首先看看这些 http://www.wrapcode.com/bootstrap/typeahead-json-objects/ http://stackoverflow.com/questions/ ...

  9. 读书笔记: nodejs API 参考

    >> bufferBuffer对象是全局对象Buffer支持的编码方式:ascii, utf8, base64, binarynew Buffer(size)new Buffer(arra ...

  10. Ghost.py 0.1b3 : Python Package Index

    Ghost.py 0.1b3 : Python Package Index Ghost.py 0.1b3 Download Ghost.py-0.1b3.tar.gz Webkit based web ...