trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian...

132
trees 1

Transcript of trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian...

Page 1: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

trees

1

Page 2: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

are lists enough?

for correctness — sure

want to efficiently access itemsbetter than linear time to find something

want to represent relationships more naturally

2

Page 3: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

inter-item relationships in lists

1 2 3 4 5

List: nodes related to predecessor/successor

3

Page 4: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

trees

trees: allow representing more relationships(but not arbitrary relationships — see graphs later in semester)

restriction: single path from root to every nodeimplies single path from every node to every other node (possiblythrough root)

4

Page 5: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

natural trees: phylogenetic tree

image: Ivicia Letunic and Mariana Ruiz Villarreal, via the tool iTOL (Interative Tree of Life), via Wikipedia 5

Page 6: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

natural trees: phylogenetic tree (zoom)

image: Ivicia Letunic and Mariana Ruiz Villarreal, via the tool iTOL (Interative Tree of Life), via Wikipedia 6

Page 7: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

natural trees: Indo-European languages

Kashubian

INDO-IRANIAN

IRANIAN

INDO-ARYAN

WESTERNEASTERN

WESTERN INDIC

Gandhari

CENTRAL INDIC

Maharashtri

Sindhi

Konkani

GoraniBalochi

Kurdish

Parthian

Talysh

Gilaki

Scythian

Sarmatian

Alanic

CELTIC

Manx

Irish

Cornish

Breton

GOIDELIC BRYTHONIC

Galatian

Celtiberian

TOCHARIAN

ARMENIAN

ANATOLIAN

ALBANIAN

GERMANIC

EAST

Gothic

Vandalic

Burgundian

Dutch

Afrikaans

Icelandic

Faroese

Norwegian

Norn

Swedish

Danish

Old Norse BALTO-SLAVIC

BALTIC SLAVIC

WEST EAST

SOUTH

Old West Slavic

ITALIC

Galindan

Prussian

Sudovian

Latvian

Lithuanian

Selonian

Semigallian

Belorussian

Russian

Rusyn

Bulgarian

Macedonian

Czech

SlovakPomeranian

LATINO-FALISCAN

Latin

Faliscan

SABELLIC

Classical Latin

Vulgar Latin

EASTERN

Romanian

DalmatianAromanian

ITALO-WESTERN

ITALO-DALMATIAN

IBERIAN

Italian

Astur-Leonese

Galician-Portuguese

INDO-EUROPEAN

HittiteLuwian

Lycian

Carian

Palaic

Lydian

PAHARI

Dogri Garhwali

DARDIC

Kashmiri

Pashayi

Magahi

Bhojpuri

Maithili

Oriya

Magadhi

Dhivehi

Avestan

Bactrian Sogdian

Yaghnobi

Tat

Old Persian

Persian

Tajik Juhuru

Aequian

Marsian

Oscan

Sardinian

Logudorese

Campidanese

Ecclesiastical Latin

Gaulish

Lepontic

Noric

Old West Norse Old East Norse

Standard German

Old High German

Flemish

Yiddish

Old Frisian Old English

ANGLO-FRISIAN

Scots

English

Turfanian

Kuchean

INSULAR

CONTINENTAL

Armenian

Albanian

ROMANCE

Sinhalese

Vedda

Vedic Sanskrit

Hindi Urdu

Dakhini

Rekhta

Mozarabic

Aragonese

Walloon

Emilian

Rhaetian

Friulian

HELLENIC

Aegean

MycenaeanDORIAN

Northwest Greek

Doric Attic

Arcado

Cypriot

Ionic

Epic Greek

Classical Greek

Koine Greek

Greek

Tsakonian

Low German

Cumbric

Welsh

Scottish Gaelic

Gallo

Volscian

Umbrian

Saka

Ossetian

PashtoPamiri

Bengali

Bhil

Marathi

Shina

Kumaoni

WEST

Prakrit

INSULAR INDIC

Potwari

Punjabi

Domari

Waziri

Yidgha Shughni

Vanji

Sarikoli

Khotanese

Khwarezmian

Median

Mazanderani

Shahmirzadi

CASPIAN

Zazaki

Zaza-Gorani

Middle Persian

Bukhori

Dari

Lurish

Bakhtiari

Old East Slavic

Ukrainian

Ruthenian

Old Novgorod

Old Church Slavonic

Church Slavonic

Serbo-Croatian

LECHITIC

Sorbian

Polish

Ivernic

Pictish

Common Brittonic

Hazaragi

Deilami

Greenlandic Norse

Old Gutnish

Pisidian

Old Saxon

Old Dutch

Yola

Assamese

WEST EAST

CENTRAL GERMAN

Limburgish

Old East Low Franconian

SOUTH

NORTH

Niya

Shauraseni

Nuristani

HINDUSTANI

BIHARI

ACHAEAN

AEOLIC

Beotian

Thessalian

EAST

Yazgulami

NORTHSOUTH

Nepali

Palpa

Halbi Chittagonian

NORTH CENTRAL EASTERN

Lahnda

Paisaci

Haryanvi

Luxembourgish

Ripuarian

Thuringian

Kumzari

Alemannic

Austro-Bavarian

Cimbrian

Istriot

Sassarese

Neapolitan

Sicilian

