Binary Tree (java version)

Source: Internet
Author: User

Binary Tree (java version)

// Node package com. huowolf. test2; public class Node {private int keyData; // keyword private int otherData; // other data private Node leftNode; // left subnode private Node rightNode; // right subnode public Node (int keyData, int otherData) {this. keyData = keyData; this. otherData = otherData;} public int getKeyData () {return keyData;} public void setKeyData (int keyData) {this. keyData = keyData;} public int getOtherData () {return otherData;} public void setOtherData (int otherData) {this. otherData = otherData;} public Node getLeftNode () {return leftNode;} public void setLefNode (Node leftNode) {this. leftNode = leftNode;} public Node getRightNode () {return rightNode;} public void setRightNode (Node rightNode) {this. rightNode = rightNode;} // display method public void display () {System. out. println (keyData + "," + otherData );}}
Package com. huowolf. test2; public class Tree {private Node root; public Node getRoot () {return root;} // insert method public void insert (int keyData, int otherData) {Node newNode = new Node (keyData, otherData); if (root = null) {root = newNode;} else {Node current = root; Node parent; // Save the reference while (true) {parent = current; if (keyData <current. getKeyData () {current = current. getLeftNode (); if (current = null) {Parent. setLefNode (newNode); return ;}} else {current = current. getRightNode (); if (current = null) {parent. setRightNode (newNode); return ;}}}// search for public Node find (int keyData) {Node current = root; while (current. getKeyData ()! = KeyData) {if (keyData <current. getKeyData () {current = current. getLeftNode ();} else {current = current. getRightNode () ;}if (current = null) {return null ;}} return current ;}// modification method public void change (int keyData, int newOtherData) {Node findNode = find (keyData); findNode. setOtherData (newOtherData);} // first traverse public void preOrder (Node node) {if (node! = Null) {node. display (); preOrder (node. getLeftNode (); preOrder (node. getRightNode () ;}}// traverse public void inOrder (Node node) {if (node! = Null) {inOrder (node. getLeftNode (); node. display (); inOrder (node. getRightNode () ;}}// traverse public void postOrder (Node node) {if (node! = Null) {postOrder (node. getLeftNode (); postOrder (node. getRightNode (); node. display ();}}}

// Test package com. huowolf. test2; public class Tree_Test {public static void main (String [] args) {Tree tree = new Tree (); tree. insert (80, 75); tree. insert (49, 49); tree. insert (42, 42); tree. insert (30, 30); tree. insert (45, 45); tree. insert (150,110); tree. insert (90, 90); tree. insert (130,130); tree. insert (); // modify the node tree. change (90, 92); Node findNode = tree. find (90); findNode. display (); System. out. println ("sequential traversal:"); tree. preOrder (tree. getRoot (); System. out. println ("-------------------------"); System. out. println ("ordinal traversal:"); tree. inOrder (tree. getRoot (); System. out. println ("-------------------------"); System. out. println ("sequential traversal:"); tree. postOrder (tree. getRoot ());}}


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.