SlideShare une entreprise Scribd logo
1  sur  40
Binary Search Trees 
• Dictionary Operations: 
 get(key) 
 put(key, value) 
 remove(key) 
• Additional operations: 
 ascend() 
 get(index) (indexed binary search tree) 
 remove(index) (indexed binary search tree)
Complexity Of Dictionary Operations 
get(), put() and remove() 
Data Structure Worst Case Expected 
Hash Table O(n) O(1) 
Binary Search 
O(n) O(log n) 
Tree 
Balanced 
Binary Search 
Tree 
O(log n) O(log n) 
n is number of elements in dictionary
Complexity Of Other Operations 
ascend(), get(index), remove(index) 
Data Structure ascend get and 
remove 
Hash Table O(D + n log n) O(D + n log n) 
Indexed BST O(n) O(n) 
Indexed 
O(n) O(log n) 
Balanced BST 
D is number of buckets
Definition Of Binary Search Tree 
• A binary tree. 
• Each node has a (key, value) pair. 
• For every node x, all keys in the left 
subtree of x are smaller than that in x. 
• For every node x, all keys in the right 
subtree of x are greater than that in x.
Example Binary Search Tree 
20 
10 
6 
2 8 
15 
40 
30 
25 
Only keys are shown.
The Operation ascend() 
20 
10 
6 
2 8 
15 
40 
30 
25 
Do an inorder traversal. O(n) time.
The Operation get() 
20 
10 
6 
2 8 
15 
40 
30 
25 
Complexity is O(height) = O(n), where n is 
number of nodes/elements.
The Operation put() 
20 
10 
6 
2 8 
15 
40 
30 
25 
Put a pair whose key is 35. 
35
The Operation put() 
20 
10 
6 
2 8 
15 
Put a pair whose key is 7. 
40 
30 
25 35 
7
The Operation put() 
20 
10 
6 
2 8 
15 
40 
30 
25 
Put a pair whose key is 18. 
35 
7 
18
The Operation put() 
20 
10 
6 
2 8 
15 
40 
30 
25 
Complexity of put() is O(height). 
35 
7 
18
The Operation remove() 
Three cases: 
 Element is in a leaf. 
 Element is in a degree 1 node. 
 Element is in a degree 2 node.
Remove From A Leaf 
20 
10 
6 
2 8 
15 
Remove a leaf element. key = 7 
40 
30 
25 35 
7 
18
Remove From A Leaf (contd.) 
20 
10 
6 
2 8 
15 
Remove a leaf element. key = 35 
40 
30 
25 35 
7 
18
Remove From A Degree 1 Node 
20 
10 
6 
2 8 
15 
40 
30 
25 35 
7 
18 
Remove from a degree 1 node. key = 40
Remove From A Degree 1 Node (contd.) 
20 
10 
6 
2 8 
15 
40 
30 
25 35 
7 
18 
Remove from a degree 1 node. key = 15
Remove From A Degree 2 Node 
20 
10 
6 
2 8 
15 
40 
30 
25 35 
7 
18 
Remove from a degree 2 node. key = 10
Remove From A Degree 2 Node 
20 
10 
6 
2 8 
15 
40 
30 
25 
35 
7 
18 
Replace with largest key in left subtree (or 
smallest in right subtree).
Remove From A Degree 2 Node 
20 
10 
6 
2 8 
15 
40 
30 
25 
35 
7 
18 
Replace with largest key in left subtree (or 
smallest in right subtree).
Remove From A Degree 2 Node 
20 
8 
6 
2 8 
15 
40 
30 
25 
35 
7 
18 
Replace with largest key in left subtree (or 
smallest in right subtree).
Remove From A Degree 2 Node 
20 
8 
6 
2 8 
15 
40 
30 
25 
35 
7 
18 
Largest key must be in a leaf or degree 1 node.
Another Remove From A Degree 2 Node 
20 
10 
6 
2 8 
15 
40 
30 
25 35 
7 
18 
Remove from a degree 2 node. key = 20
Remove From A Degree 2 Node 
20 
10 
6 
2 8 
15 
40 
30 
25 
35 
7 
18 
Replace with largest in left subtree.
Remove From A Degree 2 Node 
20 
10 
6 
2 8 
15 
40 
30 
25 
35 
7 
18 
Replace with largest in left subtree.
Remove From A Degree 2 Node 
18 
10 
6 
2 8 
15 
40 
30 
25 
35 
7 
18 
Replace with largest in left subtree.
Remove From A Degree 2 Node 
18 
10 
6 
2 8 
15 
40 
30 
25 
Complexity is O(height). 
35 
7
Indexed Binary Search Tree 
• Binary search tree. 
• Each node has an additional field. 
 leftSize = number of nodes in its left subtree
Example Indexed Binary Search Tree 
20 
10 
6 
1 
0 1 
2 8 
15 
40 
30 
1 
25 35 
7 
18 
0 
4 
0 
0 
7 
0 0 
3 
leftSize values are in red
leftSize And Rank 
Rank of an element is its position in inorder 
(inorder = ascending key order). 
[2,6,7,8,10,15,18,20,25,30,35,40] 
rank(2) = 0 
rank(15) = 5 
rank(20) = 7 
leftSize(x) = rank(x) with respect to elements in 
subtree rooted at x
leftSize And Rank 
20 
10 
6 
1 
0 1 
2 8 
15 
40 
30 
1 
25 35 
7 
18 
0 
4 
0 
0 
7 
0 0 
3 
sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
get(index) And remove(index) 
7 
20 
10 
6 
1 
0 1 
2 8 
15 
40 
30 
1 
25 35 
7 
18 
0 
4 
0 
0 
0 0 
3 
sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
get(index) And remove(index) 
• if index = x.leftSize desired element is 
x.element 
• if index < x.leftSize desired element is 
index’th element in left subtree of x 
• if index > x.leftSize desired element is 
(index - x.leftSize-1)’th element in right 
subtree of x
Applications 
(Complexities Are For Balanced Trees) 
Best-fit bin packing in O(n log n) time. 
Representing a linear list so that get(index), 
add(index, element), and remove(index) 
run in O(log(list size)) time (uses an 
indexed binary tree, not indexed binary 
search tree). 
Can’t use hash tables for either of these 
applications.
Linear List As Indexed Binary Tree 
h 
e 
b 
1 
0 1 
a d 
f 
l 
j 
1 
i k 
c 
g 
0 
4 
0 
0 
7 
0 0 
3 
list = [a,b,c,d,e,f,g,h,i,j,k,l]
add(5,’m’) 
h 
e 
b 
1 
0 1 
a d 
f 
l 
j 
1 
i k 
c 
g 
0 
4 
0 
0 
7 
0 0 
3 
list = [a,b,c,d,e,f,g,h,i,j,k,l]
add(5,’m’) 
h 
e 
b 
1 
0 1 
a d 
f 
l 
j 
1 
i k 
c 
g 
0 
4 
0 
0 
7 
0 0 
3 
list = [a,b,c,d,e, m,f,g,h,i,j,k,l] 
find node with element 4 (e)
add(5,’m’) 
h 
e 
b 
1 
0 1 
a d 
f 
l 
j 
1 
i k 
c 
g 
0 
4 
0 
0 
7 
0 0 
3 
list = [a,b,c,d,e, m,f,g,h,i,j,k,l] 
find node with element 4 (e)
add(5,’m’) 
h 
e 
b 
1 
0 1 
a d 
f 
l 
j 
1 
i k 
c 
g 
0 
4 
0 
0 
7 
0 0 
3 
m 
add m as right child of e; former right 
subtree of e becomes right subtree of m
add(5,’m’) 
h 
e 
b 
1 
0 1 
a d 
f 
l 
j 
1 
i k 
c 
g 
0 
4 
0 
0 
7 
0 0 
3 
m 
add m as leftmost node in right subtree 
of e
add(5,’m’) 
• Other possibilities exist. 
• Must update some leftSize values on path 
from root to new node. 
• Complexity is O(height).

Contenu connexe

Tendances

Solving exponential equations
Solving exponential equationsSolving exponential equations
Solving exponential equationsShaun Wilson
 
2.1 order of operations w
2.1 order of operations w2.1 order of operations w
2.1 order of operations wTzenma
 
Finding exponential equations
Finding exponential equationsFinding exponential equations
Finding exponential equationsShaun Wilson
 
algebra solving absolute value equations and inequalities
algebra solving absolute value equations and inequalitiesalgebra solving absolute value equations and inequalities
algebra solving absolute value equations and inequalitieshenig1ma
 
Integers slidecast
Integers slidecastIntegers slidecast
Integers slidecastmillergjtime
 
LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]vikram mahendra
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsAakash deep Singhal
 
Algebra solving absolute value equations and inequalities
Algebra solving absolute value equations and inequalitiesAlgebra solving absolute value equations and inequalities
Algebra solving absolute value equations and inequalitieshenig1ma
 
How Can We Evaluate Composition Of Functions
How Can We Evaluate Composition Of FunctionsHow Can We Evaluate Composition Of Functions
How Can We Evaluate Composition Of Functionsguest73f464
 
8 multiplication division of signed numbers, order of operations
8 multiplication division of signed numbers, order of operations8 multiplication division of signed numbers, order of operations
8 multiplication division of signed numbers, order of operationselem-alg-sample
 
1 s3 multiplication and division of signed numbers
1 s3 multiplication and division of signed numbers1 s3 multiplication and division of signed numbers
1 s3 multiplication and division of signed numbersmath123a
 
Top school in delhi ncr
Top school in delhi ncrTop school in delhi ncr
Top school in delhi ncrEdhole.com
 
Most Important C language program
Most Important C language programMost Important C language program
Most Important C language programTEJVEER SINGH
 

Tendances (18)

Solving exponential equations
Solving exponential equationsSolving exponential equations
Solving exponential equations
 
2.1 order of operations w
2.1 order of operations w2.1 order of operations w
2.1 order of operations w
 
Finding exponential equations
Finding exponential equationsFinding exponential equations
Finding exponential equations
 
algebra solving absolute value equations and inequalities
algebra solving absolute value equations and inequalitiesalgebra solving absolute value equations and inequalities
algebra solving absolute value equations and inequalities
 
Integers slidecast
Integers slidecastIntegers slidecast
Integers slidecast
 
LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]LIST IN PYTHON-PART 3[BUILT IN PYTHON]
LIST IN PYTHON-PART 3[BUILT IN PYTHON]
 
Lecture 11 data structures and algorithms
Lecture 11 data structures and algorithmsLecture 11 data structures and algorithms
Lecture 11 data structures and algorithms
 
Exam1 101
Exam1 101Exam1 101
Exam1 101
 
Algebra solving absolute value equations and inequalities
Algebra solving absolute value equations and inequalitiesAlgebra solving absolute value equations and inequalities
Algebra solving absolute value equations and inequalities
 
How Can We Evaluate Composition Of Functions
How Can We Evaluate Composition Of FunctionsHow Can We Evaluate Composition Of Functions
How Can We Evaluate Composition Of Functions
 
8 multiplication division of signed numbers, order of operations
8 multiplication division of signed numbers, order of operations8 multiplication division of signed numbers, order of operations
8 multiplication division of signed numbers, order of operations
 
Tricky log graphs
Tricky log graphsTricky log graphs
Tricky log graphs
 
The log rules
The log rulesThe log rules
The log rules
 
1 s3 multiplication and division of signed numbers
1 s3 multiplication and division of signed numbers1 s3 multiplication and division of signed numbers
1 s3 multiplication and division of signed numbers
 
Top school in delhi ncr
Top school in delhi ncrTop school in delhi ncr
Top school in delhi ncr
 
Algoritmo Counting sort
Algoritmo Counting sortAlgoritmo Counting sort
Algoritmo Counting sort
 
Most Important C language program
Most Important C language programMost Important C language program
Most Important C language program
 
Function graphs
Function graphsFunction graphs
Function graphs
 

Similaire à Lec26

search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortShanmuganathan C
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufWrushabhShirsat3
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.pptNielmagahod
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptxssuserd602fd
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arraysNeeru Mittal
 
Lecture12,13,14.pdf
Lecture12,13,14.pdfLecture12,13,14.pdf
Lecture12,13,14.pdfzainab278016
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfssuser034ce1
 
computer notes - Data Structures - 32
computer notes - Data Structures - 32computer notes - Data Structures - 32
computer notes - Data Structures - 32ecomputernotes
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Searchecomputernotes
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil duttAnil Dutt
 
関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡Naoki Kitora
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programmingDamian T. Gordon
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfssuser034ce1
 

Similaire à Lec26 (20)

BST.pptx
BST.pptxBST.pptx
BST.pptx
 
search_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sortsearch_sort Search sortSearch sortSearch sortSearch sort
search_sort Search sortSearch sortSearch sortSearch sort
 
chapter7.ppt
chapter7.pptchapter7.ppt
chapter7.ppt
 
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiufchapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
chapter7.pptfuifbsdiufbsiudfiudfiufeiufiuf
 
chapter7 Sorting.ppt
chapter7 Sorting.pptchapter7 Sorting.ppt
chapter7 Sorting.ppt
 
Unit III Version I.pptx
Unit III Version I.pptxUnit III Version I.pptx
Unit III Version I.pptx
 
Two dimensional arrays
Two dimensional arraysTwo dimensional arrays
Two dimensional arrays
 
Lecture4
Lecture4Lecture4
Lecture4
 