GALLO-IBERIAN

OCCITAN

GALLIC

CISALPINE

Spanish Portuguese

Galician

Catalan

OccitanLigurian

Lombard

Piedmontese

Venetian

Arpitan

Romansh

UPPER GERMAN

Swiss German

Old Polish

Romani

Gujarati

Rajasthani

Norman

French

Pali

Sanskrit

Asturian

Leonese

Mirandese

Slovene

Serbian

Croatian

Bosnian

WESTERN

EASTERN

Knaanic

Czech-Slovak

Polabian

Silesian

Fala

LadinoExtremaduran

Old Spanish

Ladin

Corsican

Istro-Romanian

Megleno-Romanian

LANGUE D'OÏL

Crimean Gothic

North Frisian

Saterland Frisian

West Frisian

Elfdalian

LOW FRANCONIAN

Eonavian

WEST

image: via Wikipedia/Mandrak 7

Page 8: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

list to tree

predecessor element successor

list — up to 2 related nodes

parent

element

left child left child

binary tree — up to 3 related nodes (list is special-case)

8

Page 9: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

more general trees

parent

element

child 1 child 2 child n…

tree — any number of relationships (binary tree is special case)at most one parent

9

Page 10: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

tree terms (1)

A

B C

E F GD

H

parent childroot: node with no parents

leafs: nodes with no children

siblings: nodes with the same parent

10

Page 11: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

paths and path lengths

A

B C

E F GD

H

path: sequence of nodes n1, n2, . . . , nk

such that ni is parent of ni+1example: {B, D, H}

length (of path): number of edges in pathexample: 2 (B → D and D → H)

internal path length: sum of depth of nodesexample: 6 = 1 + 2 + 3

11

Page 12: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

tree/node height

A

B C

E F GD

H

parent child

height (of a node): length of longest path to leaf

height (of a tree): height of tree’s root(this example: 3)

3

2

1

0

1

0 0 0

12

Page 13: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

tree/node depth

A

B C

E F GD

H

parent child

depth (of a node): length of path to root0

1

2

3

1

2 2 2

13

Page 14: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

first child/next sibling

class TreeNode {private:

string element;TreeNode *firstChild;TreeNode *nextSibling;

public:...

};

home

aaron

cs2150 cs4970nextSibling

mail

lab1

firstChild

lab2 proj1

proj.hcoll.h coll.cpp

14

Page 15: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

another tree representations

class TreeNode {private:

string element;vector<TreeNode *> children;

public:...

};

// and more --- see when we talk about graphs

15

Page 16: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

tree traversal

/

×

+

1 2

-

3 4

×

5 6

pre-order: / * + 1 2 - 3 4 * 5 6in-order: (((1+2) * (3-4)) / (5*6)) (parenthesis optional?)post-order: 1 2 + 3 4 - * 5 6 * /

16

Page 17: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

pre/post-order traversal printing

(this is pseudocode)TreeNode::printPreOrder() {

this−>print();for each child c of this:

c−>printPreOrder()}

TreeNode::printPostOrder() {for each child c of this:

c−>printPostOrder()this−>print();

}

17

Page 18: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

in-order traversal printing

(this is pseudocode)BinaryTreeNode::printInOrder() {

if (this−>left)this−>left−>printInOrder();

cout << this−>element << "␣";if (this−>right)

this−>right−>printInOrder();}

18

Page 19: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

post-order traversal counting

(this is pseudocode)int numNodes(TreeNode *tnode) {if ( tnode == NULL )

return 0;else {

sum=0;for each child c of tnode

sum += numNodes(c);return 1 + sum;

}}

19

Page 20: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

expression tree and traversals

+

a *

+

b c

d

(a + ((b + c) * d))

20

Page 21: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

expression tree and traversals

+

a *

+

b c

d

infix: (a + ((b + c) * d))postfix: a b c + d * +prefix: + a * + b c d

21

Page 22: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

postfix expression to tree

use a stack of trees

number n → push( n )

operator OP →pop into A, B; thenpush OP

AB

22

Page 23: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example

a b + c d e + * *

top of stack

a

b

+

a b

c

d

e

c

+

d e*

c +

d e

*

+

a b

*

c +

d e

23

Page 24: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example

a b + c d e + * *

top of stack

a

b

+

a b

c

d

e

c

+

d e*

c +

d e

*

+

a b

*

c +

d e

23

Page 25: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example

a b + c d e + * *

top of stack

a

b

+

a b

c

d

e

c

+

d e*

c +

d e

*

+

a b

*

c +

d e

23

Page 26: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example

a b + c d e + * *

top of stack

a

b

+

a b

c

d

e

c

+

d e*

c +

d e

*

+

a b

*

c +

d e

23

Page 27: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example

a b + c d e + * *

top of stack

a

b

+

a b

c

d

e

c

+

d e

*

c +

d e

*

+

a b

*

c +

d e

23

Page 28: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example

a b + c d e + * *

top of stack

a

b

+

a b

c

d

e

c

+

d e

*

c +

d e

*

+

a b

*

c +

d e

23

Page 29: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example

a b + c d e + * *

top of stack

a

b

+

a b

c

d

e

c

+

d e

*

c +

d e

*

+

a b

*

c +

d e

23

