Data Structures and Algorithms (Java Language) - Binary Tree

1. Definition of binary tree:

    Regarding the definition of a binary tree, because it is different from the definition of a tree, to a certain extent, it is considered that a binary tree is not a special tree, but a completely new definition, but it is similar to a tree. Combined with the relevant definitions of trees, it can be understood as follows:

1. Each node of the binary tree cannot have more than two children;

2. The binary tree is divided into left and right (part of the left and right is called a binary heap);

3. The left and right subtrees of the node are either empty or are also binary trees;

2. Implementation of binary tree:

 Because a binary tree node has at most two child nodes (children), in the declaration, the node is the information of the element plus two references to other nodes

(left and right) structure:

 
 
class BinaryNode {
    Object element; //Store node information
    BinaryNode left; //left child
    BinaryNode right; //right child
}

In particular, in the algorithm problem of the java language, only the information of the int type is stored for the node information of the binary tree, plus the construction method, then the binary tree is defined as follows:

public class BinaryNode{   
     int val;
     BinaryNodeleft;
     BinaryNoderight;
     BinaryNode(int x) {
        val = x;
     }
  }



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325493417&siteId=291194637