Binary search tree instance exercise

  • 2020-04-01 01:09:11
  • OfStack

A binary search tree is organized as a binary tree. Such a tree can be represented as a linked list structure, where each node is an object. In the node, in addition to the data, it also includes the fields left,right and p, which point to the left son and right son of the node respectively. If the node does not exist, it is NULL.
It's either an empty tree; Or binary trees with the following properties:
1) if the left subtree is not empty, the value of all nodes in the left subtree is less than the value of its root node;
(2) if the right subtree is not empty, the value of all nodes in the right subtree is greater than the value of its root node;
(3) left and right subtrees are binary search trees respectively;
Obviously, the above property is satisfied, so the binary search tree traversal in the middle order is traversal from small to large order, which is why it is called sort tree, of course, the above less than and greater than can be changed according to their own needs.
Binary search tree several common operations: find, delete, insert.
HDU 3791:
Problem Description
Determine whether the two sequences are the same binary search tree sequence
The Input
Let's start with n, (1< = n< =20) means there are n to be judged, and the input ends when n= 0.
The next line is a sequence, the sequence length is less than 10, contains (0~9) Numbers, no repeating Numbers, according to this sequence can be constructed a binary search tree.
There are n sequences in the next n rows. Each sequence has the same format as the first sequence. Please determine whether these two sequences can form the same binary search tree.
The Output
Print YES if the sequence is the same, otherwise print NO
The Sample Input
2
567432
543267
576342
0
The Sample Output
YES
NO
Explanation: after inserting the Numbers in order, the binary tree is used to determine whether the two binary trees are the same.
Java code
 
import java.util.Scanner; 
public class Main{ 
public static void main(String args[]){ 
Scanner in=new Scanner(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 nodes
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;//The root node
public BinarySearchTree(){//The constructor
root = null; 
} 
public BinarySearchTree(BinaryNode< T> t){//The 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){//Find operations
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){//The insert
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){//The preorder traverses the binary search tree
if(t != null){ 
sb.append(t.element); 
printTree(t.left); 
printTree(t.right); 
} 
return sb.toString(); 
} 
} 

Related articles: