Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on,...

51
Binary Trees S. M. Niaz Arifin, PhD Computer Science and Engineering University of Notre Dame Indiana, USA

Transcript of Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on,...

Page 1: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

BinaryTrees

S.M.NiazArifin,PhDComputerScienceandEngineering

UniversityofNotreDameIndiana,USA

Page 2: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  Binarytree•  defini=on,example

•  types

•  Binarysearchtree•  defini=on,example

•  opera=ons– search– maximum,minimum–  insert–  traversal– delete

S.M.NiazArifin,PhD

Overview

Page 3: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  atreedatastructure•  eachnodehasatmosttwochildren–  thele)childandtherightchild– mayalsocontainaparentnode

•  leafnodes:lowest-levelnodes– nochildren

•  alwaysrooted•  root:onlynodewhoseparentisNIL

S.M.NiazArifin,PhD

BinaryTree

Page 4: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

S.M.NiazArifin,PhD

BinaryTree:Example

root

internalnodes

leafnodes

•  numberofnodes,n=9•  height(depth),h=3

Page 5: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  fullbinarytree•  a.k.a.aproperorplanebinarytree

•  everynodeinthetreehaseither0or2children

S.M.NiazArifin,PhD

BinaryTree:Types

Page 6: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  completebinarytree•  everylevel,exceptpossiblythelast,iscompletelyfilled

•  allnodesinthelastlevelareasfarle)aspossible

S.M.NiazArifin,PhD

BinaryTree:Types

Page 7: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  balancedbinarytree•  hastheminimumpossibleheight(a.k.a.depth)fortheleafnodes

•  self-balancing– automa=callykeepsitsheightsmall

S.M.NiazArifin,PhD

BinaryTree:Types

Page 8: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

balancedbinarytree

S.M.NiazArifin,PhD

BinaryTree:Types

unbalancedbinarytree

imagesource:wikipedia

Page 9: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  ordered(sorted)binarytree•  nodekeysarealwaysstoredinsuchawayastosa=sfytheBSTproperty:–  letxbeanodeinabinarysearchtree–  ifyisanodeintheleRsubtreeofx,theny:key≤x:key–  ifyisanodeintherightsubtreeofx,theny:key≥x:key

S.M.NiazArifin,PhD

BinarySearchTree(BST)

Page 10: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

ifyisanodeintheleRsubtreeofx,theny:key≤x:key

ifyisanodeintherightsubtreeofx,theny:key≥x:key

S.M.NiazArifin,PhD

BinarySearchTree(BST)

Page 11: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  allowsfastsearch(lookup),inser=on,anddele=onofnodes

•  supportsmanydynamic-setopera=ons– create,add,remove,etc.

•  let,n=numberofnodesinthetree

•  'mecomplexityforsearch,inser=on,ordele=on– propor=onaltothelogarithmofn:O(logn)

•  averagecase[worstcase:O(n)]– muchbeWerthanlinear=meforunsortedarrays:O(n)

S.M.NiazArifin,PhD

BinarySearchTree:Proper=es

Page 12: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  search•  maximum,minimum

•  insert•  traversal•  delete

S.M.NiazArifin,PhD

BinarySearchTree:Opera=ons

Page 13: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  searchthetreeforaspecifickey•  canberecursiveoritera;ve•  example:searchfor45

S.M.NiazArifin,PhD

Search

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 14: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  startattheroot,45>25–  searchinrightsubtree

S.M.NiazArifin,PhD

Search

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 15: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  startattheroot,45>25–  searchinrightsubtree

2.   45<50,searchin50’sleRsubtree

S.M.NiazArifin,PhD

Search

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 16: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  startattheroot,45>25–  searchinrightsubtree

2.   45<50,searchin50’sleRsubtree3.   45>35,searchin35’srightsubtree

S.M.NiazArifin,PhD

Search

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 17: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  startattheroot,45>25–  searchinrightsubtree

2.   45<50,searchin50’sleRsubtree3.   45>35,searchin35’srightsubtree4.   45>44,but44hasnorightsubtree

so45is�notintheBST

S.M.NiazArifin,PhD

Search

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 18: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  algorithm•  n=numberofnodesintree

•  ;mecomplexity:O(logn)

S.M.NiazArifin,PhD

Search

290 Chapter 12 Binary Search Trees

2 4

3

13

7

6

17 20

