Skip to content

Commit ae55e4c

Browse files
author
tianqing.liang
committed
新增二分搜索树查询,删除
1 parent 76af35e commit ae55e4c

30 files changed

+420
-1
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

10-Binary-Search-Tree/BST.dart

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'LinkedListStatck.dart';
2+
import 'LinkedListQueue.dart';
13
/**
24
* 二分搜索树
35
*/
@@ -151,6 +153,113 @@ class BST<T extends Comparable<T>> {
151153
print(node.e);
152154
}
153155

156+
// 二分搜索树的非递归前序遍历
157+
preOrderNR() {
158+
if (root == null) return;
159+
LinkedListStack stack = new LinkedListStack();
160+
stack.push(root);
161+
while (!stack.isEmpty()) {
162+
_Node cur = stack.pop();
163+
print(cur.e);
164+
if (cur.right != null) {
165+
stack.push(cur.right);
166+
}
167+
if (cur.left != null){
168+
stack.push(cur.left);
169+
}
170+
}
171+
}
172+
173+
// 二分搜索树的层序遍历
174+
levelOrder(){
175+
if(root == null)
176+
return;
177+
LinkedListQueue q= new LinkedListQueue();
178+
q.enqueue(root);
179+
while(!q.isEmpty()){
180+
_Node cur = q.dequeue();
181+
print(cur.e);
182+
if(cur.left != null){
183+
q.enqueue(cur.left);
184+
}
185+
if(cur.right != null){
186+
q.enqueue(cur.right);
187+
}
188+
}
189+
}
190+
191+
// 寻找二分搜索树的最小元素
192+
searchMinimum<T>(){
193+
if(size == 0){
194+
throw new Exception("BST is empty!");
195+
}
196+
return _minimum(root!).e;
197+
}
198+
// 返回以node为根的二分搜索树的最小值所在的节点
199+
_Node _minimum(_Node node){
200+
if(node.left == null){
201+
return node;
202+
}
203+
return _minimum(node.left!);
204+
}
205+
206+
// 寻找二分搜索树的最大元素
207+
searchMaximum<T>(){
208+
if(size == 0){
209+
throw new Exception("BST is empty");
210+
}
211+
return _maximum(root!).e;
212+
}
213+
// 返回以node为根的二分搜索树的最大值所在的节点
214+
_Node _maximum(_Node node){
215+
if(node.right == null){
216+
return node;
217+
}
218+
return _maximum(node.right!);
219+
}
220+
221+
// 从二分搜索树中删除最小值所在节点, 返回最小值
222+
removeMin<T>(){
223+
T ret = searchMinimum();
224+
root = _removeMinDetail(root!);
225+
return ret;
226+
}
227+
// 删除掉以node为根的二分搜索树中的最小节点
228+
// 返回删除节点后新的二分搜索树的根
229+
_Node _removeMinDetail(_Node node){
230+
if(node.left == null){
231+
_Node rightNode = node.right!;
232+
node.right = null;
233+
size = size! - 1;
234+
return rightNode;
235+
}
236+
node.left = _removeMinDetail(node.left!);
237+
return node;
238+
}
239+
240+
241+
// 从二分搜索树中删除最大值所在节点
242+
removeMax<T>(){
243+
T ret = searchMaximum();
244+
root = _removeMaxDetail(root!);
245+
return ret;
246+
}
247+
// 删除掉以node为根的二分搜索树中的最大节点
248+
// 返回删除节点后新的二分搜索树的根
249+
_Node _removeMaxDetail(_Node node){
250+
if(node.right == null){
251+
_Node leftNode = node.left!;
252+
node.left = null;
253+
size = size! -1 ;
254+
return leftNode;
255+
}
256+
node.right = _removeMaxDetail(node.right!);
257+
return node;
258+
}
259+
260+
261+
262+
154263
@override
155264
String toString() {
156265
StringBuffer res = new StringBuffer();

10-Binary-Search-Tree/LinkedList.dart

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
class LinkedList<T> {
2+
_Node? _dummyHead;
3+
late int _size;
4+
5+
LinkedList() {
6+
_dummyHead = new _Node.withEmpty();
7+
_size = 0;
8+
}
9+
10+
int getSize() {
11+
return _size;
12+
}
13+
14+
bool isEmpty() {
15+
return _size == 0;
16+
}
17+
18+
remove(int index){
19+
if (index < 0 || index > _size) {
20+
throw Exception("Remove Failed,Illegal index");
21+
}
22+
_Node? prev = _dummyHead;
23+
for(var i =0;i<index ;i++){
24+
prev = prev?.next;
25+
}
26+
_Node? retNode = prev!.next;
27+
prev.next = retNode!.next;
28+
retNode.next = null;
29+
_size --;
30+
return retNode.t;
31+
}
32+
33+
//从链表中删除第一个元素,返回删除元素
34+
T? removeFirst(){
35+
remove(0);
36+
}
37+
//从链表中删除第一个元素,返回删除元素
38+
T? removeLast(){
39+
remove(_size -1);
40+
}
41+
42+
43+
//在链表的index位置添加新的元素e
44+
add(int index, T t) {
45+
if (index < 0 || index > _size) {
46+
throw Exception("Add Failed,Illegal index");
47+
}
48+
// if(index == 0){
49+
// addFirst(t);
50+
// }else{
51+
_Node? prev = _dummyHead;
52+
for (var i = 0; i < index; i++) {
53+
prev = prev?.next;
54+
}
55+
_Node _node = new _Node.wihtHead(t);
56+
_node.next = prev?.next;
57+
prev?.next = _node;
58+
_size++;
59+
// }
60+
}
61+
62+
addLast(T t) {
63+
add(_size, t);
64+
}
65+
66+
addFirst(T t) {
67+
// _Node _node =new _Node.head(t);
68+
// _node.next = _head;
69+
// _head = _node;
70+
add(0, t);
71+
}
72+
73+
//获取链表第index位置的元素
74+
T get(int index){
75+
if (index < 0 || index > _size) {
76+
throw Exception("Get Failed,Illegal index");
77+
}
78+
_Node? cur = _dummyHead!.next;
79+
for(var i=0;i<index;i++){
80+
cur = cur!.next;
81+
}
82+
return cur!.t;
83+
}
84+
85+
T getFirst(){
86+
return get(0);
87+
}
88+
T getLast(){
89+
return get(_size-1);
90+
}
91+
set(int index,T t){
92+
if (index < 0 || index > _size) {
93+
throw Exception("Set Failed,Illegal index");
94+
}
95+
_Node? cur = _dummyHead!.next;
96+
for(var i =0;i<index;i++){
97+
cur = cur!.next;
98+
}
99+
cur!.t= t;
100+
}
101+
102+
bool contains(T t){
103+
_Node? cur = _dummyHead!.next;
104+
for(var i =0;i<_size-1;i++){
105+
if(cur!.t.compareTo(t)){
106+
return true;
107+
}
108+
cur = cur.next;
109+
}
110+
return false;
111+
}
112+
113+
@override
114+
String toString() {
115+
StringBuffer res =new StringBuffer();
116+
for(_Node? cur = _dummyHead!.next ; cur != null ; cur = cur.next){
117+
res.write(cur);
118+
res.write("->");
119+
}
120+
res.write("NULL");
121+
return res.toString();
122+
}
123+
}
124+
//节点
125+
class _Node<T> {
126+
_Node? next;
127+
128+
T? t;
129+
130+
_Node.withEmpty();
131+
132+
_Node.withAll(this.t, this.next);
133+
134+
factory _Node.wihtHead(T t) {
135+
var result = new _Node.withAll(t, null);
136+
return result;
137+
}
138+
139+
@override
140+
String toString() {
141+
return t.toString();
142+
}
143+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import 'Queue.dart';
2+
3+
/**
4+
* 带尾结点的队列
5+
*/
6+
class LinkedListQueue implements Queue{
7+
8+
late _Node? _head;
9+
late _Node? _tail;
10+
late int _size;
11+
12+
LinkedListQueue(){
13+
_head = null;
14+
_tail = null;
15+
_size = 0;
16+
}
17+
18+
@override
19+
dequeue() {
20+
if (isEmpty()) {
21+
throw Exception("Cannot dequeue from an empty queue");
22+
}
23+
_Node retNode = _head!;
24+
_head = _head!.next;
25+
retNode.next = null;
26+
if(_head == null){
27+
_tail =null;
28+
}
29+
_size --;
30+
return retNode.t;
31+
}
32+
33+
@override
34+
void enqueue(e) {
35+
if(_tail == null){
36+
_tail = _Node.wihtHead(e);
37+
_head = _tail;
38+
}else{
39+
_tail!.next =_Node.wihtHead(e);
40+
_tail = _tail!.next;
41+
}
42+
_size++;
43+
}
44+
45+
@override
46+
getFront() {
47+
if (isEmpty()) {
48+
throw Exception("Queue is empty");
49+
}
50+
return _head!.t;
51+
}
52+
53+
@override
54+
int getSize() {
55+
return _size;
56+
}
57+
58+
@override
59+
bool isEmpty() {
60+
return _size == 0;
61+
}
62+
63+
@override
64+
String toString() {
65+
StringBuffer res =new StringBuffer();
66+
res.write("Queue: front ");
67+
for(_Node? cur = _head!.next ; cur != null ; cur = cur.next){
68+
res.write(cur);
69+
res.write("->");
70+
}
71+
res.write("NULL tail");
72+
return res.toString();
73+
}
74+
75+
}
76+
77+
//节点
78+
class _Node<T> {
79+
_Node? next;
80+
81+
T? t;
82+
83+
_Node();
84+
85+
_Node.withAll(this.t, this.next);
86+
87+
factory _Node.wihtHead(T t) {
88+
var result = new _Node.withAll(t, null);
89+
return result;
90+
}
91+
92+
@override
93+
String toString() {
94+
return t.toString();
95+
}
96+
}
97+
98+
void main(){
99+
LinkedListQueue queue = new LinkedListQueue();
100+
for(int i = 0 ; i < 10 ; i ++){
101+
queue.enqueue(i);
102+
print(queue);
103+
if(i % 3 ==2){
104+
queue.dequeue();
105+
print(queue);
106+
}
107+
}
108+
}

0 commit comments

Comments
 (0)