Java Basics-Binary Tree

1. Introduction to Binary Tree

Tree :
is a finite set of n (n>=0) nodes. Empty tree when n=0

The nature of the tree:

  • There is one and only one specific node called Root
  • When n>1, the remaining nodes can be divided into m (m>0) disjoint finite sets T1, T2,..., Tn, each of which is a tree itself, and is called a child of the root tree
  • When n>0, the root node is unique, there cannot be multiple root nodes, and the tree in the data structure can only have one root node
  • When m>0, there is no limit to the number of subtrees, but they must be disjoint

Example image of a normal tree:

Insert picture description here

Binary tree:
Binary tree is an ordered collection composed of n (n>=0) nodes. The collection is either empty, or consists of a root node plus two trees called the left subtree and the right subtree. Intersected binary tree composition

Features of Binary Tree:

  1. Each node has at most two subtrees, so there is no node with degree greater than 2 in the binary tree
  2. The left subtree and the right subtree are in order, and the order cannot be reversed arbitrarily
  3. Even if a node in the tree has only one subtree, it must be distinguished whether it is a left subtree or a right subtree

Binary tree nature:

  1. The number of binary tree layers>= 1, there are at most 2^(i-1) nodes on the i-th layer
  2. The number of binary tree layers>= 1, the depth is k, and there are at most 2^k-1 nodes
  3. n0=n2+1, n0 represents the number of nodes with degree 0, n2 represents the number of nodes with degree 2
  4. In a complete binary tree, the depth of a complete binary tree with n nodes is [log2n]+1, where [log2n] is rounded down
  5. If a complete binary tree with n nodes is numbered from 1 to n from top to bottom and from left to right, any node numbered i in the complete binary tree has the following characteristics:
    ① If i=1, then The node is the root of the binary tree and has no parents. Otherwise, the node numbered [i/2] is the parent node.
    ② If 2i>n, the node has no left child, otherwise, the node numbered 2i It is the left child node
    ③ If 2i+1>n, the node has no right child node, otherwise, the node numbered 2i+1 is the right child node

Related concepts:

  • Node: The foundation in the data structure is the basic unit of the complex data structure

  • Node degree: the number of subtrees owned by the node

  • Node relationship: The root node of the node subtree is the child node of the node. The corresponding node is called the parent node of the child node. In the binary tree in the following example, 8 is the parent node of 3, and 3 is the child node of 8. The child nodes of the same parent node are called sibling nodes, and 3 and 10 are sibling nodes.

  • Node level: starting from the definition of the root, the root is the first level, the children of the root are the second level, and so on
    Insert picture description here

  • The depth of the tree: the maximum number of layers of nodes in the tree is called the depth or height of the tree

Binary tree example diagram :
Insert picture description here
five forms of binary tree:
Insert picture description here

2. Full binary tree

In a binary tree. If all branch nodes have left and right subtrees, and all leaves are on the same layer, such a binary tree is called a full binary tree

Features:

  • The leaves can only appear on the bottom layer. It’s impossible to achieve balance if you appear on other levels
  • The degree of the branch node must be 2
  • In a binary tree of the same depth, a full binary tree has the most nodes and the most leaves

Example diagram of full binary tree:

Insert picture description here

3. Complete Binary Tree

For a binary tree with n nodes, it is numbered according to the level. At the same time, the left and right subtrees are numbered first from left to right. If the node numbered i and the node numbered i in the full binary tree of the same depth have exactly the same position in the binary tree , Then this binary tree is called a complete binary tree

Features:

  • Leaf nodes can only appear in the lowest and sub-lower layers
  • The lowest leaf nodes are concentrated on the left of the tree
  • If there is a leaf node on the penultimate layer, it must be in a continuous position on the right
  • If the node degree is 1, the node has only left children, that is, no right subtree
  • For a binary tree with the same number of nodes, the depth of a complete binary tree is the smallest

Note: A full binary tree must be a complete binary tree, but the reverse is not necessarily true

