Implement BST

  1 import java.util.*;
  2
  3 //docs about generic version of class: https://docs.oracle.com/javase/tutorial/java/generics/types.html
  4 public class myBST <T extends Comparable<T>>{
  5
  6     //define tree node using a inner class
  7     private class Node<T> {
  8       private T data;
  9       private Node<T> left, right;
 10
 11       public Node (T data) {
 12         this.data = data;
 13       }
 14
 15       public Node (T data, Node<T> left, Node<T> right) {
 16         this.data = data;
 17         this.left = left;
 18         this.right = right;
 19       }
 20     }
 21
 22     //Fields
 23     private Node<T> root;
 24     private Comparator<T> comparator;
 25
 26     //Two constructer
 27     public myBST() {
 28       root = null;
 29       comparator = null;
 30     }
 31
 32     public myBST(Comparator<T> comparator) {
 33       root = null;
 34       this.comparator = comparator;
 35     }
 36
 37     private int compare(T x, T y) {
 38       if (comparator == null) {
 39         return x.compareTo(y);
 40       }else{
 41         return comparator.compare(x, y);
 42       }
 43     }
 44
 45     //**************************************************************************
 46     //                    insert
 47     //**************************************************************************
 48     public void insert(T data) {
 49       root = insert(root, data);
 50     }
 51
 52     private Node<T> insert(Node<T> root, T data) {
 53       if (root == null) {
 54         return new Node<T>(data);
 55       }
 56       if (compare(root.data, data) == 0) {
 57         return root;
 58       }else if (compare(root.data, data) > 0) {
 59         root.right = insert(root.right, data);
 60       }else{
 61         root.left = insert(root.left, data);
 62       }
 63       return root;
 64     }
 65
 66     //**************************************************************************
 67     //                    search
 68     //**************************************************************************
 69     public boolean search (T data) {
 70       return search(root, data);
 71     }
 72     private boolean search (Node<T> root, T data) {
 73       if (root == null) {
 74         return false;
 75       }
 76       if (compare(root.data, data) == 0) {
 77         return true;
 78       }else if (compare(root.data, data) < 0) {
 79         return search(root.right, data);
 80       }else{
 81         return search(root.left, data);
 82       }
 83     }
 84
 85     //**************************************************************************
 86     //                    delete
 87     //**************************************************************************
 88     public void delete(T data) {
 89       root = delete(root, data);
 90     }
 91     private Node<T> delete(Node<T> root, T data) {
 92       if (root == null) {
 93         throw new RuntimeException("Cannot delete");
 94       }
 95       if (compare(root.data, data) == 0) {
 96         if (root.left == null) {
 97           return root.right;
 98         }else if (root.right == null) {
 99           return root.left;
100         }else{
101           Node<T> cur = root;
102           while (cur.left != null) {
103             cur = cur.left;
104           }
105           root.data = cur.data;
106           delete(root.left, root.data);
107         }
108       }else if (compare(root.data, data) < 0) {
109         delete(root.right, data);
110       }else{
111         delete(root.left, data);
112       }
113       return root;
114     }
115
116
117     public void preOrderTraversal()
118     {
119         preOrderHelper(root);
120     }
121     private void preOrderHelper(Node<T> r)
122     {
123         if (r != null)
124         {
125             System.out.print(r+" ");
126             preOrderHelper(r.left);
127             preOrderHelper(r.right);
128         }
129     }
130
131
132
133
134     public static void main(String[] args) {
135       Integer[] a = {1,5,2,7,4};
136       BST<Integer> bst = new BST<Integer>(new MyComp1());
137       //bst.delete(1);
138       for(Integer n : a) bst.insert(n);
139       bst.preOrderTraversal();
140       System.out.println();
141     }
142 }
143
144 class MyComp1 implements Comparator<Integer>
145 {
146     public int compare(Integer x, Integer y)
147     {
148         return y-x;
149     }
150 }
时间: 2024-10-11 21:49:20

Implement BST的相关文章

lintcode 中等题: Implement Trie

题目 Implement Trie Implement a trie with insert, search, and startsWith methods. 样例 注意 You may assume that all inputs are consist of lowercase letters a-z. 解题 Trie,字典树,又称单词查找树.前缀树,是一种哈希树的变种.应用于字符串的统计与排序,经常被搜索引擎系统用于文本词频统计. 性质: 1.根节点不包含字符,除根节点外的每一个节点都只包

Leetcode: Serialize and Deserialize BST

Serialization is the process of converting a data structure or object into a sequence of bits so that it can be stored in a file or memory buffer, or transmitted across a network connection link to be reconstructed later in the same or another comput

BST迭代器

Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Note: next() and hasNext() should run in average O(1) time and uses

代写二叉查找树程序作业、代写BST 作业、代写Data Structures

代写二叉查找树程序作业.代写BST 作业.代写Data StructuresKIT205 Data Structures and AlgorithmsAssignment 1: Data StructuresDue: 27th April, 11:55pmIntroductionYou work for an education company that develops and runs Massive Open Online Courses(MOOCs). You have been ask

Java AVL、BST编程作业代写、代做JDK实验

Java AVL.BST编程作业代写.代做JDK实验1 IntroductionNeeding a way to manage all your PlayStation friends, you decide to build a backendsystem for adding, removing and maintaining them. The idea is to organiseyour friends so you can search for individuals, search

[Cracking the Coding Interview] 4.5 Validate BST

Implement a function to check if a binary tree is a binary search tree. 这道题很经典,让我们判断一棵树是不是二叉查找树.但是首先要确定一下二叉查找树的定义,比如leetcode 98题中的定义左<根<右就可以直接通过判断中序遍历是不是有序序列就可以了.但是一般的BST定义的是左<=根<右,就不可以用这种方法来判断. 如果是如上定义,直接根据定义递归的检查下每一个node是否满足定义就可以了,如下: (对于roo

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/ Implement strStr(). Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 字符串简单题.一定要写的没BUG. 如果不在函数最后一行写return语句的话,LeetCode会出RUNTIME ERROR. Line 27: co

Implement strStr()

package cn.edu.xidian.sselab.string; /** *  * @author zhiyong wang * title: Implement strStr() * content: *  Implement strStr(). *   *  Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.  * */pub