Binary Tree, binary tree Definition

Source: Internet
Author: User

Binary Tree, binary tree Definition
Definition:

Binary Tree: a finite set of n (n> = 0) nodes. The set is an empty set (null Binary Tree ), or a binary tree consisting of a root node and two left and right trees that do not conflict with each other.

Full Binary Tree: a K-depth binary tree. If it contains 2k-1 nodes, this binary tree is full.

Full Binary Tree: a binary tree with n nodes numbered by the full binary tree. If all the nodes in the tree are exactly the same as those numbered 1-n in the full binary tree, the tree is called a Complete Binary Tree.

 

Nature:

① The number of nodes on layer I of a binary tree is 2i-1 (I> = 1 );

② In any binary tree, if the number of leaf nodes is n0 and the number of nodes with the degree of 2 is n2, n0 = n2 + 1;

③ A k-depth binary tree can have at most 2k-1 nodes (k> = 1 );

④ The depth of the Complete Binary Tree with n nodes is log2n + 1.

⑤ If a node with a Complete Binary Tree with n nodes is numbered by layer, for any node I (1 <= I <= n), there are:

1) if I = 1, node I has no parent and is the root node.

2) If 2i> n, node I is a leaf node with no left child. If 2i <= n, node I has a left child and is numbered 2i;

3) If 2i + 1> n, node I has no right node. If 2i + 1 <= n, node I has the right node and the number is 2i + 1.

Binary Tree storage:

1. Sequential storage: An array is used to store a binary tree, which may result in a waste of space. Unless the binary tree is a complete binary tree, we can use a linked list to store the binary tree as an example.

2. Binary linked list storage: adds two references to a tree node pointing to its left and right child nodes. However, this storage method is difficult to access the parent node of a node, the parent node must be searched by traversing the binary tree. To solve this problem, we can use a triple linked list storage;

3. Triples linked list storage: adds two references to a tree node pointing to its left and right child nodes and one reference to its parent node.