Page 30: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary trees

class BinaryNode {...int element;BinaryNode *left;BinaryNode *right;

};

all nodes have at most 2 children1

2

3

4

5

6

7

1

2

4 5

3

6 7

element = 2left = NULLright = addr of node 3

element = 7left = NULLright = NULL

24

Page 31: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary trees

class BinaryNode {...int element;BinaryNode *left;BinaryNode *right;

};

all nodes have at most 2 children1

2

3

4

5

6

7

1

2

4 5

3

6 7

element = 2left = NULLright = addr of node 3

element = 7left = NULLright = NULL

24

Page 32: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary trees

class BinaryNode {...int element;BinaryNode *left;BinaryNode *right;

};

all nodes have at most 2 children1

2

3

4

5

6

7

1

2

4 5

3

6 7

element = 2left = NULLright = addr of node 3

element = 7left = NULLright = NULL

24

Page 33: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary search trees

binary tree and…

each node has a keyfor each node:

keys in node’s left subtree are less than node’skeys in node’s right subtree are greater than node’s

42

1 35

76 8

left subtree of 4right subtree of 4

right subtree of 5

25

Page 34: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary search trees

binary tree and…

each node has a keyfor each node:

keys in node’s left subtree are less than node’skeys in node’s right subtree are greater than node’s

42

1 35

76 8left subtree of 4

right subtree of 4

right subtree of 5

25

Page 35: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary search trees

binary tree and…

each node has a keyfor each node:

keys in node’s left subtree are less than node’skeys in node’s right subtree are greater than node’s

42

1 35

76 8

left subtree of 4right subtree of 4

right subtree of 525

Page 36: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

not a binary search tree8

5

2

4

6

11

10

15

18

20

21

26

Page 37: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary search tree versus binary tree

binary search trees are a kind of binary tree

…but — often people say “binary tree” to mean “binary search tree”

27

Page 38: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

BST: find

(pseudocode)find(node, key) {

if (node == NULL)return NULL;

else if (key < node−>key)return find(node−>left, key)

else if (key > node−>key)return find(node−>right, key)

else // if (key == node->key)return node;

}

28

Page 39: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

BST: insert

(pseudocode)insert(Node *&node, key) {

if (node == NULL)node = new BinaryNode(key);

else if (key < node−>key)insert(node−>left, key);

else if (key < root−>key)insert(node−>right, key);

else // if (key > root->key); // duplicate -- no new node needed

}

29

Page 40: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

BST: findMin

(pseudocode)findMin(Node *node, key) {

if (node−>left == NULL)return node;

elseinsert(node−>left, key);

}

30

Page 41: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

BST: remove (1)

5

4

1

3

9

7 11

5

4

1

3

9

11

case 1: no children

31

Page 42: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

BST: remove (2)

5

4

1

3

9

7 11

5

4

3

9

7 11

case 2: one child

32

Page 43: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

BST: remove (3)

5

4

1

3

9

7 11

7

4

3

9

11

case 3: two children

replace with minimum of right subtree(alternately: maximum of left subtree, …)

33

Page 44: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary tree: worst-case height

1

2

3

4

5

6

7

n-node BST: worst-case height/depth n − 1

34

Page 45: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary tree: best-case height

4

2

1 3

6

5 7

height h: at most 2h+1 − 1 nodes

35

Page 46: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary tree: proof best-case height is possible

proof by induction: can have 2h+1 − 1 nodes in h-height tree

h = 0: h = 0: exactly one node; 2h+1 − 1 = 1 nodes

h = k → h = k + 1:start with two copies of a maximum tree of height k

create a new tree as follows:create a new root nodeadd edges from the root node to the roots of the copies

the height of this new tree is k + 1path of length k in old tree + either new edge

the number of nodes is2(2k+1 − 1) + 1 = 2k+1+1 − 2 + 1 = 2k+1+1 − 1

36

Page 47: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary tree: best-case height is best

(informally)

property of trees in root:except for the leaves, every node in tree has 2 children

no way to add nodes without increasing heightadd below leaf — longer path to root — longer heightadd above root — every old node has longer path to root

37

Page 48: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

binary tree height formula

n: number of nodes

h: height

n + 1 ≤ 2h+1

log2(n + 1) ≤ log2(2h+1)

log(n + 1) ≤ h + 1h ≥ log2 (n + 1) − 1

shortest tree of n nodes: ∼ log2(n) height38

Page 49: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

perfect binary trees

4

2

1 3

6

5 7a binary tree is perfect if

all leaves have same depthall nodes have zero children (leaf) or two children

exactly the trees that achieve 2h+1 − 1 nodes

39

Page 50: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL animation tool

http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.html

40

Page 51: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL tree idea

AVL trees: one of many balanced trees —search tree balanced to keep height Θ(log n)avoid “tree is just a long linked list” scenarios

gaurentees Θ(log n) for find, insert, remove

AVL = Adelson-Velskii and Landis

41

Page 52: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL gaurentee

the height of the left and right subtrees of every node differs by atmost one

42

Page 53: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL state

normal binary search tree stuff:data; and left, right, parent pointers

additional AVL stuff:height of right subtree minus height of left subtree

called “balance factor”-1, 0, +1

