Why practical binary treeOne, inserting delete data in an ordered array is too slow 1 inserting or deleting a piece of data moves all subsequent data two, finding the data in the linked list is too slow 2 find can only be found from the beginning or the tail of a bar
using trees to solve problemsIs there a way to insert and delete like a linked list so fast that the query can look as fast as an ordered array? Number realizes these characteristics, called one of the most interesting data structures
terminology of the treeSuch as
Tree balance tree and non-equilibrium tree
two cross-tree classes
public class Tree {/** * */private node root;/** * Constructor Method */public Tree () {}/** * constructor * * @param root * and Node */public Tree (Node root) {this.root = root;}} Class Node { /* key */ int key; /* value */ Object val ue; /* left node */ node leftchildnode; /* right node */ Node rightchildnode; /** * Construction method * * @param key * Keywords * @ param value * values */ public Node (int key, Object value) { super (); & nbsp this.key = key; this.value = value; }}
two fork tree Insert function
/** * Insert Node * * @param key * key * @param value * */public void Insert (int key, Object value) {node node = NE W Node (key, value), if (this.root = = null) {this.root = node;} else {node CurrentNode = This.root;while (True) {if (key ; Currentnode.key) {if (Currentnode.rightchildnode = = null) {Currentnode.rightchildnode = Node;return;} else {CurrentNode = Currentnode.rightchildnode;}} else {if (Currentnode.leftchildnode = = null) {Currentnode.leftchildnode = Node;return;} else {currentnode = Currentnode.l Eftchildnode;}}}}
Two search function of the fork tree
/** * Find node * * @param key * @return */public node find (int key) {if (this.root! = null) {Node CurrentNode = this.root; while (Currentnode.key! = key) {if (Key > Currentnode.key) {currentnode = Currentnode.rightchildnode;} else {Currentnod e = Currentnode.leftchildnode;} if (CurrentNode = = null) {return null;}}} return null;}
Two cross-tree display function (middle sequence traversal)
private void Show (node node) {if (node! = null) {this.show (Node.leftchildnode); System.out.println (Node.key + ":" + Node.value); This.show (Node.rightchildnode);}}
Test
public static void Main (string[] args) {node root = new Node (50, 24); Tree tree = new Tree (root), Tree.insert (530), Tree.insert (540, 520), Tree.insert (4, 540), Tree.insert (0, 550); Tree.insert (8, 520); Tree.show ();}
Full code
Package tree;/** * Binary tree * * @author JYC506 * */public class Tree {/** * nodes */private node root;/** * Construction Method */public Tree ( {}/** * Construction method * * @param root * with node */public Tree (nodes root) {this.root = root;} /** * Find node * * @param key * @return */public node find (int key) {if (this.root! = null) {Node CurrentNode = This.root;whi Le (Currentnode.key! = key) {if (Key > Currentnode.key) {currentnode = Currentnode.rightchildnode;} else {CurrentNode = Currentnode.leftchildnode;} if (CurrentNode = = null) {return null;}}} return null;} /** * Insert Node * * @param key * key * @param value * */public void Insert (int key, Object value) {Nod e node = new node (key, value), if (this.root = = null) {this.root = node;} else {node CurrentNode = This.root;while (True) { if (Key > Currentnode.key) {if (Currentnode.rightchildnode = = null) {Currentnode.rightchildnode = Node;return;} else {C Urrentnode = Currentnode.rightchildnode;}} else {if (Currentnode.leftchildnode = = null) {CurrEntnode.leftchildnode = Node;return;} else {currentnode = Currentnode.leftchildnode;}}}} /** * Show */public Void Show () {this.show (root);} /** * Middle Sequence traversal * * @param node */private void Show (node node) {if (node! = null) {this.show (Node.leftchildnode); System.out.println (Node.key + ":" + Node.value); This.show (Node.rightchildnode);}} public static void Main (string[] args) {node root = new Node (50, 24); Tree tree = new Tree (root), Tree.insert (530), Tree.insert (540, 520), Tree.insert (4, 540), Tree.insert (0, 550); Tree.insert (8, 520); Tree.show ();}} Class Node {/* key */int key;/* value */object value;/* left node */node leftchildnode;/* right node */node rightchildnode;/** * Construction Method * * @param key * keyword * @param value */public Node (int key, Object value) {super (); This.key = Key;thi S.value = value;}}
Binary tree Algorithm (Java)