18

15

9

Figure 12.2 Queries on a binary search tree. To search for the key 13 in the tree, we follow the path15 ! 6 ! 7 ! 13 from the root. The minimum key in the tree is 2, which is found by followingleft pointers from the root. The maximum key 20 is found by following right pointers from the root.The successor of the node with key 15 is the node with key 17, since it is the minimum key in theright subtree of 15. The node with key 13 has no right subtree, and thus its successor is its lowestancestor whose left child is also an ancestor. In this case, the node with key 15 is its successor.

TREE-SEARCH.x; k/

1 if x == NIL or k == x:key2 return x3 if k < x:key4 return TREE-SEARCH.x: left; k/5 else return TREE-SEARCH.x:right; k/

The procedure begins its search at the root and traces a simple path downward inthe tree, as shown in Figure 12.2. For each node x it encounters, it compares thekey k with x:key. If the two keys are equal, the search terminates. If k is smallerthan x:key, the search continues in the left subtree of x, since the binary-search-tree property implies that k could not be stored in the right subtree. Symmetrically,if k is larger than x:key, the search continues in the right subtree. The nodesencountered during the recursion form a simple path downward from the root ofthe tree, and thus the running time of TREE-SEARCH is O.h/, where h is the heightof the tree.

We can rewrite this procedure in an iterative fashion by “unrolling” the recursioninto a while loop. On most computers, the iterative version is more efficient.

12.2 Querying a binary search tree 291

ITERATIVE-TREE-SEARCH.x; k/

1 while x ¤ NIL and k ¤ x:key2 if k < x:key3 x D x: left4 else x D x:right5 return x

Minimum and maximumWe can always find an element in a binary search tree whose key is a minimum byfollowing left child pointers from the root until we encounter a NIL, as shown inFigure 12.2. The following procedure returns a pointer to the minimum element inthe subtree rooted at a given node x, which we assume to be non-NIL:

TREE-MINIMUM.x/

1 while x: left ¤ NIL2 x D x: left3 return x

The binary-search-tree property guarantees that TREE-MINIMUM is correct. If anode x has no left subtree, then since every key in the right subtree of x is at least aslarge as x:key, the minimum key in the subtree rooted at x is x:key. If node x hasa left subtree, then since no key in the right subtree is smaller than x:key and everykey in the left subtree is not larger than x:key, the minimum key in the subtreerooted at x resides in the subtree rooted at x: left.

The pseudocode for TREE-MAXIMUM is symmetric:

TREE-MAXIMUM.x/

1 while x:right ¤ NIL2 x D x:right3 return x

Both of these procedures run in O.h/ time on a tree of height h since, as in TREE-SEARCH, the sequence of nodes encountered forms a simple path downward fromthe root.

Successor and predecessorGiven a node in a binary search tree, sometimes we need to find its successor inthe sorted order determined by an inorder tree walk. If all keys are distinct, the

Page 19: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  maximum

•  minimum

•  ;mecomplexity:O(logn)

S.M.NiazArifin,PhD

MaximumandMinimum

12.2 Querying a binary search tree 291

ITERATIVE-TREE-SEARCH.x; k/

1 while x ¤ NIL and k ¤ x:key2 if k < x:key3 x D x: left4 else x D x:right5 return x

Minimum and maximumWe can always find an element in a binary search tree whose key is a minimum byfollowing left child pointers from the root until we encounter a NIL, as shown inFigure 12.2. The following procedure returns a pointer to the minimum element inthe subtree rooted at a given node x, which we assume to be non-NIL:

TREE-MINIMUM.x/

1 while x: left ¤ NIL2 x D x: left3 return x

The binary-search-tree property guarantees that TREE-MINIMUM is correct. If anode x has no left subtree, then since every key in the right subtree of x is at least aslarge as x:key, the minimum key in the subtree rooted at x is x:key. If node x hasa left subtree, then since no key in the right subtree is smaller than x:key and everykey in the left subtree is not larger than x:key, the minimum key in the subtreerooted at x resides in the subtree rooted at x: left.

The pseudocode for TREE-MAXIMUM is symmetric:

TREE-MAXIMUM.x/

1 while x:right ¤ NIL2 x D x:right3 return x

Both of these procedures run in O.h/ time on a tree of height h since, as in TREE-SEARCH, the sequence of nodes encountered forms a simple path downward fromthe root.

