Hướng dẫn bst python - trăn bst

Cây tìm kiếm nhị phân là gì?

Cây tìm kiếm nhị phân là cấu trúc dữ liệu cây nhị phân dựa trên nút có các thuộc tính sau: & nbsp; & nbsp;

Show

Nội phân chính

  • Cây tìm kiếm nhị phân là gì?
  • Làm thế nào để tìm kiếm một khóa trong cây nhị phân đã cho?
  • Làm thế nào để bạn tạo ra một BST?
  • Có một BST trong Python?
  • Làm thế nào để bạn tạo một nút BST?
  • Làm thế nào để bạn tạo một danh sách cây trong Python?

  • Subtree bên trái của một nút chỉ chứa các nút có các phím nhỏ hơn phím nút.
  • Subtree bên phải của một nút chỉ chứa các nút có các phím lớn hơn phím nút.
  • Cây con bên trái và bên phải cũng phải là một cây tìm kiếm nhị phân. & Nbsp; không có nút trùng lặp.
    There must be no duplicate nodes.

Hướng dẫn bst python - trăn bst

Các thuộc tính trên của cây tìm kiếm nhị phân cung cấp một đơn đặt hàng giữa các khóa để các hoạt động như tìm kiếm, tối thiểu và tối đa có thể được thực hiện nhanh chóng. Nếu không có thứ tự, thì chúng ta có thể phải so sánh mọi khóa để tìm kiếm một khóa nhất định.

Làm thế nào để tìm kiếm một khóa trong cây nhị phân đã cho?

Làm thế nào để bạn tạo ra một BST?‘n’ elements in search space and if the mid element is not the element that we are looking for, we reduce the search space to ‘n/2’ we keep reducing the search space until we either find the record that we are looking for or we get to only one element in search space and be done with this whole reduction. 

Có một BST trong Python?‘n’ nodes and as we discard one of the sub-trees, we discard ‘n/2’ nodes so our search space gets reduced to ‘n/2’. In the next step, we reduce the search space to ‘n/4’ and we repeat until we find the element or our search space is reduced to only one node. The search here is also a binary search hence the name; Binary Search Tree.

Implementation:

C++

Làm thế nào để bạn tạo một nút BST?

