Modification of binary tree by adding and deleting

Source: Internet
Author: User
Tags idate

Package Ertree;import java.util.ArrayList;
Import Java.util.Arrays;
Import java.util.Collections;
Import java.util.List;
Import Java.util.stack;public class Ertree {
public static void Main (string[] args) {
Bitree BT = new Bitree (); Bt.insert (50, 1.5);
Bt.insert (25, 1.7);
Bt.insert (75, 1.9);
Bt.insert (55, 1.0);
Bt.insert (20, 1.1);
Bt.insert (30, 2.0);
Bt.insert (5, 0.2);
Bt.insert (27, 0.1);
Bt.insert (40, 0.3);
Bt.insert (28, 0.7);
Huffmantreebuilder HB =new Huffmantreebuilder ();
HB.SS ();
}
}class Node {
person P1 = new person ();
Public Node left;
public Node right;
public int value;
}class Person {
int iDate;
Double fdate;
}class Bitree {
Node Root; Public Bitree () {
This.root = root;
} public Node find (int key) {
Node currect = root;
while (currect.p1.iDate! = key) {
if (Key < Currect.p1.iDate) {
Currect = Currect.left;
} else {
Currect = Currect.right;
}
if (currect = = null) {
return null;
}
}
return currect;
} public void insert (int ID, double dd) {
Node NewNode = new node ();
NewNode.p1.iDate = ID;
NewNode.p1.fDate = DD;
if (root = = null) {
root = NewNode;
} else {
Node currect = root;
Node parent;
while (true) {
parent = Currect;
if (ID < currect.p1.iDate) {
Currect = Currect.left;
if (currect = = null) {
Parent.left = NewNode;
Return
}
} else {
Currect = Currect.right;
if (currect = = null) {
Parent.right = NewNode;
Return
}
}
}
}
} public void Delete (int key) {
Node currect = root;
Node parent = root;
Boolean isleft = true;
while (currect.p1.iDate! = key && Currect! = null) {
parent = Currect;
if (Key > Currect.p1.iDate) {
Isleft = true;
Currect = Currect.right;
} else {
Isleft = false;
Currect = Currect.left;
}
} if (currect.left = = NULL && Currect.right = = null) {
if (currect = = root) {
root = null;
} else if (Isleft) {
Parent.right = null;
} else {
Parent.left = null;
}
} else if (currect.right = = null) {
if (currect = = root) {
root = Root.left;
} else if (Isleft) {
Parent.right = Currect.left;
} else {
Parent.left = Currect.left;
}
} else if (Currect.left = = null) {
if (currect = = root) {
root = Root.right;
} else if (Isleft) {
Parent.left = Currect.right;
} else {
Parent.right = Currect.right;
}
} else if (currect.left! = NULL && currect.right! = null) {
Node Houji = currect.right;
Node houjiparent = currect.right;
while (houji.left! = null) {
Houjiparent = Houji;
Houji = Houji.left;
}
if (currect = = root) {
root = Houji;
} else {
Currect.p1.iDate = houji.p1.iDate;
if (houji.right = = null) {
Houjiparent.left = null;
} else {
Houjiparent.left = Houji.right;
}
}
}
}//First-order traversal non-recursive
public void Inorder (Node localroot) {
stack<node> stack = new stack<node> ();
if (localroot! = null) {
Stack.push (Localroot);
while (!stack.empty ()) {
Localroot = Stack.pop ();
System.out.print (LocalRoot.p1.iDate + ",");
if (localroot.right! = null) {
Stack.push (Localroot.right);
}
if (localroot.left! = null) {
Stack.push (Localroot.left);
}
}
}
}//First-order traversal recursion
public void InOrder1 (Node localroot) {
if (localroot! = null) {
System.out.print (LocalRoot.p1.iDate + ",");
Inorder (Localroot.left);
Inorder (Localroot.right);
}
}//middle sequence traversal non-recursive
public void Iterativeinorder (Node localroot) {
stack<node> stack = new stack<node> ();
while (localroot! = NULL | |!stack.empty ()) {
while (localroot! = null) {
Stack.push (Localroot);
Localroot = Localroot.left;
}
if (!stack.empty ()) {
Localroot = Stack.pop ();
System.out.print (LocalRoot.p1.iDate + ",");
Localroot = Localroot.right;
}
}
}//middle-order traversal recursion
public void IterativeInorder1 (Node localroot) {
if (localroot! = null) {
IterativeInorder1 (Localroot.left);
System.out.print (LocalRoot.p1.iDate + ",");
IterativeInorder1 (Localroot.right);
}
}//Post-traversal
public void Iterativepostorder (node node) {
if (node = = null)
Return
stack<node> s = new stack<node> ();
Node Curnode; Nodes that are currently accessed
Node Lastvisitnode; Last visited node
Curnode = node;
Lastvisitnode = null;
Move the CurrentNode to the bottom of the left subtree.
while (Curnode! = null) {
S.push (Curnode);
Curnode = Curnode.left;
}
while (!s.empty ()) {
Curnode = S.pop (); Pop-up stack top element
The premise that a root node is accessed is that no right subtree or right subtree has been accessed
if (curnode.right! = NULL && curnode.right! = Lastvisitnode) {
Root node again into the stack
S.push (Curnode);
Into the right subtree, and certainly the right subtree must not be empty
Curnode = Curnode.right;
while (Curnode! = null) {
And go to the far left of the right subtree.
S.push (Curnode);
Curnode = Curnode.left;
}
} else {
Access
System.out.print (CurNode.p1.iDate + ",");
Modify the node that was recently accessed
Lastvisitnode = Curnode;
}
}//While
}//Find minimum value
Public Node min () {
Node current, last = null;
current = root;
while (current! = null) {
last = current;
current = Current.left;
}
return last;
}//ask for maximum value
Public Node Max () {
Node current, last = null;
current = root;
while (current! = null) {
last = current;
current = Current.right;
}
return last;
}//Seek maximum depth
public int maxDepth (node node) {
if (node = = null) {
return 0;
}
int left = maxDepth (Node.left);
int right = MaxDepth (node.right);
Return Math.max (left, right) + 1;
}//Seek minimum depth
public int mindepth (node node) {
if (node = = null) {
return 0;
}
int left = maxDepth (Node.left);
int right = MaxDepth (node.right);
Return Math.min (left, right) + 1;
}//Find the number of nodes in the binary tree
public int number (node node) {
if (node = = null) {
return 0;
}
int left = number (root.left);
int right = number (root.right);
Return left + right + 1;
}}

Modification of binary tree by adding and deleting

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.