Binary Tree java Implementation

Source: Internet
Author: User

Binary Tree definition:

A binary tree (BinaryTree) is a finite set of n (n ≥ 0) nodes. It is also an empty set (n = 0 ), it may also be composed of a root node and a binary tree of two left and right sub-trees separated from each other.
This definition is recursive. The left and right subtree are also binary trees, so the subtree can also be empty trees. Shows five basic binary trees.

 

 

Binary tree traversal

Traversing a binary tree refers to accessing each node in a binary tree in a certain order. The so-called Access Node refers to the abbreviation of various operations on the node. For example, you can query the content of a node data field, output its value, locate the node location, or perform other operations on the node. The process of traversing a binary tree is essentially a process of linear arrangement of the nodes of the binary tree. Assume that the operation to access a node when traversing a binary tree is to output the value of the data field of the node, then the result of traversal is a linear sequence.

(1) Access Node itself (N ),
(2) traverse the left subtree of the node (L ),
(3) traverse the right subtree (R) of the node ).
The preceding three operations have six execution orders:
NLR, LNR, LRN, NRL, RNL, and RLN.


Because the accessed Node must be the root of a subtree, N (Node), L (Left subtlee), and R (Right subtree) it can also be interpreted as the root, the left subtree of the root, and the right subtree of the root. NLR, LNR, and LRN are also called root traversal, root traversal, and root traversal respectively.

Java Implementation of Binary Trees

(Stack. size ()> 0) {node = stack. pop (); node = node. rightChild ;}}// non-recursive public void nonRecInOrder (TreeNode p) {Stack <TreeNode> stack = new Stack <BinaryTree. treeNode> (); TreeNode node = p; while (node! = Null | stack. size ()> 0) {// The left subtree while (node! = Null) {stack. push (node); node = node. leftChild;} // the stack is not empty if (stack. size ()> 0) {node = stack. pop (); visted (node); node = node. rightChild ;}}// non-Recursive Implementation of post-order traversal public void noRecPostOrder (TreeNode p) {Stack <TreeNode> stack = new Stack <BinaryTree. treeNode> (); TreeNode node = p; while (p! = Null) {// The left subtree goes into the stack for (; p. leftChild! = Null; p = p. leftChild) {stack. push (p);} // No right subtree at the current node or the right subtree has output while (p! = Null & (p. rightChild = null | p. rightChild = node) {visted (p); // record the previous output node = p; if (stack. empty () return; p = stack. pop ();} // process the right subtree stack. push (p); p = p. rightChild ;}} public void visted (TreeNode subTree) {subTree. isVisted = true; System. out. println ("key:" + subTree. key + "-- name:" + subTree. data);}/*** node data structure of the binary tree * @ author WWX */private class TreeNode {private int key = 0; private String data = null; private boolean isVisted = false; private TreeNode leftChild = null; private TreeNode rightChild = null; public TreeNode () {}/*** @ param key sequence code * @ param data field */public TreeNode (int key, String data) {this. key = key; this. data = data; this. leftChild = null; this. rightChild = null ;}// test public static void main (String [] args) {BinaryTree bt = new BinaryTree (); bt. createBinTree (bt. root); System. out. println ("the size of the tree is" + bt. size (); System. out. println ("the height of the tree is" + bt. height (); System. out. println ("******** (preorder traversal) [ABDECF] traversal *****************"); bt. preOrder (bt. root); System. out. println ("******** (Central traversal) [DBEACF] traversal *****************"); bt. inOrder (bt. root); System. out. println ("******** (post-order traversal) [DEBFCA] traversal *****************"); bt. postOrder (bt. root); System. out. println ("***** non-Recursive Implementation ***** (pre-order traversal) [ABDECF] traversing ******************* "); bt. nonRecPreOrder (bt. root); System. out. println ("***** non-Recursive Implementation ***** (in-order traversal) [DBEACF] traversing ****************** "); bt. nonRecInOrder (bt. root); System. out. println ("*** non-Recursive Implementation *** (post-order traversal) [DEBFCA] traverses ****************** "); bt. noRecPostOrder (bt. root );}}

 

The size of the tree is 6
The height of the tree is 3
* ****** (Pre-order traversal) [ABDECF] traversal *****************
Key: 1 -- name: rootNode ()
Key: 2 -- name: B
Key: 4 -- name: D
Key: 5 -- name: E
Key: 3 -- name: C
Key: 6 -- name: F
* ****** (Central traversal) [DBEACF] traversal *****************
Key: 4 -- name: D
Key: 2 -- name: B
Key: 5 -- name: E
Key: 1 -- name: rootNode ()
Key: 3 -- name: C
Key: 6 -- name: F
* ****** (Post-order traversal) [DEBFCA] traversal *****************
Key: 4 -- name: D
Key: 5 -- name: E
Key: 2 -- name: B
Key: 6 -- name: F
Key: 3 -- name: C
Key: 1 -- name: rootNode ()
* ** Non-Recursive Implementation ** (pre-sequential traversal) [ABDECF] traversal *****************
Key: 1 -- name: rootNode ()
Key: 2 -- name: B
Key: 4 -- name: D
Key: 5 -- name: E
Key: 3 -- name: C
Key: 6 -- name: F
* ** Non-Recursive Implementation ** (Central traversal) [DBEACF] traversal *****************
Key: 4 -- name: D
Key: 2 -- name: B
Key: 5 -- name: E
Key: 1 -- name: rootNode ()
Key: 3 -- name: C
Key: 6 -- name: F
* ** Non-Recursive Implementation ** (post-sequential traversal) [DEBFCA] traversal *****************
Key: 4 -- name: D
Key: 5 -- name: E
Key: 2 -- name: B
Key: 6 -- name: F
Key: 3 -- name: C
Key: 1 -- name: rootNode ()

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.