(kept up to date on insert/delete — computing on demand is too slow)

43

Page 54: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example AVL tree

5

4

1

9

7

8

11

44

Page 55: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example AVL tree

5b: +1

4b: -1

1b: 0

9b: -1

7b: +1

8b: 0

11b: 0

44

Page 56: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example non-AVL tree

5b: -2

4b: -3

1b: +2

3b: -1

2b: 0

8b: 0

7b: 0

11b: 0

45

Page 57: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL tree algorithms

find — exactly the same as binary search treejust ignore balance factors

insert — two extra steps:update balance factors“fix” tree if it became unbalanced

runtime for both Θ(d) where d is depth of node found/insertedmax balance factor ±1 at rootmax depth of node is Θ(log2 n + 1) = Θ(log n)

46

Page 58: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL tree algorithms

find — exactly the same as binary search treejust ignore balance factors

insert — two extra steps:update balance factors“fix” tree if it became unbalanced

runtime for both Θ(d) where d is depth of node found/insertedmax balance factor ±1 at rootmax depth of node is Θ(log2 n + 1) = Θ(log n)

46

Page 59: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL insertion cases

simple case: tree remains balanced

otherwise:let x be deepest imbalanced node (+2/-2 balance factor)

insert in left subtree of left child of x: single rotation rightinsert in right subtree of right child of x: single rotation leftinsert in right subtree of left child of x: double left-right rotationinsert in left subtree of right child of x: double right-left rotation

47

Page 60: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL: simple right rotation

just inserted 0unbalanced root becomes new left child

3b: -2

2b: -1

1b: 0

2b: 0

1b: 0

3b: 0

48

Page 61: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL: less simple right rotation (1)

just inserted 0unbalanced root becomes new left child

5b: -2

3b: -1

2b: -1

1b: 0

4b: 0

10b: 0

3b: 0

2b: -1

1b: 0

5b: 0

4b: 0

10b: 0

49

Page 62: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL: simple left rotation

just inserted 1deepest unbalanced node is 3

1b: +2

2b: +1

3b: 0

2b: 0

1b: 0

3b: 0

50

Page 63: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL rotation: up and down

at least one node moves up (this case: 1 and 2)at least one node moves down (this case: 3)

3b: -2

2b: -1

1b: 0

2b: 0

1b: 0

3b: 0

51

Page 64: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL: less simple right rotation (2)

just inserted 115

b: -2

5b: -2

3b: -1

2b: -1

1b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

15b: -1

3b: 0

2b: -1

1b: 0

5b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

deepest unbalanced subtree

52

Page 65: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL: less simple right rotation (2)

just inserted 115

b: -2

5b: -2

3b: -1

2b: -1

1b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

15b: -1

3b: 0

2b: -1

1b: 0

5b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

deepest unbalanced subtree

52

Page 66: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL: less simple right rotation (2)

just inserted 115

b: -2

5b: -2

3b: -1

2b: -1

1b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

15b: -1

3b: 0

2b: -1

1b: 0

5b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

deepest unbalanced subtree

52

Page 67: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL: less simple right rotation (2)

just inserted 115

b: -2

5b: -2

3b: -1

2b: -1

1b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

15b: -1

3b: 0

2b: -1

1b: 0

5b: 0

4b: 0

10b: 0

20b: 0

17b: 0

21b: 0

deepest unbalanced subtree

52

Page 68: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

general single rotationa

b

X(h+1)

Y(h)

Z(h)

b

X(h+1)

a

Y(h)

Z(h)

rotateright

rotateleft

X < b < Y < a < Z

53

Page 69: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

double rotation

15b: -2

8b: -2

4b: 1

3b: 0

6b: -1

5b: 0

10b: 0

17b: -1

16b: 0

15b: -1

6b: 0

4b: 0

3b: 0

5b: 0

8b: 1

10b: 0

17b: -1

16b: 0

step 1: rotate subtree leftstep 2: rotate imbalanced tree right

8b: 8

6b: -1

4b: -1

3b: 0

5b: 0

10b: 0

54

Page 70: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

double rotation

15b: -2

8b: -2

4b: 1

3b: 0

6b: -1

5b: 0

10b: 0

17b: -1

16b: 0

15b: -1

6b: 0

4b: 0

3b: 0

5b: 0

8b: 1

10b: 0

17b: -1

16b: 0

step 1: rotate subtree leftstep 2: rotate imbalanced tree right

8b: 8

6b: -1

4b: -1

3b: 0

5b: 0

10b: 0

54

Page 71: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

double rotation

15b: -2

8b: -2

4b: 1

3b: 0

6b: -1

5b: 0

10b: 0

17b: -1

16b: 0

15b: -1

6b: 0

4b: 0

3b: 0

5b: 0

8b: 1

10b: 0

17b: -1

16b: 0

step 1: rotate subtree leftstep 2: rotate imbalanced tree right

8b: 8

6b: -1

4b: -1

3b: 0

5b: 0

10b: 0

54

Page 72: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

double rotation

15b: -2

8b: -2

4b: 1

3b: 0

6b: -1

5b: 0

10b: 0

17b: -1

16b: 0

15b: -1

6b: 0

4b: 0

3b: 0

5b: 0

8b: 1

10b: 0

17b: -1

