数据结构——树的概述
来源:互联网 发布:软件企业认定有效期 编辑:程序博客网 时间:2024/05/29 13:45
树:跟数组,链表,堆栈,队列不一样,这些都是线性的数据结构,而树呢是层级类的数据结构。
树的词汇表:最上层的节点叫树根节点,一个元素下层的直接元素称为它的孩子节点。直接上层节点称为父节点。例如a是f的孩子,并且f是孩子的父亲。最后没有孩子节点的元素称为叶子节点。
tree ---- j <-- root / \ f k / \ \ a h z <-- leaves
为啥要用树?
- 用树的一个原因就是我们想要存储信息,这些信息可以形成层级关系。例如计算机的文件系统:
file system ----------- / <-- root / \ ... home / \ ugrad course / / | \ ... cs101 cs112 cs113
- 树(带有顺序的,例如:BST)可以提供更好的访问与查询(比链表快但是比数组慢)。
- 树提供更好的插入删除功能(比数组快,但是比无序的链表慢)。
- 与链表类似,但是与数组不同,树没有节点数目的上限,因为节点之间是通过指针连接的
树的主要应用
- 操作层级数据
- 让检索信息变得更加容易(见树的遍历)
- 操作有序的数据列表
- 作为合成数字图像特效的工作流
- 路由器算法
- 多边决策的表格
二叉树:一个树中的元素最多有两个孩子节点,叫做二叉树。因为二叉树中每个节点只有两个孩子,所以我们一般叫它们左孩子与右孩子。
C语言表示的二叉树:一个树通过一个指向最顶部的节点的指针来表示,如果这个树为空,那么根节点的值为NULL
一个树的节点包含一下内容:
1. 数据
2. 指向左孩子的指针
3. 指向右孩子的指针
在C语言中,我们可以用结构体表示一个树的节点。下面就是一个带有整数数据的树节点的例子:
struct node { int data; struct node *left; struct node *right;};
/* Class containing left and right child of current node and key value*/class Node{ int key; Node left, right; public Node(int item) { key = item; left = right = null; }}
用C写的第一个简单树
tree ---- 1 <-- root / \ 2 3 / 4
struct node { int data; struct node *left; struct node *right;};/* newNode() allocates a new node with the given data and NULL left and right pointers. */struct node* newNode(int data){ // Allocate memory for new node struct node* node = (struct node*)malloc(sizeof(struct node)); // Assign data to this node node->data = data; // Initialize left and right children as NULL node->left = NULL; node->right = NULL; return(node);}int main(){ /*create root*/ struct node *root = newNode(1); /* following is the tree after above statement 1 / \ NULL NULL */ root->left = newNode(2); root->right = newNode(3); /* 2 and 3 become left and right children of 1 1 / \ 2 3 / \ / \ NULL NULL NULL NULL */ root->left->left = newNode(4); /* 4 becomes left child of 2 1 / \ 2 3 / \ / \ 4 NULL NULL NULL / \NULL NULL*/ getchar(); return 0;}
Java实现
/* Class containing left and right child of current node and key value*/class Node{ int key; Node left, right; public Node(int item) { key = item; left = right = null; }}// A Java program to introduce Binary Treeclass BinaryTree{ // Root of Binary Tree Node root; // Constructors BinaryTree(int key) { root = new Node(key); } BinaryTree() { root = null; } public static void main(String[] args) { BinaryTree tree = new BinaryTree(); /*create root*/ tree.root = new Node(1); /* following is the tree after above statement 1 / \ null null */ tree.root.left = new Node(2); tree.root.right = new Node(3); /* 2 and 3 become left and right children of 1 1 / \ 2 3 / \ / \ null null null null */ tree.root.left.left = new Node(4); /* 4 becomes left child of 2 1 / \ 2 3 / \ / \ 4 null null null / \ null null */ }}
总结:树是一种层级的数据结构。树主要用于维护层级数据,提供更方便的访问,插入删除功能。二叉树是树的特例,它的每个节点最多有两个孩子。
0 0
- 数据结构——树的概述
- 数据结构 — 图的概述
- 数据结构 —— 数据结构概述
- 数据结构—概述
- 数据结构学习总结——数据结构概述
- 【大话数据结构】——-数据结构概述
- [数据结构]前言——概述
- java数据结构-数据结构的概述
- Java数据结构与算法解析(四)——树的概述
- Java数据结构与算法解析(四)——树的概述
- 数据结构的概述
- 数据结构的概述
- 数据结构-图的概述
- 数据结构的概述
- 数据结构的概述
- 数据结构学习一——概述
- 【数据结构】二叉树概述
- 数据结构的基本类型概述
- 如何搭建一台深度学习服务器
- 并发程序设计模式之Future模式
- SOL第十一章上机题1-5
- Lua 条件语句
- python ddt 数据驱动测试
- 数据结构——树的概述
- Memcached 与 Redis 实现的对比(下)
- springMVC请求流程详解
- TCP Nagel算法
- 3340 数据结构实验之二叉树一:树的同构
- C# 第十一章上机练习1
- android-多渠道自动打包之一秒五包
- iOS一行代码调用手机系统震动
- 面向对象的设计思想