1 package tree; 2 public class BinaryTree {3 public static class TreeNode <E> {4 E data; 5 TreeNode <E> parent; 6 TreeNode <E> leftNode; 7 TreeNode <E> rightNode; 8 public TreeNode () {} 9 public TreeNode (E data) {10 this. data = data; 11} 12 public TreeNode (E data, TreeNode <E> leftNode, TreeNode <E> rightNode) {13 this. data = data; 14 this. leftNode = leftNode; 15 this. rightNode = rightNode; 16} 17} 18 19 priva Te TreeNode root; 20 public <E> void setRoot (TreeNode <E> root) {21 this. root = root; 22} 23 // returns the root node 24 public <E> TreeNode <E> getRoot () {25 if (root = null | isEmpty ()) {26 throw new RuntimeException ("root is null"); 27} else {28 return root; 29} 30} 31 public BinaryTree () {32} 33 public <E> BinaryTree (TreeNode <E> root) {34 this. root = root; 35} 36 // return the parent node 37 public <E> TreeNode <E> getParen T (TreeNode <E> node) {38 if (node = null) {39 throw new RuntimeException ("the node is null"); 40} else {41 return node. parent; 42} 43} 44 // Add a subnode 45 public <E> void addNode (TreeNode <E> parent, E data, boolean isLeft) to the specified Node) {46 if (parent = null) {47 throw new RuntimeException ("parent is null, so can't add child to this parent "); 48} else if (isLeft & parent. leftNode! = Null) {49 throw new RuntimeException ("the parent already has haven a leftchild"); 50} else if (! IsLeft & parent. rightNode! = Null) {51 throw new RuntimeException ("the parent already has haven a rightchild"); 52} 53 TreeNode <E> child = new TreeNode <E> (data ); 54 if (isLeft) {55 parent. leftNode = child; 56} else {57 parent. rightNode = child; 58} 59 child. parent = parent; 60} 61 public <E> void addNode (TreeNode <E> parent, TreeNode <E> child, boolean isLeft) {62 if (parent = null) {63 throw new RuntimeException ("parent is null, so Can't add child to this parent "); 64} else if (isLeft & parent. leftNode! = Null) {65 throw new RuntimeException ("the parent already has haven a leftchild"); 66} else if (! IsLeft & parent. rightNode! = Null) {67 throw new RuntimeException ("the parent already has havena rightchild"); 68} 69 if (isLeft) {70 parent. leftNode = child; 71} else {72 parent. rightNode = child; 73} 74 child. parent = parent; 75} 76 // determine whether the binary tree is null 77 public boolean isEmpty () {78 return root = null; 79} 80 // return the left subnode 81 public of the specified node <E> TreeNode <E> getLeftChild (TreeNode <E> parent) {82 if (parent = null) {83 throw new runtime0000t Ion ("parent is null"); 84} else {85 return parent. leftNode = null? Null: parent. leftNode; 86} 87} 88 // return 89 public <E> TreeNode <E> getRightChild (TreeNode <E> parent) of the right subnode of the specified Node) {90 if (parent = null) {91 throw new RuntimeException ("parent is null"); 92} else {93 return parent. rightNode = null? Null: parent. rightNode; 94} 95} 96 // returns the depth of the Binary Tree 97 public int deep () {98 return deep (root); 99} 100 // uses recursion, if the node is empty, the depth is 0. If the node has neither left nor right children, the depth is 1, otherwise, the maximum depth + 1101 private <E> int deep (TreeNode <E> root) {102 if (root = null) {103 return 0; 104} else if (root. leftNode = null & root. rightNode = null) {105 return 1; 106} else {107 int leftDeep = deep (root. leftNode); 108 int rightDeep = deep (root. rightNode); 109 Int max = leftDeep> rightDeep? LeftDeep: rightDeep; 110 return max + 1; 111} 112} 113}View Code

 

Traverse Binary Trees

If the ordered structure is used to save the binary tree, it is very easy for the program to traverse the underlying array directly. If the linked list is used to save the binary tree, there are two traversal methods:
Deep priority Traversal
This Traversal method uses recursive calling (using the stack principle) to deliver to the stack and return to the stack.
Breadth-first Traversal
This Traversal method will access each layer of nodes layer by layer, first access the root (first layer) node, then access the second layer of nodes ...... And so on. Therefore, breadth-first traversal is also called layer-by-layer traversal.

Deep priority Traversal

Depth-first traversal is divided into first-order traversal, middle-order traversal, and post-order traversal.

Graphic depth-first traversal:


Sequential traversal:
① Access the root node
② Recursively traverse the left subtree
③ Recursively traverse the right subtree

 
1 public List <TreeNode> preIterator () {2 return preIterator (root); 3} 4 private List <TreeNode> preIterator (TreeNode root) {5 List <TreeNode> list = new ArrayList <TreeNode> (); 6 list. add (root); 7 if (root. leftNode! = Null) {8 list. addAll (preIterator (root. leftNode); 9} 10 if (root. rightNode! = Null) {11 list. addAll (preIterator (root. rightNode); 12} 13 return list; 14}View Code

 

Sequential Traversal
① Recursively traverse the left subtree
② Access the root node
③ Recursively traverse the right subtree

 
1 public List <TreeNode> midIterator () {2 return midIterator (root); 3} 4 private List <TreeNode> midIterator (TreeNode root) {5 List <TreeNode> list = new ArrayList <TreeNode> (); 6 if (root = null) {7 return null; 8} 9 if (root. leftNode! = Null) {10 list. addAll (midIterator (root. leftNode); 11} 12 list. add (root); 13 if (root. rightNode! = Null) {14 list. addAll (midIterator (root. rightNode); 15} 16 return list; 17}View Code

 

Post-order traversal① Recursively traverse the left subtree ② recursively traverse the right subtree ③ access the root node
 
1 public List <TreeNode> postIterator () {2 return postIterator (root); 3} 4 private List <TreeNode> postIterator (TreeNode root) {5 if (root = null) {6 return null; 7} 8 9 List <TreeNode> list = new ArrayList <TreeNode> (); 10 if (root. leftNode! = Null) {11 list. addAll (postIterator (root. leftNode); 12} 13 if (root. rightNode! = Null) {14 list. addAll (postIterator (root. rightNode); 15} 16 list. add (root); 17 return list; 18}View Code

 

Breadth-first Traversal
To achieve breadth-first traversal, queues with the "FIFO" feature can be used: ① create a queue (first-in-first-out) and press the root node of the book into the queue; ② pop up a node from the queue (the first node is the root node), and then press the Left and Right nodes of the node into the queue. If there are no child nodes, it indicates that the leaf node has been reached. ③ Execute it repeatedly in a loop. ② when the queue is empty, it means that all the leaf nodes (the deepest layer) have passed through the queue and the traversal is completed.
1 public List <TreeNode> breadthFirst () {2 Queue <TreeNode> queue = new ArrayDeque <TreeNode> (); 3 List <TreeNode> list = new ArrayList <TreeNode> (); 4 if (root! = Null) {5 // Add the root to the queue 6 queue. offer (root); 7} 8 while (! Queue. isEmpty () {9 // Add the "Team end" element of the queue to the 10 list in the list. add (queue. peek (); 11 // a node pops up from the queue and obtains 12 TreeNode node = queue. poll (); 13 // if the node has a left child, press the left child into the queue 14 if (node. leftNode! = Null) {15 queue. offer (node. leftNode); 16} 17 // if the node has right children, press the right child into the queue 18 if (node. rightNode! = Null) {19 queue. offer (node. rightNode); 20} 21} 22 return list; 23}
View Code

 

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.