Two-fork Tree

Source: Internet
Author: User

Class binarytree{treenode head;class treenode{private int val;public treenode  left;public treenode right;public treenode (int val) {this.val=val;left=null;right= null;}} Public void add (int n)//construct tree or insert value {treenode node=new treenode (n); if (IsEmpty ()) Head=node; Else insert (node);} Private void insert (Treenode node)//Insert the value {treenode cur=head;while (cur!=null) {if (node.val) into the tree <=cur.val) {if (cur.left==null) cur.left=node;cur=cur.left;} ElseIf (Node.right==null) cur.right=node;cur=cur.right;}} Public void delete (int n) {}public treenode find (int n)//Find the node where n is located {treenode  Tmp=head;while (tmp!=null) {if (tmp.val>n) tmp=tmp.left;else if (tmp.val<n) Tmp=tmp.right;elsereturn  tmp;} Return null;} Public boolean isleaf (Int n)//is empty tree {treenode tmp=find (n); if (tmp!=null&&tmp!=head &&tmp.left==null&&tmp.right==null) Return true;retUrn false;} Public treenode parent (Int n)//parent node {linkenlist<treenode> l=new linkenlist< Treenode> (); Treenode cur;treenode node=find (n), L.offer (head), while (L.size () >0) {cur=l.poll (); if (cur.left==node| | Cur.right==node) return cur;if (cur.left!=null) l.offer (cur.left) if (Cur.right!=nulll.offer (cur.right)}return  null;} Public int max (treenode node)  //maximum node value {treenode tmp=node;if (tmp==null) return -1; while (Tmp!=null) tmp=tmp.right;return tmp.val;} Public int min (treenode node)  //min node value {treenode tmp=node;if (tmp==null) return -1; while (Tmp!=null) tmp=tmp.left;return tmp.val;} Public void deleteleaf (int n)//Delete leaf node {treenode node=find (n); treenode parent=parent (n) ; if (Parent.left==node) parent.left==null;elseparent.right==null;}}


Two-fork Tree

Related Article

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.