Binary Tree Overview
arrays, vectors, and linked lists are sequential containers that provide a means of accessing data by location. In many cases, we need to access elements by the value of the data, rather than their location. For example, with an array of int num[3]={1,2,3}, we can very quickly access the data in the array labeled 2, which means that we know the location of the data and can be accessed quickly. Sometimes we don't know where the element is, but we know how much it's worth. Let's say we have a variable that's stored in the NUM array, and we know it's a value of 2, but we don't know what the subscript is, which means it doesn't know where it is. It is hard to access this element in the array at this time, and you have to iterate through the array, and also ensure that there are no duplicate elements in the array.
The binary tree solves this shortcoming to a large extent, the binary tree is to save the element by value, and also to access the element by value. How to do it, and linked to the list, the binary tree is also composed of a node, the difference is linked to the list of nodes with pointers to form a chain, if the chain "straightened", like a line in the plane, is one-dimensional. and the binary tree from the root node began to diverge, the pointer points to the left and right two sub-nodes, like a tree in the plane spread, is two-dimensional. As follows:
The definition of a binary tree:
Binary tree is an important type of tree structure. Many practical problems abstract data structure is often the form of two-tree, even the general tree can be easily converted to two-fork tree, and the binary tree storage structure and its algorithm is relatively simple, so the binary tree is particularly important.
A binary tree (BinaryTree) is a finite set of nodes of N (n≥0), which is either an empty (n=0) or a two-fork tree consisting of a root node and two Saozi right subtrees, called this root, which are disjoint respectively.
This definition is recursive. Because the left and right subtrees are also binary trees, the subtree can also be an empty tree. Shows five different basic forms of two-fork trees.
where (a) is an empty tree, (b) is a two-fork tree with only one node, (c) is a two-fork tree with only Zuozi and the right subtree is empty, (d) is a two-fork tree with only the right subtree and the left subtree empty, and (e) A two-tree with a non-empty left and right subtree It should be noted here that the Saozi right subtree of the binary tree is strictly distinguishable and cannot be arbitrarily reversed, and the figure (c) and figure (d) are two different two-fork trees.
Traversal of a binary tree
The most important and basic operation for a binary tree is traversal.
Traversing a binary tree means accessing each node in a binary tree in a certain order. The so-called access node refers to the node for various operations of the abbreviation. For example, querying the contents of a Node data field, or outputting its value, or locating a node location, or performing other operations on a node. The process of traversing the binary tree is the process of arranging the nodes of the two-fork tree linearly. Assuming that the operation of the Access node when traversing the binary tree is the value of the output node data field, then the result of the traversal is a linear sequence.
From the recursive definition of the two-fork tree, a non-empty two-fork tree consists of three basic parts, the root node and the left and right subtree. Therefore, on any given node, three operations can be performed in a certain order:
(1) Access node itself (N),
(2) Zuozi (L) traversing the node,
(3) The right sub-tree (R) of the node is traversed.
There are six execution sequences for the above three operations:
NLR, LNR, LRN, NRL, RNL, RLN.
Attention:
The first three sequences are symmetrical with the last three order, so we only discuss the first three order of left and right.
Since the node visited must be the root of a subtree, n (Node), L (left Subtlee), and R (right subtree) can be interpreted as the tree of the root, the Zoki of the root. NLR, LNR, and LRN are also known as first-root traversal, middle-root traversal, and post-root traversal, respectively.
Java implementation of binary tree
First, create a binary tree, and then traverse the binary tree (the implementation of the traversal operation is divided into recursive implementation and non-recursive implementation), but also provides some methods such as acquiring parents node, getting left child, right child and so on.
PackageCom.zjn.tree;ImportJava.util.Stack;/** * Binary tree chain storage * * @author zjn */ Public class Tree { PrivateTreeNode root =NULL; Public Tree() {root =NewTreeNode (1,"RootNode (A)"); }/** * Binary tree node data structure * * / Private class TreeNode { Private intKey =0;PrivateString data =NULL;Private Booleanisvisted =false;PrivateTreeNode Leftchild =NULL;PrivateTreeNode Rightchild =NULL;/** * @param key * Sequence code * @param data field */ Public TreeNode(intKey, String data) { This. key = key; This. data = data; This. Leftchild =NULL; This. Rightchild =NULL; } }/** * Create a binary tree * * <pre> * * A * B C * D E F * </pre> * * @param root * @author wwx */ Public void Createbintree(TreeNode Root) {TreeNode Newnodeb =NewTreeNode (2,"B"); TreeNode Newnodec =NewTreeNode (3,"C"); TreeNode newnoded =NewTreeNode (4,"D"); TreeNode Newnodee =NewTreeNode (5,"E"); TreeNode Newnodef =NewTreeNode (6,"F"); Root.leftchild = Newnodeb; Root.rightchild = Newnodec; Root.leftChild.leftChild = newnoded; Root.leftChild.rightChild = Newnodee; Root.rightChild.rightChild = Newnodef; } Public Boolean IsEmpty() {returnRoot = =NULL; }//height of the tree Public int Height() {returnHeight (root); }//Number of nodes Public int size() {returnSize (root); }Private int Height(TreeNode subtree) {if(Subtree = =NULL)return 0;//recursive end: Empty tree height is 0 Else{inti = height (subtree.leftchild);intj = height (subtree.rightchild);return(I < J)? (j +1): (i +1); } }Private int size(TreeNode subtree) {if(Subtree = =NULL) {return 0; }Else{return 1+ Size (subtree.leftchild) + size (subtree.rightchild); } }//Return to parent node PublicTreeNodeParent(TreeNode Element) {return(Root = =NULL|| root = = Element)?NULL: Parent (root, Element); } PublicTreeNodeParent(TreeNode subtree, TreeNode Element) {if(Subtree = =NULL)return NULL;if(Subtree.leftchild = = Element | | subtree.rightchild = = Element)//Return parent node address returnsubtree; TreeNode p;//Now look in the left subtree, if not found in the left dial hand tree, go to the right subtree to find if(p = parent (subtree.leftchild, Element))! =NULL)//Recursive search in the left sub-tree returnPElse //Recursive search in the right sub-tree returnParent (subtree.rightchild, Element); } PublicTreeNodeGetleftchildnode(TreeNode Element) {return(Element! =NULL) ? Element.leftchild:NULL; } PublicTreeNodeGetrightchildnode(TreeNode Element) {return(Element! =NULL) ? Element.rightchild:NULL; } PublicTreeNodeGetroot() {returnRoot }//When a node is released, the left and right subtrees of the node have been released, //So a subsequent traversal should be used to release the storage space of the node when a node is accessed Public void Destroy(TreeNode subtree) {//delete subtree with root subtree if(Subtree! =NULL) {//Delete left sub-treeDestroy (Subtree.leftchild);//Delete right sub-treeDestroy (Subtree.rightchild);//Delete root nodesubtree =NULL; } } Public void Traverse(TreeNode subtree) {System.out.println ("key:"+ Subtree.key +"--name:"+ Subtree.data); Traverse (Subtree.leftchild); Traverse (Subtree.rightchild); }//Pre-order traversal Public void Preorder(TreeNode subtree) {if(Subtree! =NULL) {visted (subtree); Preorder (Subtree.leftchild); Preorder (Subtree.rightchild); } }//Middle sequence traversal Public void inorder(TreeNode subtree) {if(Subtree! =NULL) {inorder (subtree.leftchild); visted (subtree); Inorder (Subtree.rightchild); } }//Follow-up traversal Public void Postorder(TreeNode subtree) {if(Subtree! =NULL) {postorder (subtree.leftchild); Postorder (Subtree.rightchild); visted (subtree); } }non-recursive implementation of//pre-sequence traversal Public void Nonrecpreorder(TreeNode p) {Stack<treenode> Stack =NewStack<treenode> (); TreeNode node = p; while(Node! =NULL|| Stack.size () >0) { while(Node! =NULL) {visted (node); Stack.push (node); node = Node.leftchild; } while(Stack.size () >0) {node = Stack.pop (); node = Node.rightchild; } } }//non-recursive implementation of middle sequence traversal Public void Nonrecinorder(TreeNode p) {Stack<treenode> Stack =NewStack<tree.treenode> (); TreeNode node = p; while(Node! =NULL|| Stack.size () >0) {//presence of left sub-tree while(Node! =NULL) {Stack.push (node); node = Node.leftchild; }//stack non-empty if(Stack.size () >0) {node = Stack.pop (); visted (node); node = Node.rightchild; } } }//non-recursive implementation of post-sequential traversal Public void Norecpostorder(TreeNode p) {Stack<treenode> Stack =NewStack<tree.treenode> (); TreeNode node = p; while(P! =NULL) {//Left dial hand tree into the stack for(; P.leftchild! =NULL; p = p.leftchild) {Stack.push (P); }//Current node No right subtree or right subtree has been output while(P! =NULL&& (P.rightchild = =NULL|| P.rightchild = = node) {visted (P);//Record the last output node.node = p;if(Stack.empty ())return; p = Stack.pop (); }//Handle Right sub-treeStack.push (P); p = p.rightchild; } } Public void visted(TreeNode subtree) {subtree.isvisted =true; System.out.println ("key:"+ Subtree.key +"--name:"+ Subtree.data); }//Test Public Static void Main(string[] args) {Tree BT =NewTree (); 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 ("******* (pre-order traversal) [ABDECF] Traversal *****************"); Bt.preorder (Bt.root); System.out.println ("******* (middle order traversal) [DBEACF] Traversal *****************"); Bt.inorder (Bt.root); System.out.println ("******* (post-sequential traversal) [DEBFCA] Traversal *****************"); Bt.postorder (Bt.root); System.out.println ("* * * * Non-recursive implementation * * * (pre-order traversal) [ABDECF] Traversal *****************"); Bt.nonrecpreorder (Bt.root); System.out.println ("* * * * Non-recursive implementation * * * (middle order traversal) [DBEACF] Traversal *****************"); Bt.nonrecinorder (Bt.root); System.out.println ("* * * * Non-recursive implementation * * * (POST traversal) [DEBFCA] Traversal *****************"); Bt.norecpostorder (Bt.root); }}
Java implementation of "Data structure" binary tree