Binary Tree java Implementation of Data Structure and binary tree java

Source: Internet
Author: User

Binary Tree java Implementation of Data Structure and binary tree java

A binary tree is a non-linear data structure and belongs to a tree structure. Its maximum feature is that the degree is 2, that is, each node has only one left subtree and one right subtree. The operations of a binary tree are mainly creation, first traversing, middle traversing, and then traversing. There is also hierarchical traversal. There are two ways to traverse. One is to use recursion, and the other is to convert to stack for traversal. The nature of binary tree traversal is listed to convert non-linear structures into linear sequences.

 

1 package tree; 2 3 import java. util. arrayList; 4 import java. util. list; 5 import java. util. queue; 6 7 // Add the array to binary tree 8 public class CreateTreeByArray in sequence <E> {9 public static class Node <E> {10 Node <E> left = null; // left subtree 11 Node <E> right = null; // right subtree 12 E data = null; // data field 13 14 // initialization Node 15 public Node (E data) {16 this. data = data; 17 this. left = null; 18 this. right = null; 19} 20 21 public Node () {22 23} 24} 25 private Node <E> root = null; // root Node 26 private List <Node <E> list = null; // Node List, convert the array element to Node 27 28 public Node <E> getRoot () {29 return this. root; 30} 31 32 // convert the array into a binary tree. conversion rules: assign values to the left and right children of nodes in the node list in sequence. The left child is I * 2 + 1, and the right child is I * 2 + 2 33 @ SuppressWarnings ("unchecked") 34 public void createTreeByArray (Object [] array) {35 this. list = new ArrayList <Node <E> (); 36 37 // Add the array to the Node list 38 for (int I = 0; I <array. length; I ++) {39 list. add (new Node <E> (E) array [I]); 40} 41 42 System. out. println ("header node->" + list. get (0 ). data); 43 this. root = new Node <E> (list. get (0 ). data); // root node 44 45 // The binary tree pointer is assigned a value of 46 for (int j = 0; j <(list. size ()/2); j ++) {47 try {48 // assign j * 2 + 1 49 list to the left subtree. get (j ). left = list. get (j * 2 + 1); 50 System. out. println ("Node" + list. get (j ). data + "Left subtree->" + list. get (j * 2 + 1 ). data); 51 // assign j * 2 + 2 52 list to the right subtree. get (j ). right = list. get (j * 2 + 2); 53 System. out. println ("Node" + list. get (j ). data + "right subtree->" + list. get (j * 2 + 2 ). data); 54} catch (Exception e) {55 56} 57} 58 59} 60 61 // first traverse Binary Tree 62 public void Indorder (Node <E> root) {63 if (root = null) {64 return; 65} 66 System. out. println (root. data); 67 Indorder (root. left); // recursively output the left subtree 68 Indorder (root. right); // recursively traverse the right subtree 69} 70 71 // traverse the binary tree in the middle order 72 public void inOrderTraverse (Node <E> root) {73 if (root = null) {74 return; 75} 76 inOrderTraverse (root. left); // traverse the left subtree 77 System. out. println (root. data); 78 inOrderTraverse (root. right); // Traverse right subtree 79} 80 81 // traverse 82 public void postOrderTraverse (Node <E> root) {83 if (root = null) {84 return; 85} 86 postOrderTraverse (root. left); // traverse the left child node 87 postOrderTraverse (root. right); // traverse the right subtree node 88 System. out. println (root. data); // traverse from bottom up 89} 90 91 92 public static void main (String [] args) {93 CreateTreeByArray <Integer> createTreeByArray = new CreateTreeByArray <Integer> (); 94 Object [] arrays = {new Integer (1), new Integer (2), new Integer (3), new Integer (4), 5, 6, 7, 8, 9, 10 }; 95 createTreeByArray. createTreeByArray (arrays); 96 System. out. println ("============================"); 97 createTreeByArray. postOrderTraverse (createTreeByArray. list. get (0); 98 99} 100 101}

 

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.