Binary Search Tree instance exercises

Source: Internet
Author: User

A binary tree is organized based on the binary tree structure. Such a tree can be represented by a linked list. Each node is an object. In addition to data, the node also includes the left, right, and p fields. They point to the left and right sons of the node respectively. If the node does not exist, the value is NULL.
It is either an empty tree or a binary tree of the following nature:
1) if the left subtree is not empty, the values of all nodes on the left subtree are smaller than the value of its root node;
(2) If the right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node;
(3) left and right subtree are also binary lookup trees;
Obviously, the above properties are satisfied, so the binary search tree is traversed in the ascending order, which is why it is called the sorting tree, of course, the above values smaller than or greater than can be changed according to your own needs.
Common Operations of the Binary Search Tree: search, delete, and insert.
HDU 3791:
Problem Description
Determine whether the two sequences are of the same binary search tree Sequence
Input
Start with n, (1 <= n <= 20) indicates there are n to be judged, and the input ends when n = 0.
The next row is a sequence with a length less than 10 and contains (0 ~ 9). There are no repeated numbers. Based on this sequence, a binary search tree can be constructed.
The next n rows have n sequences. The format of each sequence is the same as that of the first sequence. Determine whether the two sequences can form the same binary search tree.
Output
If the sequence is the same, YES is output; otherwise NO is output.
Sample Input
2
567432
543267
576342
0
Sample Output
YES
NO
Explanation: After the numbers are inserted in sequence, use the binary tree to traverse and determine whether the two binary trees are the same.
Java code Copy codeThe Code is as follows: import java. util. collections;
Public class Main {
Public static void main (String args []) {
Running in = new Processing (System. in );
BinarySearchTree <Character> root = null;
While (in. hasNext ()){
Int t = in. nextInt ();
If (t = 0) break;
Root = new BinarySearchTree <Character> ();
String result = null;
String result1 = null;
String s = in. next ();
For (int I = 0; I <s. length (); I ++ ){
Root. insert (s. charAt (I ));
}
Result = root. printTree (root. getRoot ());
For (int I = 0; I <t; I ++ ){
Root = new BinarySearchTree <Character> ();
S = in. next ();
For (int j = 0; j <s. length (); j ++ ){
Root. insert (s. charAt (j ));
}
Result1 = root. printTree (root. getRoot ());
If (result. equals (result1) System. out. println ("YES ");
Else System. out. println ("NO ");
}
}
}
}
Class BinaryNode <T extends Comparable <T> {// Binary Search Tree node
BinaryNode <T> left;
BinaryNode <T> right;
T element;
Public BinaryNode (T theElement ){
This (theElement, null, null );
}
Public BinaryNode (T theElement, BinaryNode lt, BinaryNode rt ){
Element = theElement;
Left = lt;
Right = rt;
}
Public T getElement (){
Return this. element;
}
Public BinaryNode <T> getLeft (){
Return this. left;
}
Public BinaryNode <T> getRight (){
Return this. right;
}
Public String toString (){
Return element + "";
}
}
Class BinarySearchTree <T extends Comparable <T> {// Binary Search Tree
Private BinaryNode <T> root; // root Node
Public BinarySearchTree () {// Constructor
Root = null;
}
Public BinarySearchTree (BinaryNode <T> t) {// Constructor
Root = t;
}
Public void makeEmpty (){
Root = null;
}
Public boolean isEmpty (){
Return root = null;
}
Public BinaryNode <T> getRoot (){
Return root;
}
Public T find (T x ){
Return find (x, root). element;
}
Public void insert (T x ){
Root = insert (x, root );
}
Public void printTree (){
PrintTree (root );
}
Private BinaryNode <T> find (T x, BinaryNode <T> t) {// search operation
If (t = null ){
Return null;
}
If (x. compareTo (t. element) <0 ){
Return find (x, t. left );
}
Else if (x. compareTo (t. element) = 0 ){
Return t;
}
Else {
Return find (x, t. right );
}
}
Private BinaryNode <T> insert (T x, BinaryNode <T> t) {// insert operation
If (t = null ){
T = new BinaryNode <T> (x, null, null );
}
Else if (x. compareTo (t. element) <0 ){
T. left = insert (x, t. left );
}
Else if (x. compareTo (t. element)> 0 ){
T. right = insert (x, t. right );
}
Else;
Return t;
}
Private StringBuffer sb = new StringBuffer ();
Public String printTree (BinaryNode <T> t) {// traverse the Binary Search Tree in the forward order
If (t! = Null ){
Sb. append (t. element );
PrintTree (t. left );
PrintTree (t. right );
}
Return sb. toString ();
}
}

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.