Successor and predecessorGiven a node in a binary search tree, sometimes we need to find its successor inthe sorted order determined by an inorder tree walk. If all keys are distinct, the

12.2 Querying a binary search tree 291

ITERATIVE-TREE-SEARCH.x; k/

1 while x ¤ NIL and k ¤ x:key2 if k < x:key3 x D x: left4 else x D x:right5 return x

Minimum and maximumWe can always find an element in a binary search tree whose key is a minimum byfollowing left child pointers from the root until we encounter a NIL, as shown inFigure 12.2. The following procedure returns a pointer to the minimum element inthe subtree rooted at a given node x, which we assume to be non-NIL:

TREE-MINIMUM.x/

1 while x: left ¤ NIL2 x D x: left3 return x

The binary-search-tree property guarantees that TREE-MINIMUM is correct. If anode x has no left subtree, then since every key in the right subtree of x is at least aslarge as x:key, the minimum key in the subtree rooted at x is x:key. If node x hasa left subtree, then since no key in the right subtree is smaller than x:key and everykey in the left subtree is not larger than x:key, the minimum key in the subtreerooted at x resides in the subtree rooted at x: left.

The pseudocode for TREE-MAXIMUM is symmetric:

TREE-MAXIMUM.x/

1 while x:right ¤ NIL2 x D x:right3 return x

Both of these procedures run in O.h/ time on a tree of height h since, as in TREE-SEARCH, the sequence of nodes encountered forms a simple path downward fromthe root.

Successor and predecessorGiven a node in a binary search tree, sometimes we need to find its successor inthe sorted order determined by an inorder tree walk. If all keys are distinct, the

Page 20: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  insertnewnodex:worksmuchlikesearch•  ;mecomplexity:O(lgn)•  setrootascurrentnode(current)•  recursivelyexaminecurrent•  ifx:key<current:key–  ifcurrenthasaleRchild,searchleR– elseaddxascurrent’sleRchild

•  ifx:key≥current:key–  ifcurrentnodehasarightchild,searchright– elseaddxascurrent’srightchild

S.M.NiazArifin,PhD

Insert

Page 21: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:insertnode60

S.M.NiazArifin,PhD

Insert

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 22: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  startattheroot,60>25–  searchinrightsubtree

S.M.NiazArifin,PhD

Insert

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 23: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  startattheroot,60>25–  searchinrightsubtree

2.   60>50,searchin50’srightsubtree

S.M.NiazArifin,PhD

Insert

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 24: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  startattheroot,60>25–  searchinrightsubtree

2.   60>50,searchin50’srightsubtree3.   60<70,searchin70’sleRsubtree

S.M.NiazArifin,PhD

Insert

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 25: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

CS21, Tia Newhall

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root (1)

(2)(3)

(4)

Example: insert 60 in the tree:1. start at the root, 60 is greater than 25, search in right subtree2. 60 is greater than 50, search in 50’s right subtree3. 60 is less than 70, search in 70’s left subtree4. 60 is less than 66, add 60 as 66’s left child

60

1.  startattheroot,60>25–  searchinrightsubtree

2.   60>50,searchin50’srightsubtree3.   60<70,searchin70’sleRsubtree4.   60<66,add60as66’sleRchild

S.M.NiazArifin,PhD

Insert

Page 26: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  visiteverynodeinthetree•  threestepstoatraversal:

1.  visitthecurrentnode2.  traverseitsle)subtree3.  traverseitsrightsubtree

•  orderdefinesdifferenttraversalmethods:– pre-ordertraversal:(1)(2)(3)–  in-ordertraversal:(2)(1)(3)– post-ordertraversal:(2)(3)(1)

•  ;mecomplexity:O(logn)

S.M.NiazArifin,PhD

Traversal

Page 27: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 28: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 29: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 30: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 31: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 32: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,12,

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 33: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,12,15,

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 34: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,12,15,

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 35: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,12,15,18,

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 36: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,12,15,18,22,

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 37: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,12,15,18,22,24,•  andsoon…

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

Page 38: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  example:in-ordertraversal:(2)(1)(3)

•  4,10,12,15,18,22,24,…

S.M.NiazArifin,PhD

Traversal

CS21, Tia Newhall

Binary Search Trees (BST)1. Hierarchical data structure with a single pointer to root node 2. Each node has at most two child nodes (a left and

a right child)3. Nodes are organized by the Binary Search property:

• Every node is ordered by some key data field(s)• For every node in the tree, its key is greater than its

left child’s key and less than its right child’s key

25

15

10 22

4 12 2418

50

35 70

31 44 9066

root

sortedorder

Page 39: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  threepossiblecases:1.  dele=nganodewithnochildren2.  dele=nganodewithonechild3.  dele=nganodewithtwochildren

•  ;mecomplexity:O(logn)

S.M.NiazArifin,PhD

Delete

Page 40: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

1.  dele=nganodewithnochildren–  simplyremovethenodefromthetree

S.M.NiazArifin,PhD

Delete

10 10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

9 13

11

8

42

3

5

61

3

2 4

7

9 13

11

8

5

6 9 13

11

8

710 10

10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

13

11

9

10

18

deletenode7

Page 41: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

2. dele=nganodewithonechild–  removethenodeandreplaceitwithitschild

S.M.NiazArifin,PhD

Delete

deletenode1

10 10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

9 13

11

8

42

3

5

61

3

2 4

7

9 13

11

8

5

6 9 13

11

8

710 10

10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

13

11

9

10

18

Page 42: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

3. dele=nganodewithtwochildren•  hardercase

•  let,nodez(havingtwochildren)tobedeleted

•  iden=fyz’ssuccessor,y

•  successorofanodez–  thenodeywiththesmallestkeyintree

suchthaty:key>z:key

S.M.NiazArifin,PhD

Delete

Page 43: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

3.  dele=nganodewithtwochildren•  z’ssuccessor,y

•  yeitherisaleaforhasonlytherightchild1.  promoteytoz’splace2.  treatthelossofyusingoneoftheabovetwo

solu=ons

S.M.NiazArifin,PhD

Delete

Page 44: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

3.  dele=nganodewithtwochildren•  todelete:z=node8;

•  successor,y=node9–  smallestkeyintreewithy:key>z:key

S.M.NiazArifin,PhD

Delete

10 10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

9 13

11

8

42

3

5

61

3

2 4

7

9 13

11

8

5

6 9 13

11

8

710 10

10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

13

11

9

10

18

Page 45: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

3.  dele=nganodewithtwochildren•  todelete:z=node8;•  successor,y=node9

1.  promotenode9tonode8’splace2.  treatthelossofnode9using:•  dele=nganodewithonechild

S.M.NiazArifin,PhD

Delete

10 10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

9 13

11

8

42

3

5

61

3

2 4

7

9 13

11

8

5

6 9 13

11

8

710 10

10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

13

11

9

10

18

10 10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

9 13

11

8

42

3

5

61

3

2 4

7

9 13

11

8

5

6 9 13

11

8

710 10

10

5

61

3

2 4

7

9 13

11

8

5

61

3

2 4

13

11

9

10

18

Page 46: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  Binarytree

•  Binarysearchtree– defini=on,example– opera=ons

•  search•  maximum,minimum

•  insert•  traversal•  delete

S.M.NiazArifin,PhD

Summary

Page 47: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  Red-blacktree•  aspecialtypeofself-balancingBST•  eachnodehasanextracolorbit–  redorblack

•  colorbitsareusedtoensurethetreeremainsapproximatelybalanced– duringinser=onsanddele=ons

S.M.NiazArifin,PhD

AdvancedBinaryTrees

Page 48: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  Red-blacktree1.  therootandallleaves(NIL)areblack2.  ifanodeisred,thenbothitschildrenareblack3.  foreachnode,allsimplepathsfromthenodeto

descendantleavescontainthesamenumberofblacknodes

S.M.NiazArifin,PhD

AdvancedBinaryTrees

Page 49: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

•  AVLtree•  anotherspecialtypeofself-balancingBST•  heightsofthetwochildsubtreesofanynodedifferbyatmostone

•  ifatany=metheydifferbymorethanone,rebalancingisdonetorestorethisproperty

S.M.NiazArifin,PhD

AdvancedBinaryTrees

Page 50: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

S.M.NiazArifin,PhD

Ques=ons?

Page 51: Binary Trees - University of Notre Damesemrich/ds17/14/bst.pdf · •Binary tree • defini=on, example • types • Binary search tree • defini=on, example • operaons –

S.M.NiazArifin,PhD

ThankYou!