16b: 0

step 1: rotate subtree leftstep 2: rotate imbalanced tree right

8b: 8

6b: -1

4b: -1

3b: 0

5b: 0

10b: 0

54

Page 73: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

general double rotation

a

b

W(h)

c

X(h)

Y(h-1)

Z(h)

c

b

W(h)

X(h)

a

Y(h-1) Z

(h)

rotate

W < b < X < c < Y < Z

c becomes root, so its childrenX and Y both switch parents

55

Page 74: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

general double rotation

a

b

W(h)

c

X(h)

Y(h-1)

Z(h)

c

b

W(h)

X(h)

a

Y(h-1) Z

(h)

rotate

W < b < X < c < Y < Z

c becomes root, so its childrenX and Y both switch parents

55

Page 75: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

double rotation names

sometimes “double left”first rotation left, or second?

us: “double left-right”rotate child tree leftrotate parent tree right

“double right-left”rotate child tree rightrotate parent tree left

56

Page 76: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL insertion cases

simple case: tree remains balanced

otherwise:let x be deepest imbalanced node (+2/-2 balance factor)

insert in left subtree of left child of x: single rotation rightinsert in right subtree of right child of x: single rotation leftinsert in right subtree of left child of x: double left-right rotationinsert in left subtree of right child of x: double right-left rotation

57

Page 77: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL insert cases (revisited)

3b: -2

2b: -1

1b: 0

1b: +2

2b: +1

3b: 0

3b: -2

1b: +1

2b: 0

1b: +2

3b: -1

2b: 0

single left single right left-right right-left

choose rotation based on lowest imbalanced nodeand on direction of insertion(inserted node is green+dashed)

58

Page 78: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL insert cases (revisited)

3b: -2

2b: -1

1b: 0

1b: +2

2b: +1

3b: 0

3b: -2

1b: +1

2b: 0

1b: +2

3b: -1

2b: 0

single left single right left-right right-leftchoose rotation based on lowest imbalanced nodeand on direction of insertion(inserted node is green+dashed) 58

Page 79: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL insert case: detail (1)

3b: -2

2b: -1

1b: 0

7b: -2

3b: -2

2b: -1

1b: 0

9b: 0

choose rotation based onlowest imbalanced nodeand on direction of insertion(inserted node is green+dashed)

59

Page 80: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL insert case: detail (2)

3b: -2

2b: -1

0b: 0

7b: -2

4b: -1

2b: -1

1b: -1

0b: 0

3b: 0

5b: +1

6b: 0

8b: 0 choose using

lowest imbalanced nodeand on direction of insertion(inserted node isgreen+dashed)

60

Page 81: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

61

Page 82: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AVL tree: runtime

worst depth of node: Θ(log2 n + 2) = Θ(log n)find: Θ(log n)

worst case: traverse from root to worst depth leaf

insert: Θ(log n)worst case: traverse from root to worst depth leafthen back up (update balance factors)then perform constant time rotation

remove: Θ(log n)left as exercise (similar to insert)

print: Θ(n)visit each of n nodes

62

Page 83: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

other types of trees

many kinds of balanced trees

not all binary trees

different ways of tracking balance factors, etc.

different ways of doing tree rotations or equivalent

63

Page 84: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

red-black trees

each node is red or blacknull leafs considered nodes to aid analysis (still null pointers…)rules about when nodes can be red/black gaurentee maximum depth

13

8

1

nNULL 6

nNULL nNULL

11

nNULL nNULL

17

15

nNULL nNULL

25

22

nNULL nNULL

27

nNULL nNULL 64

Page 85: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

red-black tree rules

root is black

counting null pointers as nodes, leaves are black

a red node’s children are black→ a red node’s parents are black

every simple path from node to leaf under it contains same numberof black nodes

(property holds regardless of whether null pointers are considered nodes)

65

Page 86: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

worst red-black tree imbalance

same number of black nodes on paths to leaves→ factor of 2 imbalance max

A

B

D E

C

F

H

J K

I

L M

G

N O

66

Page 87: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

red-black insert

default: insert as red (no change to black node count), but…

(1) if new node is root: color black

(2) if parent is black: keep child red

(3) if parent and uncle is red: adjust several colors

(4) if parent is red, uncle is black, new node is right childperform a rotation, then go to case 5

(5) if parent is red, uncle is black, new node is left childperform a rotation

property: “root is black”no children → no worries about # black nodeson different pathsproperty: “children of red node are black”no change in # of black nodes on paths

67

Page 88: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

red-black insert

default: insert as red (no change to black node count), but…

(1) if new node is root: color black

(2) if parent is black: keep child red

(3) if parent and uncle is red: adjust several colors

(4) if parent is red, uncle is black, new node is right childperform a rotation, then go to case 5

(5) if parent is red, uncle is black, new node is left childperform a rotation

property: “root is black”no children → no worries about # black nodeson different paths

property: “children of red node are black”no change in # of black nodes on paths

68

Page 89: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

red-black insert

default: insert as red (no change to black node count), but…

(1) if new node is root: color black

(2) if parent is black: keep child red

(3) if parent and uncle is red: adjust several colors

(4) if parent is red, uncle is black, new node is right childperform a rotation, then go to case 5