{

Làm thế nào để bạn tạo một danh sách cây trong Python?

Subtree bên trái của một nút chỉ chứa các nút có các phím nhỏ hơn phím nút.

Subtree bên phải của một nút chỉ chứa các nút có các phím lớn hơn phím nút.

Cây con bên trái và bên phải cũng phải là một cây tìm kiếm nhị phân. & Nbsp; không có nút trùng lặp.

Các thuộc tính trên của cây tìm kiếm nhị phân cung cấp một đơn đặt hàng giữa các khóa để các hoạt động như tìm kiếm, tối thiểu và tối đa có thể được thực hiện nhanh chóng. Nếu không có thứ tự, thì chúng ta có thể phải so sánh mọi khóa để tìm kiếm một khóa nhất định.

struct4

Để tìm kiếm một giá trị, nếu chúng tôi có một mảng được sắp xếp, chúng tôi có thể đã thực hiện tìm kiếm nhị phân. Hãy nói rằng chúng tôi muốn tìm kiếm một số trong mảng, trong tìm kiếm nhị phân, trước tiên chúng tôi xác định danh sách đầy đủ là không gian tìm kiếm của chúng tôi, số chỉ có thể tồn tại trong không gian tìm kiếm. Bây giờ chúng tôi so sánh số sẽ được tìm kiếm hoặc phần tử được tìm kiếm với phần tử giữa (trung bình) của không gian tìm kiếm và nếu bản ghi được tìm kiếm ít hơn phần tử giữa, chúng tôi sẽ tìm kiếm ở nửa bên trái, nếu không chúng tôi đi tìm kiếm Ở nửa bên phải, trong trường hợp bình đẳng, chúng tôi đã tìm thấy yếu tố. Trong tìm kiếm nhị phân, chúng tôi bắt đầu với các yếu tố 'n' trong không gian tìm kiếm và nếu phần tử giữa không phải là yếu tố mà chúng tôi đang tìm Hồ sơ mà chúng tôi đang tìm kiếm hoặc chúng tôi chỉ có một yếu tố trong không gian tìm kiếm và được thực hiện với toàn bộ mức giảm này. & NBSP;

Hoạt động tìm kiếm trong cây tìm kiếm nhị phân sẽ rất giống nhau. Giả sử chúng tôi muốn tìm kiếm số, chúng tôi bắt đầu từ gốc và sau đó chúng tôi so sánh giá trị sẽ được tìm kiếm với giá trị của gốc, nếu nó bằng nhau, chúng tôi được thực hiện với tìm kiếm nếu chúng tôi biết rằng chúng tôi cần phải Đi đến cây con bên trái vì trong một cây tìm kiếm nhị phân, tất cả các phần tử trong cây con bên trái đều nhỏ hơn và tất cả các phần tử trong cây con bên phải đều lớn hơn. Tìm kiếm một yếu tố trong cây tìm kiếm nhị phân về cơ bản là điều này, ở mỗi bước chúng tôi đi bên trái hoặc phải và ở mỗi bước, chúng tôi loại bỏ một trong những cây con. Nếu cây được cân bằng (chúng ta gọi một cây cân bằng nếu với tất cả các nút, sự khác biệt giữa độ cao của các cây con trái và phải không lớn hơn một) -Trees, chúng tôi loại bỏ các nút 'N/2' để không gian tìm kiếm của chúng tôi bị giảm xuống còn 'N/2'. Trong bước tiếp theo, chúng tôi giảm không gian tìm kiếm xuống ‘N/4 và chúng tôi lặp lại cho đến khi chúng tôi tìm thấy phần tử hoặc không gian tìm kiếm của chúng tôi được giảm xuống chỉ còn một nút. Việc tìm kiếm ở đây cũng là một tìm kiếm nhị phân do đó tên; Cây tìm kiếm nhị phân.

{

struct node* search(struct node* root, int key)

    ____10

10 15 20 30 40 50 60 
1

10 15 20 30 40 50 60 
2
10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

    ____10

10 15 20 30 40 50 60 
7

10 15 20 30 40 50 60 
2
10 15 20 30 40 50 60 
3 struct0

    

10 15 20 30 40 50 60 
3 struct3

Java

struct5 struct6int key)

    ____10 node* search(2node* search(3 node* search(4

node* search(5

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

    ____10 struct0

10 15 20 30 40 50 60 
2
10 15 20 30 40 50 60 
3 struct3

    

10 15 20 30 40 50 60 
3 struct6

C#

struct4

Python

{

struct8 struct9

node* search(5{4

    ____10

10 15 20 30 40 50 60 
1

10 15 20 30 40 50 60 
2
10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

    ____10

10 15 20 30 40 50 60 
7

10 15 20 30 40 50 60 
2
10 15 20 30 40 50 60 
3 struct0

struct4

    10 15 20 30 40 50 60 3 struct3

    8

Java

{

struct8 struct9

node* search(5{4

    ____10

10 15 20 30 40 50 60 
1

10 15 20 30 40 50 60 
2
10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

    ____10

10 15 20 30 40 50 60 
7

10 15 20 30 40 50 60 
2
10 15 20 30 40 50 60 
3 struct0

struct4

10 15 20 30 40 50 60 
22

    

10 15 20 30 40 50 60 
3 struct3 

  1. Java
  2. struct5 struct6int key)
  3.     ____10 node* search(2node* search(3 node* search(4
     

node* search(5

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4
 :

Một khóa mới luôn được chèn vào lá. Chúng tôi bắt đầu tìm kiếm một khóa từ gốc cho đến khi chúng tôi nhấn một nút lá. Sau khi tìm thấy một nút lá, nút mới được thêm vào dưới dạng con của nút lá. & NBSP;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 100 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 100

& nbsp; & nbsp; & nbsp; & nbsp; / & nbsp; \ & nbsp; & nbsp; & nbsp; & nbsp; chèn 40 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;/ & nbsp; & nbsp; \

& nbsp; & nbsp; & nbsp; 20 & nbsp; & nbsp; 500 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 20 & nbsp; & nbsp; 500 & nbsp;

& nbsp; & nbsp; & nbsp;/ & nbsp; \ & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;/ & nbsp; \ & nbsp;

& nbsp; & nbsp; 10 & nbsp; 30 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 10 & nbsp; 30

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; \ & nbsp; & nbsp;

& nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; 40

Implementation:

C++

10 15 20 30 40 50 60 
23

10 15 20 30 40 50 60 
24
10 15 20 30 40 50 60 
25
10 15 20 30 40 50 60 
26

10 15 20 30 40 50 60 
27
10 15 20 30 40 50 60 
28

    int

10 15 20 30 40 50 60 
31

    

10 15 20 30 40 50 60 
33

struct5

10 15 20 30 40 50 60 
35

    

10 15 20 30 40 50 60 
37

    

10 15 20 30 40 50 60 
39int
10 15 20 30 40 50 60 
41

    

10 15 20 30 40 50 60 
43int
10 15 20 30 40 50 60 
41

    

10 15 20 30 40 50 60 
47
10 15 20 30 40 50 60 
48

10 15 20 30 40 50 60 
49

10 15 20 30 40 50 60 
50

    

10 15 20 30 40 50 60 
52

    

10 15 20 30 40 50 60 
54

    

10 15 20 30 40 50 60 
56

{

struct4

10 15 20 30 40 50 60 
59int
10 15 20 30 40 50 60 
61

{

    

10 15 20 30 40 50 60 
64

    

10 15 20 30 40 50 60 
66

struct4

10 15 20 30 40 50 60 
68int
10 15 20 30 40 50 60 
61

{

    ____10

10 15 20 30 40 50 60 
74

node* search(5

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
77
10 15 20 30 40 50 60 
78

    struct4

    ____10

10 15 20 30 40 50 60 
83

node* search(5

10 15 20 30 40 50 60 
85

    struct4

    

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
91

node* search(5

10 15 20 30 40 50 60 
93

    struct4

    

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

struct4

10 15 20 30 40 50 60 
47 struct01

{

    ____10

10 15 20 30 40 50 60 
74

node* search(5

10 15 20 30 40 50 60 
3struct08

    struct4

    struct12

    struct14

    struct16

struct4

node* search(5

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
77
10 15 20 30 40 50 60 
78

{

    struct22

    struct24

    struct26

    struct28

    struct30

    struct32

    ____10

10 15 20 30 40 50 60 
83

    struct36

    struct38

    

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
91

struct4

    10 15 20 30 40 50 60 3 10 15 20 30 40 50 60 4

struct43

struct44

10 15 20 30 40 50 60 
47 struct01

int struct19

    struct34

10 15 20 30 40 50 60 
49

    

10 15 20 30 40 50 60 
3 struct41

{

C

struct struct46

    struct73

    struct75

    int struct49

    struct struct52

struct struct55int struct57

{

    struct struct61

node* search(5struct89

node* search(5struct91struct67struct93struct94

node* search(5struct96

    struct4

struct4

node* search(5struct63struct struct65struct66struct67struct68struct67__

{

    

10 15 20 30 40 50 60 
3 struct78

struct4

10 15 20 30 40 50 60 
47 struct81struct struct83

node* search(5node* search(17

    ____10 struct87

node* search(5node* search(23

struct node* search(01struct node* search(03int key)

struct4

node* search(5

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
77
10 15 20 30 40 50 60 
78

{

    ____10

10 15 20 30 40 50 60 
83

    node* search(35

    node* search(37

    node* search(39

    node* search(41

    node* search(43

    node* search(45

    node* search(47

    node* search(49

    

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
91

struct4

    10 15 20 30 40 50 60 3 10 15 20 30 40 50 60 4

10 15 20 30 40 50 60 
47 struct01

int struct19

    struct34

node* search(5node* search(63

    

10 15 20 30 40 50 60 
3 struct41

node* search(5{

node* search(71node* search(72

node* search(71node* search(74node* search(3struct08

node* search(5struct4

    struct4

    node* search(82

    node* search(84node* search(3node* search(86

C

struct struct46

    int struct49

    {

    struct struct52

struct struct55int struct57

    struct struct61

node* search(5struct63struct struct65struct66struct67struct68struct67__

    

10 15 20 30 40 50 60 
3 struct78

node* search(71struct23

struct4

node* search(71struct29

10 15 20 30 40 50 60 
47 struct81struct struct83

    struct4

    ____10 struct87

struct node* search(01struct node* search(03int key)

    {

node* search(5

10 15 20 30 40 50 60 
0 struct45node* search(3struct08

node* search(71struct49

node* search(71struct51

node* search(71struct53

node* search(5struct4

    struct4

    struct5 struct60

10 15 20 30 40 50 60 
47 struct62

    {

node* search(5struct66

10 15 20 30 40 50 60 
77 struct68

node* search(5struct70struct71

10 15 20 30 40 50 60 
41

node* search(5struct70struct75

10 15 20 30 40 50 60 
41

node* search(5struct70struct79

10 15 20 30 40 50 60 
41

node* search(5struct70struct83

10 15 20 30 40 50 60 
41

node* search(5struct70struct87

10 15 20 30 40 50 60 
41

node* search(5struct70struct91

10 15 20 30 40 50 60 
41

node* search(5struct70struct95

10 15 20 30 40 50 60 
41

node* search(5struct98

    struct4

struct4

Python

10 15 20 30 40 50 60 
27 node* root, 03

    struct8 node* root, 06node* root, 07node* root, 08

node* search(5node* root, 07node* root, 11node* root, 7 node* root, 4

node* search(5node* root, 07node* root, 16node* root, 7 node* root, 4

node* search(5node* root, 07node* root, 21node* root, 7 node* root, 23

struct8 node* root, 25

    ____10 node* root, 22____53 node* root, 4

10 15 20 30 40 50 60 
35

node* search(5

10 15 20 30 40 50 60 
3 node* root, 34

    

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
35

node* search(5

10 15 20 30 40 50 60 
0 node* root, 6node* root, 7node* root, 7 node* root, 9

node* search(71

10 15 20 30 40 50 60 
3 int2

node* search(5node* root, 48 int5

node* search(71node* root, 51node* root, 7 node* root, 53

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
35

node* search(71node* root, 58node* root, 7 node* root, 60

    

10 15 20 30 40 50 60 
3 int2

struct8 node* root, 65

    ____10 node* root, 68

node* search(5node* root, 70

node* search(5node* root, 72node* root, 73

node* search(5node* root, 75

node* root, 76node* root, 7 node* search(66struct71node* root, 80

node* root, 76node* root, 7 node* root, 83struct75node* root, 80

node* root, 76node* root, 7 node* root, 83struct79node* root, 80

node* root, 76node* root, 7 node* root, 83struct83node* root, 80

node* root, 76node* root, 7 node* root, 83struct87node* root, 80

node* root, 76node* root, 7 node* root, 83struct91node* root, 80

node* root, 76node* root, 7 node* root, 83struct95node* root, 80

int11

C#

10 15 20 30 40 50 60 
24 int13

10 15 20 30 40 50 60 
27 node* search(55

    struct5

10 15 20 30 40 50 60 
27 node* search(58

node* search(5struct5 int struct49

node* search(5struct5 node* search(63

node* search(5struct5 node* search(66int struct57

node* search(5{

node* search(71node* search(72

node* search(71node* search(74node* search(3struct08

node* search(5struct4

    struct4

    node* search(82

    node* search(84node* search(3node* search(86

    node* search(88int node* search(90

10 15 20 30 40 50 60 
77 node* search(92

    

10 15 20 30 40 50 60 
47 node* search(95int node* search(97

    node* search(99int key)

    {

node* search(5

10 15 20 30 40 50 60 
0 {0node* search(3struct08

node* search(71struct10

10 15 20 30 40 50 60 
77 struct12

node* search(71

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

node* search(5struct4

node* search(5

10 15 20 30 40 50 60 
0 struct21

node* search(71struct23

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0 struct27

node* search(71struct29

node* search(5

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

    struct4

    

10 15 20 30 40 50 60 
47 struct37

    

10 15 20 30 40 50 60 
47 struct40

    {

node* search(5

10 15 20 30 40 50 60 
0 struct45node* search(3struct08

node* search(71struct49

node* search(71key)13

node* search(71struct53

node* search(5struct4

    struct4

    struct5 struct60

10 15 20 30 40 50 60 
47 key)24

    {

node* search(5struct66

10 15 20 30 40 50 60 
77 struct68

node* search(5key)32

node* search(5key)34

node* search(5key)36

node* search(5key)38

node* search(5key)40

node* search(5key)42

node* search(5key)44

node* search(5struct98

    struct4

struct4

JavaScript

    8

    key)52

key)53

node* search(71key)55key)56

node* search(71key)55key)59key)55key)61node* search(3struct08

node* search(5struct4

    struct4

        9 key)75

node* search(5key)77

    struct4

        9 key)82

node* search(5

10 15 20 30 40 50 60 
0 {0node* search(3struct08

node* search(71struct10

10 15 20 30 40 50 60 
77 struct12

node* search(71

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

node* search(5

10 15 20 30 40 50 60 
0 struct21

node* search(5

10 15 20 30 40 50 60 
0 struct21

node* search(71struct23

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0 struct27

node* search(71struct29

node* search(5

10 15 20 30 40 50 60 
3
10 15 20 30 40 50 60 
4

    struct4

    

10 15 20 30 40 50 60 
47 struct37

node* search(5{17

    struct4

    

10 15 20 30 40 50 60 
47 struct40

    {

node* search(5

10 15 20 30 40 50 60 
0 struct45node* search(3struct08

node* search(71struct49

node* search(71{33{34

10 15 20 30 40 50 60 
41

node* search(71struct53

node* search(5struct4

    struct4

    struct5 struct60

10 15 20 30 40 50 60 
47 key)24

node* search(5{45

node* search(5{47

node* search(5{49

node* search(5{51

node* search(5{53

node* search(5{55

node* search(5{57

10 15 20 30 40 50 60 
22

struct4

20
30
40
50
60
70
80

JavaScript 

  1. So sánh phần tử chèn với gốc, nếu ít hơn root, sau đó gọi đệ quy Subtree, khác gọi là Subtree bên phải. & NBSP;
  2. Sau khi đạt đến cuối, chỉ cần chèn nút đó ở bên trái (nếu nhỏ hơn hiện tại) hoặc đúng. & NBSP; & nbsp;
     

Độ phức tạp về thời gian: Độ phức tạp trong trường hợp xấu nhất của các hoạt động tìm kiếm và chèn là O (h) trong đó h là chiều cao của cây tìm kiếm nhị phân. Trong trường hợp xấu nhất, chúng ta có thể phải di chuyển từ gốc đến nút lá sâu nhất. Chiều cao của một cây bị lệch có thể trở thành N và độ phức tạp về thời gian của hoạt động tìm kiếm và chèn có thể trở thành o (n). & Nbsp;The worst-case time complexity of search and insert operations is O(h) where h is the height of the Binary Search Tree. In the worst case, we may have to travel from root to the deepest leaf node. The height of a skewed tree may become n and the time complexity of search and insert operation may become O(n). 

Thực hiện: Chèn sử dụng Loop.Insertion using loop.

C++

{59

10 15 20 30 40 50 60 
24
10 15 20 30 40 50 60 
25
10 15 20 30 40 50 60 
26

10 15 20 30 40 50 60 
27 node* search(58

struct5

10 15 20 30 40 50 60 
35

    int {69

    {71

    {73

    node* search(66int {77

node* search(5{79

node* search(5

10 15 20 30 40 50 60 
54

node* search(5

10 15 20 30 40 50 60 
56

    {

    struct4

10 15 20 30 40 50 60 
49

10 15 20 30 40 50 60 
47 {90int key)

{

    {95

10 15 20 30 40 50 60 
77 struct12

    ____10

10 15 20 30 40 50 60 
74

node* search(5    02

node* search(5

10 15 20 30 40 50 60 
3struct08

    struct4

        09

        11

        13     14

node* search(5

10 15 20 30 40 50 60 
0     17

node* search(71    19

node* search(71    21

node* search(5struct4

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0     27

node* search(71    19

node* search(71    31

node* search(5struct4

    struct4

    ____10     38

node* search(5    40

    

10 15 20 30 40 50 60 
89

node* search(5    44

struct4

10 15 20 30 40 50 60 
47     47

{

        11

        52

        13     55

node* search(5

10 15 20 30 40 50 60 
0     58

node* search(71    60

node* search(71    21

node* search(5struct4

node* search(5

10 15 20 30 40 50 60 
89 {

node* search(71    69

node* search(71    71

node* search(71    73    74struct08

node* search(71    31

node* search(5struct4

    struct4

struct4

int struct19

{

        87

    node* search(37

        91

        93

    node* search(39

        97

    node* search(41

    node* search(45

    node* search(49

    

10 15 20 30 40 50 60 
3 struct41

struct4

Java

10 15 20 30 40 50 60 
008
10 15 20 30 40 50 60 
009

10 15 20 30 40 50 60 
008
10 15 20 30 40 50 60 
011

10 15 20 30 40 50 60 
27
10 15 20 30 40 50 60 
013

    struct5 struct60

10 15 20 30 40 50 60 
47
10 15 20 30 40 50 60 
018

10 15 20 30 40 50 60 
019
10 15 20 30 40 50 60 
020
10 15 20 30 40 50 60 
77
10 15 20 30 40 50 60 
37

node* search(5struct70struct75

10 15 20 30 40 50 60 
41

node* search(5struct70struct71

10 15 20 30 40 50 60 
41

node* search(5struct70

10 15 20 30 40 50 60 
033
10 15 20 30 40 50 60 
41

node* search(5struct70struct79

10 15 20 30 40 50 60 
41

node* search(5struct70

10 15 20 30 40 50 60 
041
10 15 20 30 40 50 60 
41

node* search(5struct70struct83

10 15 20 30 40 50 60 
41

node* search(5struct70struct91

10 15 20 30 40 50 60 
41

node* search(5struct98

    struct4

struct4

10 15 20 30 40 50 60 
27
10 15 20 30 40 50 60 
057

    

10 15 20 30 40 50 60 
059

    int {69

    

10 15 20 30 40 50 60 
064

    node* search(66int {77

10 15 20 30 40 50 60 
47 {90int key)

    struct4

struct4

    {95

10 15 20 30 40 50 60 
77 struct12

10 15 20 30 40 50 60 
077node* search(82

    ____10

10 15 20 30 40 50 60 
74

        13     14

node* search(5

10 15 20 30 40 50 60 
0node* search(2node* search(3struct08

node* search(71    02

node* search(71

10 15 20 30 40 50 60 
3struct08

node* search(5struct4

node* search(5

10 15 20 30 40 50 60 
102node* search(3struct08

node* search(5

10 15 20 30 40 50 60 
106

node* search(5

10 15 20 30 40 50 60 
0     17

node* search(71

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
114

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
116

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
118

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0     27

node* search(5struct4

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
116

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
128

node* search(71struct4

node* search(5struct4

node* search(5

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
135

node* search(71

10 15 20 30 40 50 60 
137

    ____10     38

    struct4

10 15 20 30 40 50 60 
47     47

node* search(5

10 15 20 30 40 50 60 
106

        13     55

node* search(5

10 15 20 30 40 50 60 
0     58

node* search(71

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
109node* search(3
10 15 20 30 40 50 60 
111

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
164

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
118

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
0     27

node* search(5struct4

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
173

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
175    74
10 15 20 30 40 50 60 
41

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
128

node* search(71struct4

node* search(5struct4

    struct4

struct4

Python3

    ____10     38

    

10 15 20 30 40 50 60 
190

10 15 20 30 40 50 60 
47     47

        13     55

node* search(5struct70struct75node* root, 80

node* search(5struct70struct71node* root, 80

node* search(5struct70

10 15 20 30 40 50 60 
033node* root, 80

node* search(5struct70struct79node* root, 80

node* search(5

10 15 20 30 40 50 60 
0     58

node* search(5struct70struct83node* root, 80

node* search(5struct70struct91node* root, 80

node* search(5

10 15 20 30 40 50 60 
227

10 15 20 30 40 50 60 
27
10 15 20 30 40 50 60 
229

    

10 15 20 30 40 50 60 
231node* root, 7 node* root, 4

    

10 15 20 30 40 50 60 
235node* root, 7
10 15 20 30 40 50 60 
237

    

10 15 20 30 40 50 60 
239node* root, 7 node* root, 4

    struct8 node* root, 06node* root, 07

10 15 20 30 40 50 60 
246

node* search(5node* root, 07node* root, 21node* root, 7

10 15 20 30 40 50 60 
251

10 15 20 30 40 50 60 
27
10 15 20 30 40 50 60 
253

    node* root, 22 node* root, 4

    struct8 node* search(95node* root, 07

10 15 20 30 40 50 60 
262

node* search(5

10 15 20 30 40 50 60 
264node* root, 7 node* root, 34

node* search(5

10 15 20 30 40 50 60 
0 struct67node* root, 07
10 15 20 30 40 50 60 
271node* root, 7node* root, 7 node* root, 4
10 15 20 30 40 50 60 
275

node* search(71node* root, 07

10 15 20 30 40 50 60 
271node* root, 7
10 15 20 30 40 50 60 
280

node* search(71

10 15 20 30 40 50 60 
3

node* search(5

10 15 20 30 40 50 60 
284node* root, 7 node* root, 4

node* search(5

10 15 20 30 40 50 60 
288node* root, 7 node* root, 07
10 15 20 30 40 50 60 
291

node* search(5    13

10 15 20 30 40 50 60 
294node* root, 7 node* root, 4
10 15 20 30 40 50 60 
275

node* search(71

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
300

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
284node* root, 7
10 15 20 30 40 50 60 
304

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
308

node* search(71node* root, 48

10 15 20 30 40 50 60 
311

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
284node* root, 7
10 15 20 30 40 50 60 
304

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
308

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
319

node* search(5

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
322

node* search(71

10 15 20 30 40 50 60 
324node* root, 7
10 15 20 30 40 50 60 
280

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
35

node* search(71

10 15 20 30 40 50 60 
331node* root, 7
10 15 20 30 40 50 60 
280

node* search(5

10 15 20 30 40 50 60 
288node* root, 7 node* root, 07
10 15 20 30 40 50 60 
291

node* search(5    13

10 15 20 30 40 50 60 
294node* root, 7 node* root, 4
10 15 20 30 40 50 60 
275

node* search(71

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
300

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
284node* root, 7
10 15 20 30 40 50 60 
304

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
369

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
308

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
319

node* search(5

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
322

node* search(71

10 15 20 30 40 50 60 
324node* root, 7
10 15 20 30 40 50 60 
280

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
308

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
319

    

10 15 20 30 40 50 60 
402

C#

node* search(5

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
322

node* search(71

10 15 20 30 40 50 60 
324node* root, 7
10 15 20 30 40 50 60 
280

node* search(5

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
35

node* search(71

10 15 20 30 40 50 60 
331node* root, 7
10 15 20 30 40 50 60 
280

    struct8 struct81node* root, 07

10 15 20 30 40 50 60 
275

    key)34

________ 35 ________ 1345 ________ 57 & nbsp;

10 15 20 30 40 50 60 
347

    

10 15 20 30 40 50 60 
424

    key)36

    

10 15 20 30 40 50 60 
428

    key)38

    key)42

    struct98

10 15 20 30 40 50 60 
077struct4

struct4

node* search(5    13

10 15 20 30 40 50 60 
294node* root, 7 node* root, 4 node* root, 5
10 15 20 30 40 50 60 
354 struct67__

node* search(71

10 15 20 30 40 50 60 
0
10 15 20 30 40 50 60 
294node* root, 7 node* root, 4
10 15 20 30 40 50 60 
275

node* search(71

10 15 20 30 40 50 60 
89
10 15 20 30 40 50 60 
35

10 15 20 30 40 50 60 
115
10 15 20 30 40 50 60 
288node* root, 7
10 15 20 30 40 50 60 
380

10 15 20 30 40 50 60 
115node* root, 72___

    key)55

10 15 20 30 40 50 60 
458

10 15 20 30 40 50 60 
077struct4

struct4

____10

10 15 20 30 40 50 60 
396node* root, 7node* root, 7
10 15 20 30 40 50 60 
399
10 15 20 30 40 50 60 
35

10 15 20 30 40 50 60 
24 int13

10 15 20 30 40 50 60 
24
10 15 20 30 40 50 60 
406

struct5

10 15 20 30 40 50 60 
27
10 15 20 30 40 50 60 
013

10 15 20 30 40 50 60 
0777____25 struct60
10 15 20 30 40 50 60 
47
10 15 20 30 40 50 60 
414

10 15 20 30 40 50 60 
483    02

10 15 20 30 40 50 60 
483
10 15 20 30 40 50 60 
3struct08

    struct4

    

10 15 20 30 40 50 60 
491node* search(3struct08

    

10 15 20 30 40 50 60 
495

    

10 15 20 30 40 50 60 
416
10 15 20 30 40 50 60 
77
10 15 20 30 40 50 60 
37

    key)32

node* search(5    19

node* search(5

10 15 20 30 40 50 60 
507

struct5

10 15 20 30 40 50 60 
27 node* search(58

node* search(5    19

10 15 20 30 40 50 60 
0777____25
10 15 20 30 40 50 60 
059

10 15 20 30 40 50 60 
483struct4

    struct4

10 15 20 30 40 50 60 
0777____25 int {69

10 15 20 30 40 50 60 
483
10 15 20 30 40 50 60 
525

    

10 15 20 30 40 50 60 
89

10 15 20 30 40 50 60 
483
10 15 20 30 40 50 60 
529

10 15 20 30 40 50 60 
077struct4

10 15 20 30 40 50 60 
0777____25
10 15 20 30 40 50 60 
064

    

10 15 20 30 40 50 60 
495

10 15 20 30 40 50 60 
0777____25 node* search(66int
10 15 20 30 40 50 60 
455

struct5

10 15 20 30 40 50 60 
27
10 15 20 30 40 50 60 
28

10 15 20 30 40 50 60 
0777____25 node* search(82

node* search(5

10 15 20 30 40 50 60 
553

node* search(5

10 15 20 30 40 50 60 
507

10 15 20 30 40 50 60 
0777____25
10 15 20 30 40 50 60 
47 node* search(95int
10 15 20 30 40 50 60 
473

node* search(5

10 15 20 30 40 50 60 
561

    

10 15 20 30 40 50 60 
475
10 15 20 30 40 50 60 
77 struct12

node* search(5

10 15 20 30 40 50 60 
516

10 15 20 30 40 50 60 
483struct4

    struct4

10 15 20 30 40 50 60 
077struct4

struct4

    ____10 {0node* search(3struct08

10 15 20 30 40 50 60 

        13

10 15 20 30 40 50 60 
498node* search(3struct08  

  • 10 15 20 30 40 50 60 
    483
    10 15 20 30 40 50 60 
    0
    10 15 20 30 40 50 60 
    503
  • 10 15 20 30 40 50 60 
    483
    10 15 20 30 40 50 60 
    509
    10 15 20 30 40 50 60 
    89 ____10
    10 15 20 30 40 50 60 
    512
  • node* search(5
    10 15 20 30 40 50 60 
    516

    ____10

10 15 20 30 40 50 60 
523 

  • 10 15 20 30 40 50 60 
    0777____25
    10 15 20 30 40 50 60 
    47 {15
  •     
    10 15 20 30 40 50 60 
    539
    10 15 20 30 40 50 60 
    77
    10 15 20 30 40 50 60 
    541
  •         13
    10 15 20 30 40 50 60 
    498node* search(3
    10 15 20 30 40 50 60 
    546
  • 10 15 20 30 40 50 60 
    483
    10 15 20 30 40 50 60 
    0
    10 15 20 30 40 50 60 
    498node* search(3struct08

10 15 20 30 40 50 60 48310 15 20 30 40 50 60 50910 15 20 30 40 50 60 89 {

node* search(5

10 15 20 30 40 50 60 
563    74
10 15 20 30 40 50 60 
41.

Đầu ra

Một số sự thật thú vị: & nbsp; & nbsp;

Nếu giá trị đích nhỏ hơn giá trị khóa cho một nút, hãy tạo nút hiện tại của đứa trẻ bên trái ..

Có một BST trong Python?

Cây tìm kiếm nhị phân là một loại cấu trúc dữ liệu cây đặc biệt có thứ tự cung cấp một danh sách các nút hoặc đỉnh được sắp xếp. Trong Python, chúng ta có thể trực tiếp tạo một đối tượng BST bằng mô -đun BinaryTree. BST () tạo một cây tìm kiếm nhị phân ngẫu nhiên và trả về nút gốc của nó.In Python, we can directly create a BST object using binarytree module. bst() generates a random binary search tree and return its root node.

Làm thế nào để bạn tạo một nút BST?

Algorithm..

Tạo một nút BST mới và gán các giá trị cho nó ..

insert(node, key) i) If root == NULL, return the new node to the calling function. ii) if root=>data < key. call the insert function with root=>right and assign the return value in root=>right. ... .

Cuối cùng, trả lại con trỏ gốc ban đầu về chức năng gọi ..

Làm thế nào để bạn tạo một danh sách cây trong Python?

Đưa ra một danh sách các danh sách, hãy viết một chương trình Python để chuyển đổi danh sách danh sách đã cho thành một từ điển giống như cây ...

Examples:.

Phương pháp số 1: Phương pháp ngây thơ. Đây là một cách tiếp cận ngây thơ trong đó chúng tôi sử dụng hai vòng cho các vòng lặp để vượt qua danh sách danh sách. ....

Phương pháp số 2: Sử dụng giảm ().