The data structure can be classified into two types: linear structure and nonlinear structure, the content of this paper is about nonlinear structure: The basic definition of tree and its related algorithm. Some basic conceptual definitions of trees are available for reference: Wikipedia
ADT Model of the tree:
According to the tree definition, each node's descendants form a tree, called a subtree. Therefore, from the data type, the tree, subtree, tree nodes are equal status, can be seen as a node, using the Pass class: tree representation. As shown in the following:
Figure: Tree ADT Model
The "father-son-brother" model can be used to represent the ADT of the tree. , in addition to data items, with three references, respectively, to the father, son, brother, who points to the node.
Figure: Data structure of the "father-son-brother" model
Table: Actions implemented by the tree ADT
In the case of updates to the tree, different application issues require that the tree structure provide different methods. This difference is too large to define a generic update operation in the tree ADT. After that, we will combine various application problems, and give some specific update operation implementation.
Java interface for the tree:
PackageCom.tree;/** * Java Structure for Tree * Construct Interface Tree * @author gannyee * */ Public interface Tree { //get size of tree which recent node as parent Public int GetSize();//get height of recent node Public int getheight();//get Depth of recent node Public int getdepth();//get element of recent node PublicObjectgetelement();//set element of recent node, return former element PublicObjectsetElement(Object newelement);//get Parent of recent node PublicTreelinkedlistgetParent();//get first child of recent node PublicTreelinkedlistGetfirstchild();//get biggest sibling of recent node PublicTreelinkedlistgetnextsibling();}
Java Code: tree node Model implementation
PackageCom.tree;ImportJava.lang.reflect.Constructor; Public class treelinkedlist implements Tree{ //pointer Point to Parent PrivateTreelinkedlist parent;//element PrivateObject element;//pointer point to FirstChild PrivateTreelinkedlist FirstChild;//pointer point to NextSibling PrivateTreelinkedlist nextSibling;//constructor Public treelinkedlist(){ This(NULL,NULL,NULL,NULL); }//constructor with Parameters Public treelinkedlist(Treelinkedlist p, Object e,treelinkedlist f,treelinkedlist N) { This. parent = P; This. element = e; This. FirstChild = f; This. nextSibling = n; }//get size of tree which recent node as parent Public int GetSize(){intSize =1;//recent node also include own childrentreelinkedlist subtree = firstchild;//start with first child while(NULL! = Subtree) {size + = Subtree.getsize (); subtree = subtree.getnextsibling ();//get all Descendants}returnSize }//get height of recent node Public int getheight(){intHeight =-1;//recent node ' s (parent) Heighttreelinkedlist subtree = firstchild;//start with first child while(NULL! = Subtree) {height = Math.max (height, subtree.getheight ());//get the max heightsubtree = subtree.getnextsibling (); }returnHeight +1;//get recent node height}//get Depth of recent node Public int getdepth(){intdepth =0; Treelinkedlist p = parent;//start with parent while(P! =NULL) {depth + +; p = p.getparent ();//get all parents of every node}returnDepth }//get element of recent node,if nothing return null PublicObjectgetelement(){return This. element; }//set element of recent Node,return former element PublicObjectsetElement(Object newelement) {Object swap; Swap = This. element; This. element = newelement;return This. element; }//get Parent of recent node,if nothing return null PublicTreelinkedlistgetParent(){returnParent }//get first child of recent node,if nothing return null PublicTreelinkedlistGetfirstchild(){returnFirstChild; }//get biggest sibling of recent node,if nothing return null PublicTreelinkedlistgetnextsibling(){returnnextSibling; }}
Test code:
Package com.tree; Public classtreetest {/** * @param args * * Public Static void Main(string[] args) {//TODO auto-generated method stubTreelinkedlist A =NewTreelinkedlist (); Treelinkedlist B =NewTreelinkedlist (); Treelinkedlist C =NewTreelinkedlist (); Treelinkedlist d =NewTreelinkedlist (); Treelinkedlist e =NewTreelinkedlist (); Treelinkedlist f =NewTreelinkedlist (); Treelinkedlist g =NewTreelinkedlist (); A =NewTreelinkedlist (NULL,0DNULL); b =NewTreelinkedlist (A,1, D,c.getfirstchild ()); c =NewTreelinkedlist (A,2,NULL,NULL); D =NewTreelinkedlist (b,3, F,e.getfirstchild ()); E =NewTreelinkedlist (b,4,NULL,NULL); f =NewTreelinkedlist (D,5,NULL, G.getfirstchild ()); g =NewTreelinkedlist (D,6,NULL,NULL); System. out. println (A.getdepth ()); System. out. println (B.getdepth ()); System. out. println (C.getdepth ()); System. out. println (D.getdepth ()); System. out. println (E.getdepth ()); System. out. println (F.getdepth ()); System. out. println (A.getheight ()); System. out. println (B.getheight ()); System. out. println (C.getheight ()); System. out. println (D.getheight ()); System. out. println (E.getheight ()); System. out. println (F.getheight ()); System. out. println (G.getheight ()); }}
Test results:
012com.tree.TreeLinkedList@7c1c8c581nullnull
The following will give a tree traversal algorithm!
References: Data structures and Algorithms (Java description) Deng Junhui
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. Personal GitHub code space: Https://github.com/gannyee
Tree structure customization and basic algorithm (Java Data Structure learning note)