(5) if parent is red, uncle is black, new node is left childperform a rotation

property: “root is black”no children → no worries about # black nodeson different pathsproperty: “children of red node are black”no change in # of black nodes on paths

69

Page 90: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

case 3: parent, uncle are red

G

P U

N

1 2

3 4 5

G

P U

N

1 2

3 4 5

make grandparent red, parent and uncle black(property: every path to leaf has same number of black nodes)just swapped grandparent and parent/uncle in those paths

but…what if grandparent’s parent is red?(property: children of red node are black)solution: recurse to the grandparent, as if it was just inserted

image: Wikipedia/Abloomfi 70

Page 91: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

case 3: parent, uncle are red

G

P U

N

1 2

3 4 5

G

P U

N

1 2

3 4 5

make grandparent red, parent and uncle black(property: every path to leaf has same number of black nodes)just swapped grandparent and parent/uncle in those paths

but…what if grandparent’s parent is red?(property: children of red node are black)solution: recurse to the grandparent, as if it was just inserted

image: Wikipedia/Abloomfi 70

Page 92: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

case 3: parent, uncle are red

G

P U

N

1 2

3 4 5

G

P U

N

1 2

3 4 5

make grandparent red, parent and uncle black(property: every path to leaf has same number of black nodes)just swapped grandparent and parent/uncle in those paths

but…what if grandparent’s parent is red?(property: children of red node are black)solution: recurse to the grandparent, as if it was just inserted

image: Wikipedia/Abloomfi 70

Page 93: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

red-black insert

default: insert as red (no change to black node count), but…

(1) if new node is root: color black

(2) if parent is black: keep child red

(3) if parent and uncle is red: adjust several colors

(4) if parent is red, uncle is black, new node is right childperform a rotation, then go to case 5

(5) if parent is red, uncle is black, new node is left childperform a rotation

property: “root is black”no children → no worries about # black nodeson different pathsproperty: “children of red node are black”no change in # of black nodes on paths

71

Page 94: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

case 4: parent red, uncle black, right child

G

P U

N1

2 3

4 5

G

UN

P

1 2

3 4 5

perform left rotation on parent subtree and new node

now case 5 (but new node is P , not N)

image: Wikipedia/Abloomfi 72

Page 95: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

red-black insert

default: insert as red (no change to black node count), but…

(1) if new node is root: color black

(2) if parent is black: keep child red

(3) if parent and uncle is red: adjust several colors

(4) if parent is red, uncle is black, new node is right childperform a rotation, then go to case 5

(5) if parent is red, uncle is black, new node is left childperform a rotation

property: “root is black”no children → no worries about # black nodeson different pathsproperty: “children of red node are black”no change in # of black nodes on paths

73

Page 96: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

case 5: parent red, uncle black, left child

G

P U

N

1 2

3 4 5

G

U

P

N

1 2 3

4 5

perform right rotation of grandparent and parentswap colors of parent and grandparentpreserves properties:

red parent’s children are blackevery path to leaf has same number of black nodes

image: Wikipedia/Abloomfi 74

Page 97: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example recursive case

100

10

3

1 5

8

15

200

10

3

1 5

8

100

15 200

initially:leaves are black Xred node’s children are black Xsame number of black nodesin every path from node to leaves X

insert 8initially make redcase 3: parent, uncle are red

3

1 5

8

before:

case 3: parent, uncle are red:grandparent becomes redparent/uncle black

case 3 (parent, uncle are red) continued:recusively examine grandparent 3case 5: parent (of 3) is red

uncle is black, left child

100

10

3

… …

15

200

case 5: parent is reduncle is black, left child:

perform right rotationof parent + grandparent (of 3)(and swap parent/grandparent colors)

75

Page 98: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example recursive case

100

10

3

1 5

8

15

200

10

3

1 5

8

100

15 200

initially:leaves are black Xred node’s children are black Xsame number of black nodesin every path from node to leaves X

insert 8initially make redcase 3: parent, uncle are red

3

1 5

8

before:

case 3: parent, uncle are red:grandparent becomes redparent/uncle black

case 3 (parent, uncle are red) continued:recusively examine grandparent 3case 5: parent (of 3) is red

uncle is black, left child

100

10

3

… …

15

200

case 5: parent is reduncle is black, left child:

perform right rotationof parent + grandparent (of 3)(and swap parent/grandparent colors)

75

Page 99: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example recursive case

100

10

3

1 5

8

15

200

10

3

1 5

8

100

15 200

initially:leaves are black Xred node’s children are black Xsame number of black nodesin every path from node to leaves X

insert 8initially make redcase 3: parent, uncle are red

3

1 5

8

before:

case 3: parent, uncle are red:grandparent becomes redparent/uncle black

case 3 (parent, uncle are red) continued:recusively examine grandparent 3case 5: parent (of 3) is red

uncle is black, left child

100

10

3

… …

15

200

case 5: parent is reduncle is black, left child:

perform right rotationof parent + grandparent (of 3)(and swap parent/grandparent colors)

75

Page 100: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example recursive case

100

10

3

1 5

8

15

200

10

3

1 5

8

100

15 200

initially:leaves are black Xred node’s children are black Xsame number of black nodesin every path from node to leaves X

insert 8initially make redcase 3: parent, uncle are red

