Illustrates a data structure - the tree and traversing the tree

When you first learn coding, most people are with the array as the main data structure to learn.

After that, you will learn to hash table. If you are a computer professional, you definitely need elective courses a data structure. In class, you will learn such as linked lists, queues, and stack data structure. These are collectively referred to as a linear data structure, because they have start and end points logically.

When you start to learn the tree graph and data structures, you will feel it is so confusing. Because of its storage is not linear, they have their own particular way of storing data.

definition

Tree is a well-known non-linear data structure. They do not store data in a linear manner. They organize data hierarchically.

The definition of the tree

Tree (Tree) is n (n> = 0) of a finite set of nodes. n = 0, called null tree.

In any one tree is not empty:

(1) one and only one specific called the root (Root) node.

(2) when n> 1, the remaining nodes can be divided into m (m> 0) th disjoint finite sets T1, T2, ....., Tm, wherein each set is itself a tree , and called the root of the subtree (subTree).

The following figure meets the definition of tree:

Wherein A root node has two sub-trees:

 

Our hard disk file system is a classic tree structure.

"Tree" It has the following features:

    Each node ① zero or more child nodes;

    ② no parent node is called the root;

    ③ Each non-root node has only one parent node;

    ④ In addition to the root node, each child node can be divided into a plurality of disjoint sub-trees;

 

Tree (  tree) is called node (  nodecollection of entities) is. By an edge node (  edgeconnection). Each node contains a value or data (  value/date), and each junction node may or may not have a child node.

The first node in the tree is called the root node (ie  rootnode). If the root node and other connected nodes, the root node is the parent node and the root node is a child node is connected.

All nodes are connected by an edge. It is important to have a concept tree, because it is responsible for managing the relationship between the nodes.

Tree leaf node is the end, they have no child nodes. Like a real tree, we can see that the trees have roots, branches and leaves.

GLOSSARY OF TERMS 

  • The root of the tree is the top-most node

  • Edge is a connection between two nodes

  • A child node having a parent node of the node

  • It is a parent node has a child node connected to node

  • Leaf node is the node has no child nodes in the tree (tree ending in)

  • Height is the length of the tree to leaf nodes (tree obtained terminus)

  • Depth is the length of the root node to node

 

Tree nodes

Tree nodes comprises a plurality of data elements and branch points of its subtree .

Has a number of sub-tree node is referred to as node degree (Degree) .

Of the tree is the maximum point of each node within the tree.

 

 Hierarchy from the root node is defined from the start, the root of the first layer, the second layer is a child root, and so on, if a node in the i-layer, the root of the subtree which is in a first layer i + 1 .

Its parent node in the same layer as each other cousins. Clearly the lower the figure D, E, F cousins, and G, H, l, J is.

Depth (Depth) is the maximum height of the tree or hierarchical tree node. 

 

Tree height (  height) and depth (  depth)

  • Is the height of the tree to a leaf node (end of the tree) the length, i.e. the root node to the leaf node the maximum side length

  • Depth node is the root node to its length, i.e. level

 

 

Memory tree

 

 

 Parents notation

In each node, the indicator indicating a attached to its parent node to the location of the list.

 

  

 

Advantages: parent pointer field pointing to the array index, the parent node to find the time complexity is O (1), has been found up faster root
Cons: looking from top to bottom will be very slow, if the nodes are looking for a child or sibling, to traverse the entire tree

 

Children notation

 

 

Advantages: easier to find children

Disadvantages: takes up a lot of unnecessary child domain null pointer. When looking for the father node point, to traverse the entire tree.

 

An improvement: adding a node for each node of the domain, easy to control the number of pointer field

 

 Disadvantages: difficult to maintain, easy to achieve   

 

Two improvements: sequential structure and binding chain structure

First of all nodes on the array inside each node will have its own child nodes, the first child is represented by a pointer, each child's next pointer to its brothers

 

 

Baby brother representation

任意一棵树,它的结点的第一个孩子如果存在就是唯一的,它的右兄弟如果存在也是唯一的。因此,我们设置两个指针 ,分别指向该结点的第一个孩子和此结点的右兄弟

 

 

 

二叉树

二叉树的定义 

二叉树(Binary Tree)是n(n>=0)个结点的有限集合,该集合或者为空集(空二叉树),或者由一个根结点和两棵互不相交的、分别称为根结点的左子树和右子树的二叉树组成(子树也为二叉树)。

二叉树的特点

  • 每个结点最多有两棵子树,所以二叉树中不存在度大于2的结点。
  • 左子树和右子树是有顺序的,次序不能任意颠倒。
  • 即使树中某结点只有一棵子树,也要区分它是左子树还是右子树。

二叉树五种基本形态

  1、空二叉树

  2、只有一个根结点

  3、根结点只有左子树

  4、根结点只有右子树

  5、根结点既有左子树又有右子树

几种特殊的二叉树

斜树

左斜树:  右斜树:

 

 

满二叉树

 

满二叉树:

 

 

完全二叉树

完全二叉树:

 

二叉树的性质

二叉树性质1

性质1:在二叉树的第i层上至多有2i-1个结点(i>=1)

二叉树性质2

性质2:深度为k的二叉树至多有2k-1个结点(k>=1)

N=2K-1    K是层次/高度(4)   N=15

二叉树性质3

性质3:对任何一棵二叉树T,如果其终端结点数为n0,度为2的结点数为n2,则n0 = n2+1。

