Data Structure: Binary Tree

Source: Internet
Author: User

Directory

  • What is binary tree
  • Binary Tree nature
  • Binary tree traversal
    • First-order traversal
    • Sequential Traversal
    • Post-order traversal
  • Special Binary Tree
    • Full Binary Tree
    • Full Binary Tree
  • Last

There is a common structure in Data Structure classification, that is, tree. There are many types of tree classification, including binary tree, binary search tree, red/black tree, and B + tree, however, most of them are based on the derivative structure of the binary tree, so I will learn about the binary tree today.

What is binary tree

Definition: a binary tree is a tree structure with a maximum of two Subtrees on each node. Generally, a subtree is called "Left subtree" and "right subtree". The top node is called "root ".

Logically, binary trees can be divided into five forms:

1) Empty Binary Tree

2) binary tree with only one root node

3) only the left and right subtree are supported.

4) only the left and right subtree are supported.

5) Root and left/right subtree (full Binary Tree)

Note: Binary Trees are defined recursively, that is, in theory, each node can infinitely extend the structure of a binary tree. Therefore, each node has left and right Subtrees, the above form is just a simple display.

Binary Tree nature

Property 1: The level of all nodes in a binary tree is no greater than 2.

Property 2: the maximum number of nodes on layer I of a binary tree is 2i-1 (I> = 1)

Nature 3: Binary Trees with a depth of K have at most 2k-1 nodes (k> = 1)

Property 4: the height of a Binary Tree Containing N nodes is at least (log2n) + 1

Property 5: In any binary tree, if the number of terminal nodes is N0 and the number of nodes with the degree of 2 is N2, N0 = n2 + 1

Proof of Nature 4:

(1) based on Nature 1, we know that the node degree of a binary tree cannot exceed 2. So here we assume that N0 represents the number of nodes with a degree of 0, and N1 represents the number of nodes with a degree of 1, n2 indicates the number of nodes whose degree is 2. The sum of the three types of nodes is the number of summary points, so we can get: N = N0 + N1 + N2

(2) Based on property 1, we can conclude that the total degree of the tree is degree 0, 1, and 2 plus the root node, that is, n = N0 * 0 + N1 * 1 + N2 * 1 + 1

Based on the above (1) and (2) proofs, we can conclude that N0 = n2 + 1.

Binary tree traversal

Before traversing a binary tree, we need to implement a binary tree first. According to the characteristics of the binary tree, it contains the data of the node itself, the left and right subtree, which can be expressed in Java code.

Public class node {/** a binary tree consists of three parts: data, left and right children */private int mdata; private node mleftchild; private node mrightchild; Public node (INT data, node leftchild, node rightchild) {mdata = data; mleftchild = leftchild; mrightchild = rightchild;} public int getdata () {return mdata;} public void setdata (INT data) {mdata = data ;} public node getleftchild () {return mleftchild;} public void setleftchild (node leftchild) {mleftchild = leftchild;} public node getrightchild () {return mrightchild;} public void setrightchild (node rightchild) {mrightchild = rightchild ;}}

Each node of a binary tree can be used.NodeClass representation. The following describes the Traversal method.

As mentioned above, a binary tree is a recursive structure. Each node can have a left and right subtree structure, while a binary tree traversal is also a recursive traversal process, each node can be accessed only once.

Based on the traversal structure, the traversal of a binary tree is generally divided into three types:

  • First-order traversal
  • Sequential Traversal
  • Post-order traversal
First-order traversal

Traversal method:

  • First access the root node
  • Traverse the left subtree in sequence
  • Traverse the right subtree in sequence

Code implementation:

Public void firstorder (node) {If (node = NULL) {return;} showdata (node); firstorder (node. getleftchild (); firstorder (node. getrightchild ();} // output node data public void showdata (node) {If (node = NULL) {return;} system. out. println (node. getdata ());}
Sequential Traversal

Traversal method:

  • Traverse the left subtree in the middle order
  • Access the root node
  • Traverse the right subtree in the middle order

Code implementation:

public void MediumOrder(Node node){    if (node == null){        return;    }    MediumOrder(node.getLeftChild());    showData(node);    MediumOrder(node.getRightChild());}
Post-order traversal

Traversal method:

  • Traverse the left subtree sequentially
  • Traverse the right subtree in descending order
  • Last access to the root node

Code implementation:

public void LastOrder(Node node){    if (node == null){        return;    }    LastOrder(node.getLeftChild());    LastOrder(node.getRightChild());    showData(node);}

The following binary tree is used as an example.

Sequential traversal: 1 2 4 5 7 3 6

Sequential traversal: 4 2 7 5 1 3 6

Post-order traversal: 4 7 5 2 6 3 1

After finishing the basic knowledge of Binary trees, we will introduce several extended structures of Binary Trees.

Special Binary Tree

The following describes two special Binary Trees: Full binary tree and full binary tree.

Full Binary Tree

Definition: A node with a height of K and 2 ^ K-1. That is to say, except for leaf nodes, each node has left and right subtree.

As follows:

Full Binary Tree

The full binary tree is similar to the full binary tree, but it is somewhat different and must meet the following conditions:

  • All leaf nodes appear on the K or K-1 layer, and the maximum number of nodes must be reached from 1 to the K-1 layer;
  • The k-th layer may not be full, but all nodes in the k-th layer must be concentrated on the leftmost.

Let's take a look at the differences between the two:

Last

After learning about the two special Binary Trees, let's consolidate the algorithm.

What we often see during the written examination is how do you determine that a binary tree is a Complete Binary Tree?

Idea: according to the characteristics of a Complete Binary Tree, we know that all the nodes at the last layer are on the far left. Therefore, we can solve the problem by following these ideas:In the process of sequence traversal, find the first non-full node. Full nodes refer to nodes with both left and right children. After finding the first non-full node, the remaining node should not have child nodes. If yes, the binary tree is not a Complete Binary Tree.

Based on this idea, we can use the queue to traverse the full Binary Tree in breadth. The following shows the code implementation:

Public class completetreechecker {// auxiliary space, queue private partition list <node> queue = new partition list <node> (); // the flag of the rightmost node on layer N is private Boolean leftmost = false; public Boolean processchild (node child) {If (child! = NULL) {If (! Leftmost) {queue. addlast (child);} else {return false;} else {leftmost = true;} return true;} public Boolean iscompletetree (node root) {// The empty tree is also a full binary tree if (root = NULL) return true; // first, the root node enters the queue. addlast (Root); While (! Queue. isempty () {node = queue. removefirst (); // process the left subnode if (! Processchild (node. getleftchild () return false; // process the right subnode if (! Processchild (node. getrightchild () return false;} // The breadth-first traversal is completed. The full Binary Tree return true ;}

Data Structure: Binary Tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.