3

1 5

8

before:

case 3: parent, uncle are red:grandparent becomes redparent/uncle black

case 3 (parent, uncle are red) continued:recusively examine grandparent 3case 5: parent (of 3) is red

uncle is black, left child

100

10

3

… …

15

200

case 5: parent is reduncle is black, left child:

perform right rotationof parent + grandparent (of 3)(and swap parent/grandparent colors)

75

Page 101: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example recursive case

100

10

3

1 5

8

15

200

10

3

1 5

8

100

15 200

initially:leaves are black Xred node’s children are black Xsame number of black nodesin every path from node to leaves X

insert 8initially make redcase 3: parent, uncle are red

3

1 5

8

before:

case 3: parent, uncle are red:grandparent becomes redparent/uncle black

case 3 (parent, uncle are red) continued:recusively examine grandparent 3case 5: parent (of 3) is red

uncle is black, left child

100

10

3

… …

15

200

case 5: parent is reduncle is black, left child:

perform right rotationof parent + grandparent (of 3)(and swap parent/grandparent colors)

75

Page 102: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

example recursive case

100

10

3

1 5

8

15

200

10

3

1 5

8

100

15 200

initially:leaves are black Xred node’s children are black Xsame number of black nodesin every path from node to leaves X

insert 8initially make redcase 3: parent, uncle are red

3

1 5

8

before:

case 3: parent, uncle are red:grandparent becomes redparent/uncle black

case 3 (parent, uncle are red) continued:recusively examine grandparent 3case 5: parent (of 3) is red

uncle is black, left child

100

10

3

… …

15

200

case 5: parent is reduncle is black, left child:

perform right rotationof parent + grandparent (of 3)(and swap parent/grandparent colors)

75

Page 103: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

RB-tree: removal

start with normal BST remove of x, but…

instead find next highest/lowest node ycan choose node with at most one child(“bottom” of a left or right subtree)

swap x and y’s value, then replace y with its child

several cases for color maintainence/rotations

76

Page 104: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

RB tree: removal cases

N: node just replaced with child; S: its sibling; P: its parent

(1): N is new root(2): S is red(3): P, S, and S’s children are black(4): S and S’s children are black(5): S is black, S’s left child is red, S’s right child is black, N isleft child of P(6): S is black, S’s right child is red, N is left child

details: see, e.g., Wikipedia article 77

Page 105: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

why red-black trees?

a lot more cases…but

a lot less rotations

…because tree is kept less rigidly balanced

red-black trees end up being faster in practice

78

Page 106: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

more balanced trees

several other kinds of balanced trees

one notable kind: non-binary balanced trees

commonly used in databasesmore efficient to store multiple nodes together on disk/SSD

79

Page 107: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

splay trees

tree that’s fast for recently used nodes

self-balancing binary search tree

keeps recent nodes near the top

simpler to implement than AVL or RB trees

80

Page 108: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

‘splaying’

every time node is accessed (find, insert, delete)…

“splay” tree around that node

make the node the new tree root

Θ(h) time — where h is tree height

worst-case height: Θ(n) — linked-list case

81

Page 109: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

‘splaying’

every time node is accessed (find, insert, delete)…

“splay” tree around that node

make the node the new tree root

Θ(h) time — where h is tree height

worst-case height: Θ(n) — linked-list case

81

Page 110: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

‘splaying’

every time node is accessed (find, insert, delete)…

“splay” tree around that node

make the node the new tree root

Θ(h) time — where h is tree heightworst-case height: Θ(n) — linked-list case

81

Page 111: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

splay tree operations

3

2

1

4

3

2

1

2

1 3

4

insert 4 find 2

82

Page 112: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

amortized complexity

splay tree insert/find/delete is amortized O(log n) time

informally: average insert/find/delete: O(log n)

more formally: m operations: O(m log n) time (where n: max sizeof tree)

83

Page 113: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

splay tree pro/con

can be faster than AVL, RB-trees in practicetake advantage of frequently accessed items

simpler to implement

but worst case find/insert is Θ(n) time

84

Page 114: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

last time

red-black treesless well-balanced than AVL treestrack color instead of balance factorrules about colors to limit possible imbalancealgorithm for insertion, etc. that makes tree always obey rulesusually faster in practice — less rotations

splay treesoptimized for repeated accesseskeep recently accessed items near top of treefind rearranges tree!amortized logarithmic time(but worst case is linear)

85

Page 115: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

amortized analysis: vector growth

vector insert algorithm:if not big enough, double capacitywrite to end of vector

doubling size — requires copying! — Θ(n) time

Θ(n) worst case per insert

but average…?

86

Page 116: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

amortized analysis: vector growth

vector insert algorithm:if not big enough, double capacitywrite to end of vector

doubling size — requires copying! — Θ(n) time

Θ(n) worst case per insert

but average…?

86

Page 117: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

counting copies (1)

suppose initial capacity 100 + insert 1600 elements100 → 200: 100 copies200 → 400: 200 copies400 → 800: 400 copies800 → 1600: 800 copiestotal: 1500 copies

total operations: 1500 copies + 1600 writes of new elementsabout 2 operations per insert

87

Page 118: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

counting copies (2)

more generally: for N inserts