Lecture12,13,14.pdf
Lecture12,13,14.pdfLecture12,13,14.pdf
Lecture12,13,14.pdf
 
lec4.ppt
lec4.pptlec4.ppt
lec4.ppt
 
CS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdfCS-102 BST_27_3_14v2.pdf
CS-102 BST_27_3_14v2.pdf
 
PYTHON.pptx
PYTHON.pptxPYTHON.pptx
PYTHON.pptx
 
computer notes - Data Structures - 32
computer notes - Data Structures - 32computer notes - Data Structures - 32
computer notes - Data Structures - 32
 
Chapter0
Chapter0Chapter0
Chapter0
 
Computer notes - Binary Search
Computer notes - Binary SearchComputer notes - Binary Search
Computer notes - Binary Search
 
Introduction to data structure by anil dutt
Introduction to data structure by anil duttIntroduction to data structure by anil dutt
Introduction to data structure by anil dutt
 
関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡関数プログラミングことはじめ in 福岡
関数プログラミングことはじめ in 福岡
 
Sorting Methods.pptx
Sorting Methods.pptxSorting Methods.pptx
Sorting Methods.pptx
 
Introduction to MatLab programming
Introduction to MatLab programmingIntroduction to MatLab programming
Introduction to MatLab programming
 
CS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdfCS-102 BST_27_3_14.pdf
CS-102 BST_27_3_14.pdf
 

Plus de Nikhil Chilwant (20)

The joyless economy
The joyless economyThe joyless economy
The joyless economy
 
I 7
I 7I 7
I 7
 
IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )IIT Delhi training pioneer ct (acemicromatic )
IIT Delhi training pioneer ct (acemicromatic )
 
Lec39
Lec39Lec39
Lec39
 
Lec38
Lec38Lec38
Lec38
 
Lec37
Lec37Lec37
Lec37
 
Lec36
Lec36Lec36
Lec36
 
Lec35
Lec35Lec35
Lec35
 
Lec34
Lec34Lec34
Lec34
 
Lec33
Lec33Lec33
Lec33
 
Lec32
Lec32Lec32
Lec32
 
Lec30
Lec30Lec30
Lec30
 
Lec29
Lec29Lec29
Lec29
 
Lec28
Lec28Lec28
Lec28
 
Lec27
Lec27Lec27
Lec27
 
Lec25
Lec25Lec25
Lec25
 
Lec24
Lec24Lec24
Lec24
 
Lec23
Lec23Lec23
Lec23
 
Lec21
Lec21Lec21
Lec21
 
Lec20
Lec20Lec20
Lec20
 