一棵二叉树,除了终端结点(叶子结点),就是度为1或2的结点。假设n1度为1的结点数,则数T 的结点总数n=n0+n1+n2。我们再换个角度,看一下树T的连接线数,由于根结点只有分支出去,没有分支进入,所以连接线数为结点总数减去1。也就是n-1=n1+2n2,可推导出n0+n1+n2-1 = n1+2n2,继续推导可得n0 = n2+1。

二叉树性质4

性质4:具有n个结点的完全二叉树的深度为[log2n +1] ([X]表示不大于X的最大整数)。

2K=N+1     N是结点数(15)

K=log2n+1  <  log2n+1

由性质2可知,满二叉树的结点个数为2k-1,可以推导出满二叉树的深度为k=log2(n + 1)。对于完全二叉树,它的叶子结点只会出现在最下面的两层,所以它的结点数一定少于等于同样深度的满二叉树的结点数2k-1,但是一定多于2k-1 -1。因为n是整数,所以2k-1 <= n < 2k,不等式两边取对数得到:k-1 <= log2n <k。因为k作为深度也是整数,因此 k= [log2n ]+ 1。

二叉树性质5

性质5:如果对一颗有n个结点的完全二叉树(其深度为 [ log2n+1 ] )的结点按层序编号(从第1层到第 [log2n+1] 层,每层从左到右),对任一结点 i (1<=i<=n) 有:

  1. 如果i=1,则结点i是二叉树的根,无双亲;如果 i>1,则其双亲是结点 [ i / 2 ]。    双亲结点的编号 = 两个子结点中的一个子结点  / 2

  2. 如果2i>n,则结点i无左孩子(结点i为叶子结点);否则其左孩子是结点2i
  3. 如果2i+1>n,则结点 i 无右孩子;否则其右孩子是结点2i+1

结合下图很好理解:

 

 

二叉树的存储结构

二叉树顺序存储结构

一般二叉树:

^ 代表不存在的结点。

 

二叉链表

链表每个结点包含一个数据域和两个指针域:

mark

其中data是数据域,lchild和rchild都是指针域,分别指向左孩子和右孩子。

mark

 

二叉树的遍历

深度优先搜索(Depth-First Search,DFS)

DFS 在回溯和搜索其他路径之前找到一条到叶节点的路径。让我们看看这种类型的遍历的示例。

输出结果为: 1–2–3–4–5–6–7

为什么?

让我们分解一下:

  1. 从根结点(1)开始。输出

  2. 进入左结点(2)。输出

  3. 然后进入左孩子(3)。输出

  4. 回溯,并进入右孩子(4)。输出

  5. 回溯到根结点,然后进入其右孩子(5)。输出

  6. 进入左孩子(6)。输出

  7. 回溯,并进入右孩子(7)。输出

  8. 完成

当我们深入到叶结点时回溯,这就被称为 DFS 算法。

既然我们对这种遍历算法已经熟悉了,我们将讨论下 DFS 的类型:前序、中序和后序。

前序遍历

这和我们在上述示例中的作法基本类似。

  1. 输出节点的值

  2. 进入其左结点并输出。当且仅当它拥有左结点。

  3. 进入右结点并输出之。当且仅当它拥有右结点

 代码实现 -- 迭代实现

/**
 * 前序遍历--迭代
 */
public void preOrder(TreeNode node) {
    if (node == null) {
        return;
    } else {
        System.out.println("preOrder data:" + node.getData());
        preOrder(node.leftChild);
        preOrder(node.rigthChild);
    }
}

 

前序遍历 - -栈实现

/**
 * 前序遍历--栈
 *
 * @param node
 */
public void nonRecOrder(TreeNode node) {
    if (root == null) {
        return;
    }
    Stack<TreeNode> stack = new Stack<>();
    stack.push(node);
    while (!stack.isEmpty()) {
        //出栈和进栈
        TreeNode n = stack.pop();//弹出根节点
        //压入子结点
        System.out.println("nonRecOrder data: " + n.getData()); //避免叶子结点为空,出现空指针异常 if (n.rigthChild != null) { stack.push(n.rigthChild); } if (n.leftChild != null) { stack.push(n.leftChild); } } }

 

 

中序遍历

 

 

示例中此树的中序算法的结果是3–2–4–1–6–5–7。

左结点优先,之后是中间,最后是右结点。

 代码实现:

/**
 * 中序遍历--迭代
 */
public void midOrder(TreeNode node) {
    if (node == null) {
        return;
    } else {
        midOrder(node.leftChild);
        System.out.println("midOrder data:" + node.getData());
        midOrder(node.rigthChild);
    }
}

 

 

后序遍历

以此树为例的后序算法的结果为 3–4–2–6–7–5–1 。

左结点优先,之后是右结点,根结点的最后。

 代码实现:

/**
     * 后序遍历--迭代
     */
    public void

    postOrder(TreeNode node) {
        if (node == null) {
            return;
        } else {
            postOrder(node.leftChild);
            postOrder(node.rigthChild);
            System.out.println("postOrder data:" + node.getData());
        }
    }

 

 

自创遍历小技巧(附链接)

先根遍历法(超级简单小技巧)

 

 

三角形遍历法

 

 

 结果: G D  I H B A E J C F

 

例子:

上图二叉树遍历结果

    前序遍历:ABCDEFGHK

    中序遍历:BDCAEHGKF

    后序遍历:DCBHKGFEA

 

Guess you like

Origin www.cnblogs.com/xiaozhongfeixiang/p/11593844.html