about N copies + N writeswhy? K to 2K elements: K copiesN inserts: 1 + 2 + 4 + . . . + N/4 + N/2 = N − 1 copies(and a bit better if initial capacity isn’t 1)

Θ(n) worst case

but Θ(n) time for n inserts

→ O(1) amortized time per insert

88

Page 119: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

counting copies (2)

more generally: for N inserts

about N copies + N writeswhy? K to 2K elements: K copiesN inserts: 1 + 2 + 4 + . . . + N/4 + N/2 = N − 1 copies(and a bit better if initial capacity isn’t 1)

Θ(n) worst case

but Θ(n) time for n inserts

→ O(1) amortized time per insert

88

Page 120: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

other vector capacity increases? (1)

instead of doubling…add 1000

N inserts: 1000 + 2000 + 3000 + . . . + N ∼ N2

→ Θ(N2) total — O(N) amortized time per insert

increase by constant: linear worst-case and amortized

89

Page 121: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

other vector capacity increases? (1)

instead of doubling…add 1000

N inserts: 1000 + 2000 + 3000 + . . . + N ∼ N2

→ Θ(N2) total — O(N) amortized time per insert

increase by constant: linear worst-case and amortized

89

Page 122: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

instead of doubling…multiply by k > 1e.g. k = 1.1 — increase by 10%

N inserts: 1 + k + k2 + k3 + . . . + klogk N = 1 − klogk N

1 − k∼ N

→ Θ(N) total — O(1) amortized time per insertamortized constant time for all k > 1

90

Page 123: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

instead of doubling…multiply by k > 1

e.g. k = 1.1 — increase by 10%

N inserts: 1 + k + k2 + k3 + . . . + klogk N = 1 − klogk N

1 − k∼ N

→ Θ(N) total — O(1) amortized time per insertamortized constant time for all k > 1

90

Page 124: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

instead of doubling…multiply by k > 1

e.g. k = 1.1 — increase by 10%

N inserts: 1 + k + k2 + k3 + . . . + klogk N = 1 − klogk N

1 − k∼ N

→ Θ(N) total — O(1) amortized time per insertamortized constant time for all k > 1

90

Page 125: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

trees are not great for…

ordered, unsorted lists

list of TODO tasks

being easy/simple to implement

compare, e.g., stack/queue

Θ(1) time

compare vector

compare hashtables (almost)

91

Page 126: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

programs as trees

int z;

int foo (int x) {for (int y = 0;

y < x;y++)

cout << y << endl;}

int main() {int z = 5;cout << "enter x" << endl;cin >> z;foo(z);

}

program

vars

int z

functions

foo()

params

int z

vars body

for

int y y < x y++ body

cout

y endl

main()

params vars

int z

=5

body

cout

"enter z" endl

cin

z

foo()

z

92

Page 127: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

programs as trees

int z;

int foo (int x) {for (int y = 0;

y < x;y++)

cout << y << endl;}

int main() {int z = 5;cout << "enter x" << endl;cin >> z;foo(z);

}

program

vars

int z

functions

foo()

params

int z

vars body

for

int y y < x y++ body

cout

y endl

main()

params vars

int z

=5

body

cout

"enter z" endl

cin

z

foo()

z

92

Page 128: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

abstract syntax tree

program

vars

int z

functions

foo()

params

int z

vars body

for

int y y < x y++ body

couty endl

main()

params vars

int z

=5

body

cout

"enter z" endl

cin

z

foo()

z

for loop: four childreninit, condition, update, body

class ASTNode {...

};

// public class ForNode extends ASTNodeclass ForNode : public ASTNode {

...private:

ASTNode *init, *condition,*update, *body;

};

93

Page 129: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

abstract syntax treeprogram

vars

int z

functions

foo()

params

int z

vars body

for

int y y < x y++ body

couty endl

main()

params vars

int z

=5

body

cout

"enter z" endl

cin

z

foo()

z

for loop: four childreninit, condition, update, body

class ASTNode {...

};

// public class ForNode extends ASTNodeclass ForNode : public ASTNode {

...private:

ASTNode *init, *condition,*update, *body;

};

93

Page 130: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

abstract syntax treeprogram

vars

int z

functions

foo()

params

int z

vars body

for

int y y < x y++ body

couty endl

main()

params vars

int z

=5

body

cout

"enter z" endl

cin

z

foo()

z

for loop: four childreninit, condition, update, body

class ASTNode {...

};

// public class ForNode extends ASTNodeclass ForNode : public ASTNode {

...private:

ASTNode *init, *condition,*update, *body;

};

93

Page 131: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

AST applications

“abstract syntax tree” = “parse tree”

part of how compilers work

do some tree traversal to do…

code generation — e.g. ASTNode::outputCode() method

optimization

type checking…

94

Page 132: trees · 2018. 10. 3. · Common Brittonic Hazaragi Deilami Greenlandic Norse Old Gutnish Pisidian Old Saxon Old Dutch Yola Assamese WEST EAST CENTRAL GERMAN Limburgish Old East Low

using AST to compare programs

comparing trees is a good way to compare programs…while ignoring:

function/method order (e.g. sort function nodes by length)

variable names (e.g. ignore variable names when comparing)

comments

part of many software plagerism/copy+paste detection tools95