Lec26

  • 1. Binary Search Trees • Dictionary Operations:  get(key)  put(key, value)  remove(key) • Additional operations:  ascend()  get(index) (indexed binary search tree)  remove(index) (indexed binary search tree)
  • 2. Complexity Of Dictionary Operations get(), put() and remove() Data Structure Worst Case Expected Hash Table O(n) O(1) Binary Search O(n) O(log n) Tree Balanced Binary Search Tree O(log n) O(log n) n is number of elements in dictionary
  • 3. Complexity Of Other Operations ascend(), get(index), remove(index) Data Structure ascend get and remove Hash Table O(D + n log n) O(D + n log n) Indexed BST O(n) O(n) Indexed O(n) O(log n) Balanced BST D is number of buckets
  • 4. Definition Of Binary Search Tree • A binary tree. • Each node has a (key, value) pair. • For every node x, all keys in the left subtree of x are smaller than that in x. • For every node x, all keys in the right subtree of x are greater than that in x.
  • 5. Example Binary Search Tree 20 10 6 2 8 15 40 30 25 Only keys are shown.
  • 6. The Operation ascend() 20 10 6 2 8 15 40 30 25 Do an inorder traversal. O(n) time.
  • 7. The Operation get() 20 10 6 2 8 15 40 30 25 Complexity is O(height) = O(n), where n is number of nodes/elements.
  • 8. The Operation put() 20 10 6 2 8 15 40 30 25 Put a pair whose key is 35. 35
  • 9. The Operation put() 20 10 6 2 8 15 Put a pair whose key is 7. 40 30 25 35 7
  • 10. The Operation put() 20 10 6 2 8 15 40 30 25 Put a pair whose key is 18. 35 7 18
  • 11. The Operation put() 20 10 6 2 8 15 40 30 25 Complexity of put() is O(height). 35 7 18
  • 12. The Operation remove() Three cases:  Element is in a leaf.  Element is in a degree 1 node.  Element is in a degree 2 node.
  • 13. Remove From A Leaf 20 10 6 2 8 15 Remove a leaf element. key = 7 40 30 25 35 7 18
  • 14. Remove From A Leaf (contd.) 20 10 6 2 8 15 Remove a leaf element. key = 35 40 30 25 35 7 18
  • 15. Remove From A Degree 1 Node 20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 1 node. key = 40
  • 16. Remove From A Degree 1 Node (contd.) 20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 1 node. key = 15
  • 17. Remove From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 2 node. key = 10
  • 18. Remove From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Replace with largest key in left subtree (or smallest in right subtree).
  • 19. Remove From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Replace with largest key in left subtree (or smallest in right subtree).
  • 20. Remove From A Degree 2 Node 20 8 6 2 8 15 40 30 25 35 7 18 Replace with largest key in left subtree (or smallest in right subtree).
  • 21. Remove From A Degree 2 Node 20 8 6 2 8 15 40 30 25 35 7 18 Largest key must be in a leaf or degree 1 node.
  • 22. Another Remove From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 2 node. key = 20
  • 23. Remove From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Replace with largest in left subtree.
  • 24. Remove From A Degree 2 Node 20 10 6 2 8 15 40 30 25 35 7 18 Replace with largest in left subtree.
  • 25. Remove From A Degree 2 Node 18 10 6 2 8 15 40 30 25 35 7 18 Replace with largest in left subtree.
  • 26. Remove From A Degree 2 Node 18 10 6 2 8 15 40 30 25 Complexity is O(height). 35 7
  • 27. Indexed Binary Search Tree • Binary search tree. • Each node has an additional field.  leftSize = number of nodes in its left subtree
  • 28. Example Indexed Binary Search Tree 20 10 6 1 0 1 2 8 15 40 30 1 25 35 7 18 0 4 0 0 7 0 0 3 leftSize values are in red
  • 29. leftSize And Rank Rank of an element is its position in inorder (inorder = ascending key order). [2,6,7,8,10,15,18,20,25,30,35,40] rank(2) = 0 rank(15) = 5 rank(20) = 7 leftSize(x) = rank(x) with respect to elements in subtree rooted at x
  • 30. leftSize And Rank 20 10 6 1 0 1 2 8 15 40 30 1 25 35 7 18 0 4 0 0 7 0 0 3 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
  • 31. get(index) And remove(index) 7 20 10 6 1 0 1 2 8 15 40 30 1 25 35 7 18 0 4 0 0 0 0 3 sorted list = [2,6,7,8,10,15,18,20,25,30,35,40]
  • 32. get(index) And remove(index) • if index = x.leftSize desired element is x.element • if index < x.leftSize desired element is index’th element in left subtree of x • if index > x.leftSize desired element is (index - x.leftSize-1)’th element in right subtree of x
  • 33. Applications (Complexities Are For Balanced Trees) Best-fit bin packing in O(n log n) time. Representing a linear list so that get(index), add(index, element), and remove(index) run in O(log(list size)) time (uses an indexed binary tree, not indexed binary search tree). Can’t use hash tables for either of these applications.
  • 34. Linear List As Indexed Binary Tree h e b 1 0 1 a d f l j 1 i k c g 0 4 0 0 7 0 0 3 list = [a,b,c,d,e,f,g,h,i,j,k,l]
  • 35. add(5,’m’) h e b 1 0 1 a d f l j 1 i k c g 0 4 0 0 7 0 0 3 list = [a,b,c,d,e,f,g,h,i,j,k,l]
  • 36. add(5,’m’) h e b 1 0 1 a d f l j 1 i k c g 0 4 0 0 7 0 0 3 list = [a,b,c,d,e, m,f,g,h,i,j,k,l] find node with element 4 (e)
  • 37. add(5,’m’) h e b 1 0 1 a d f l j 1 i k c g 0 4 0 0 7 0 0 3 list = [a,b,c,d,e, m,f,g,h,i,j,k,l] find node with element 4 (e)
  • 38. add(5,’m’) h e b 1 0 1 a d f l j 1 i k c g 0 4 0 0 7 0 0 3 m add m as right child of e; former right subtree of e becomes right subtree of m
  • 39. add(5,’m’) h e b 1 0 1 a d f l j 1 i k c g 0 4 0 0 7 0 0 3 m add m as leftmost node in right subtree of e
  • 40. add(5,’m’) • Other possibilities exist. • Must update some leftSize values on path from root to new node. • Complexity is O(height).

Notes de l'éditeur

  1. List elements are placed in a binary tree so that an inorder traversal of the binary tree visits the elements in list order from left to right. get() and remove() work as they do in an indexed binary search tree.