Tree, binary tree

 

tree

 

First, the definition of the tree

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

        In an arbitrary non-empty tree:

  (1) one and only one specific root

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

        As shown below

       

 

2, the node

       (1) node comprises a plurality of data elements, and its subtree branch point.

  Number (2) the node has a subtree of node referred

  (3) is 0 node called a leaf node or a terminal node

  (4) of not called non-terminal node or a branch node to node 0

  (5) of the tree is a maximum degree of each node within the tree

3, the relationship between nodes

  Root node of the subtree is called the child node, the node is called the child's parents. With a child between parents called each other brother.

       Ancestor node is the node from the root to all nodes on the branch is. Conversely, any node to a root node of a subtree have become the descendants of the node.

 

 

 

Binary Tree

 

           Binary tree each node has at most two sub-trees tree structure. Subtree generally referred to as "left subtree" (left subtree), and "right subtree" (right subtree). Binary tree is often used to implement a binary search tree and a binary heap.

        Binary Tree has the following features:

1, each node contains a subtree elements and n, where 0≤n≤2.
2, the left subtree and right subtree is in order, the order can not be arbitrarily reversed. Value is less than the left subtree is the parent node, the value is greater than the right subtree of the parent node.

        Suppose we now have such a set of numbers [55122938352748], the sequence is inserted into the structure of a number of, 

                   

 

 

        This is a binary tree it! We can see, by insertion through a series of original disorder has become the number one set up an ordered structure, and this tree to meet the characteristics of the two above-mentioned binary tree!

  But if the same number is above that group, and then insert ourselves in ascending order, that is inserted in accordance with the order of [48,552,729,353,812], what will happen?

                        

 

 

 

       由于是升序插入,新插入的数据总是比已存在的结点数据都要大,所以每次都会往结点的右边插入,最终导致这棵树严重偏科!!!上图就是最坏的情况,也就是一棵树退化为一个线性链表了,这样查找效率自然就低了,完全没有发挥树的优势了呢!
为了较大发挥二叉树的查找效率,让二叉树不再偏科,保持各科平衡,所以有了平衡二叉树!

平衡二叉树

 

 平衡二叉树是一种特殊的二叉树,所以他也满足前面说到的二叉树的两个特性,同时还有一个特性:

它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

  大家也看到了前面[35 27 48 12 29 38 55]插入完成后的图,其实就已经是一颗平衡二叉树啦。

  那如果按照[12 27 29 35 38 48 55]的顺序插入一颗平衡二叉树,会怎么样呢?我们看看插入以及平衡的过程:

 

 

 

 

 

 这棵树始终满足平衡二叉树的几个特性而保持平衡!这样我们的树也不会退化为线性链表了!我们需要查找一个数的时候就能沿着树根一直往下找,这样的查找效率和二分法查找是一样的呢!

  一颗平衡二叉树能容纳多少的结点呢?这跟树的高度是有关系的,假设树的高度为h,那每一层最多容纳的结点数量为2^(n-1),整棵树最多容纳节点数为2^0+2^1+2^2+...+2^(h-1)。这样计算,100w数据树的高度大概在20左右,那也就是说从有着100w条数据的平衡二叉树中找一个数据,最坏的情况下需要20次查找。如果是内存操作,效率也是很高的!但是我们数据库中的数据基本都是放在磁盘中的,每读取一个二叉树的结点就是一次磁盘IO,这样我们找一条数据如果要经过20次磁盘的IO?那性能就成了一个很大的问题了!那我们是不是可以把这棵树压缩一下,让每一层能够容纳更多的节点呢?虽然我矮,但是我胖啊...

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/ljl150/p/11995907.html