Example diagram of a complete binary tree:

Insert picture description here

4. Traverse method

4.1 Preorder traversal

Starting from the root node of the binary tree, the node data is output when the node is reached for the first time, and the access is in the direction of first left and right

Insert picture description here
Starting from the root node, it reaches node A for the first time, so output A
continues to visit to the left, and node B is visited for the first time, so output B
follows the same rules, output D, output H
when it reaches leaf node H, Return to D, this is the second time to reach D, so it is not output D, and then to D right subtree visit, D right subtree is not empty, then visit to I, the first time I reach I, output I
I If it is a leaf node, it returns to D, and the left and right subtrees of D have been visited, then it returns to B, and then to the right subtree of B, and reaches E for the first time, so the output E
goes to the left subtree of E, so the output J
follows the same Access rules, continue to output C, F, G

The output of the preorder traversal is: ABDHIEJCFG

4.2 Mid-order traversal

Starting from the root node of the binary tree, the node data is output when the node is reached for the second time, and the access is in the direction of first to the left and to the right

Starting from the root node, it will reach node A for the first time, without outputting A, continue to visit to the left, visit node B for the first time, without outputting B; continue to reach D, H
reach H, and the left subtree of H is empty , Then return to H, at this time visit H for the second time, so output H
H right subtree is empty, then return to D, then reach D for the second time, so output
D returns from D to B, and arrives at the second time B, so output B
continues to visit according to the same rules, output J, E, A, F, C, G

The middle order traversal output is: HDIBJEAFCG

4.3 Post-order traversal

Starting from the root node of the binary tree, the node data is output when the node is reached for the third time, and the access is in the direction of first to the left and to the right

Starting from the root node, it will reach node A for the first time, without outputting A, continue to visit to the left, visit node B for the first time, without outputting B; continue to reach D, H
reach H, and the left subtree of H is empty , Then return to H. At this time, visit H for the second time, do not output H. If the
right subtree is empty, return to H. At this time, reach H for the third time, so output
H returns from H to D, and arrives at the second time. D, do not output D and
continue to visit to I. The left and right subtrees of I are empty, so when I visit I for the third time, the output I
returns to D. At this time, D is reached for the third time, so the output D
continues to visit according to the same rules, and output J, E, B, F, G, C, A

The output of the post-order traversal is: HIDJEBFGCA

4.4 Level Traversal

Traverse the binary tree from top to bottom according to the level of the tree

The result of level traversal is: ABCDEFGHIJ

5. The storage structure of the binary tree

5.1 Array-based implementation

Use a one-dimensional array to store the nodes in the binary tree, and the storage location of the node is the subscript index of the array.
By wasting the address with index 0, the index of all the left nodes becomes 2i (i is the height of the node ), the indexes of all right nodes have become 2i +1

advantage:

  • A node of a known height can be randomly accessed through the subscript
  • Saves the space needed to store pointers to child node addresses

Disadvantages:

  • A certain amount of space is wasted when representing an incomplete binary tree
  • The time complexity of the expansion operation of the binary tree is O(n), and the data of the array must be transported

Applicable scenarios:
small amount of data storage, large amount of access, few insert and delete operations

Insert picture description here
Among them, ∧ means that there is no storage node at this position in the array. At this time, it can be found that there will be a waste of space

Insert picture description here

5.2 Implementation based on linked list

  • data: Represents the data field, used to store the corresponding data element
  • lchild: Represents the left pointer domain, used to store the left child node
  • rchild: Represents the right pointer domain, used to store the right child node

advantage:

  • Easy to expand
  • The time complexity of insertion and deletion operations is O(logn) (consistent with binary search)

Disadvantages:

  • The time complexity of accessing data is higher than that of array storage, and the time complexity is O(logn) (consistent with the binary search method)

Applicable scene:

  • Large amount of stored data, frequent insertion and deletion, fewer data read operations

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/xueguchen/article/details/108488951