Top Banner
CS32 Discussion Sec.on 1B Week9 TA: Zhou Ren
31

CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Jul 31, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

CS32%Discussion%Sec.on%1B%Week9%

%TA:%Zhou%Ren%

Page 2: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Tree: Definitions"

node link (edge)

root

parent

children

leaves

siblings

H"

Page 3: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Tree: Definitions"

node link (edge)

root

parent

children

leaves

siblings

subtree

No loop! X

height H"

Page 4: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Bound on # of edges"

How many edges should there be in a tree of n nodes?"

H"

Page 5: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Binary Trees"

No node has more than 2 children (left child + right child). "

Page 6: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Binary Trees"

How many nodes can a binary tree of height h have? (one with max. # of nodes == full binary tree)"

Page 7: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Tree is a data structure!"

•  For every data structure we need to know:"–  how to insert a node,"

–  how to remove a node,"–  search for a node"

•  and (for tree only)"–  how to traverse the tree"

Page 8: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Tree is a data structure!"

•  For every data structure we need to know:"–  how to insert a node,"

–  how to remove a node,"–  search for a node"

•  and (for tree only)"–  how to traverse the tree"

struct&Node&

{&

&&&ItemType&val;&

&&&Node*&left;&

&&&Node*&right;&

};&

Page 9: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Three Methods of Traversal"void&preorder(const&Node&*node)&

{&

&&if&(node&==&NULL)&return;&

&&cout&<<&nodeB>val&<<&"&";&

&&preorder(nodeB>left);&

&&preorder(nodeB>right);&

}&

void&inorder(const&Node&*node)&

{&

&&if&(node&==&NULL)&return;&

&&inorder(nodeB>left);&

&&cout&<<&nodeB>val&<<&"&";&

&&inorder(nodeB>right);&

}&

void&postorder(const&Node&*node)&

{&

&&if&(node&==&NULL)&return;&

&&postorder(nodeB>left);&

&&postorder(nodeB>right);&

&&cout&<<&nodeB>val&<<&"&";&

}&

Page 10: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Binary Search Tree"

•  At all nodes:"–  All nodes in the left subtree have

smaller values than the current node�s value"

–  All nodes in the right subtree have larger values than the current node�s value"

•  Which traversal method should you use to:"–  print values in the increasing order?"

–  print values in the decreasing order?"

Page 11: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Insert"void&insert(Node*&&node,&ItemType&newVal)&

{&

&&&

&

&

&

&

&

&

&

&

&

&

}&&&

Page 12: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Insert"void&insert(Node*&&node,&ItemType&newVal)&{&

&&&&if&(node&==&NULL)&

&&&&{&

&&&&&&&&node&=&new&Node;&

&&&&&&&&nodeB>val&=&newVal;&

&&&&&&&&nodeB>left&=&nodeB>right&=&NULL;&

&&&&}&

&

&&&&if&(nodeB>val&>&newVal)&

&&&&&&&&insert(nodeB>left,&newVal);&

&&&&else&

&&&&&&&&insert(nodeB>right,&newVal);&

}&&&

Zhou Ren
Anything wrong here?
Page 13: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Insert"

•  Worst-case time complexity?"– as many steps as the height of the tree"–  full tree: n = 2h+1 - 1 � 2h+1 nodes"

– h � log2 n - 1"

– Roughly, it takes O(log N)."

Page 14: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Search"Node*&search(const&Node&*node,&ItemType&value)&

{&

&&&

&

&

&

&

&

&

&

&

}&&&

Page 15: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Search"Node*&search(const&Node&*node,&ItemType&value)&

{&

&&&&if&(node&==&NULL)&

&&&&&&&&return&NULL;&

&&

&&&&if&(nodeB>val&==&value)&

&&&&&&&&return&node;&

&&&&else&if&(nodeB>val&>&value)&

&&&&&&&&return&search(nodeB>left,&value);&

&&&&else&

&&&&&&&&return&search(nodeB>right,&value);&

}&&&

Page 16: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Removal"

•  A little tricky!"•  General strategy:"

–  Find a replacement."

–  Delete the node."

–  Replace."

•  Case-by-case analysis"–  Case 1: the node is a leaf (easy)"

–  Case 2: the node has one child"

–  Case 3: the node has two children""

Page 17: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Case 3"

Page 18: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Case 3"

copy

Use in-order traversal to identify these nodes

Page 19: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

findMax"

ItemType&findMax(const&Node&*node)&

{&

&&&&&

&

&

&

&

&

&

&

&

&

&

&

&

}&&&

Page 20: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

findMax"

ItemType&findMax(const&Node&*node)&

{&

&&&&if&(nodeB>left&==&NULL&&&&nodeB>right&==&NULL)&

&&&&&&&&return&nodeB>val;&

&

&&&&int&maxVal&=&nodeB>val;&

&&&&int&leftMax&=&findMax(nodeB>left);&

&&&&int&rightMax&=&findMax(nodeB>right);&

&

&&&&if&(maxVal&<&leftMax)&

&&&&&&&&maxVal&=&leftMax;&

&&&&if&(maxVal&<&rightMax)&

&&&&&&&&maxVal&=&rightMax;&

&

&&&&return&maxVal;&

}&&&

Page 21: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

findMin"

ItemType&findMin(const&Node&*node)&{&

&&&&if&(nodeB>left&==&NULL&&&&nodeB>right&==&NULL)&

&&&&&&&&return&nodeB>val;&

&

&&&&int&minVal&=&nodeB>val;&

&&&&int&leftMin&=&findMin(nodeB>left);&&&&&int&rightMin&=&findMin(nodeB>right);&&

&&&&if&(minVal&>&leftMin)&&&&&&&&&minVal&=&leftMin;&

&&&&if&(maxMin&>&rightMin)&&&&&&&&&minVal&=&rightMin;&

&

&&&&return&minVal;&

}&&&

Zhou Ren
minVal
Page 22: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

valid"

bool&valid(const&Node&*node)&

{&

&&&

&

&

&

&

&

&

&

&

&

}&&&

Page 23: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

valid"

bool&valid(const&Node&*node)&

{&

&&&&if&(node&==&NULL)&

&&&&&&&&return&true;&

&

&&&&if&(nodeB>left&!=&NULL&&&&findMax(nodeB>left)&>&nodeB>val)&

&&&&&&&&return&false;&

&

&&&&if&(nodeB>right&!=&NULL&&&&findMin(nodeB>right)&<&nodeB>val)&

&&&&&&&&return&false;&

&

&&&&return&valid(nodeB>left)&&&&valid(nodeB>right);&

}&&&

Page 24: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

treeHeight"int&treeHeight(const&Node&*node)&

{&

&&&

&

&

&

&

&

&

&

&

&

}&&&

Page 25: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

treeHeight"int&treeHeight(const&Node&*node)&

{&

&&&&if&(node&==&NULL)&

&&&&&&&&return&B1;&

&

&&&&int&leftHeight&=&treeHeight(nodeB>left);&

&&&&int&rightHeight&=&treeHeight(nodeB>right);&

&

&&&&if&(leftHeight&>&rightHeight)&

&&&&&&&&return&leftHeight&+&1;&

&&&&else&

&&&&&&&&return&rightHeight&+&1;&

}&&&

Page 26: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Hash Functions"

•  Hashing"–  Take a �key� and map it to a number"

"•  A requirement for hash function H: should return the same value

for the same key."•  A good hash function"

–  spreads out the values: two different keys are likely to result in different hash values"

–  computes each value quickly"

Hash Function H"�David Smallberg� 4531

Page 27: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Hash Table"

…"

4530"

4531"

4532"

4533"

…"

array

D"Hash func"D" 4531

Hash func"B" 4533 B"

Page 28: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Hash Table"

…"

4530"

4531"

4532"

4533"

…"

array

D"Hash func"C" 4531

B"

C"

May collide, so make a linked list!

Page 29: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Hash Table"

•  Running time"–  Insert?"–  Remove?"

–  Search?"

…"

4530"

4531"

4532"

4533"

…"

array

D"

B"

C"

May collide, so make a linked list!

Page 30: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Hash Table"

•  Running time"–  Insert? O(1)!–  Remove? O(1)!

–  Search? O(1)!

…"

4530"

4531"

4532"

4533"

…"

array

D"

B"

C"

May collide, so make a linked list!

Page 31: CS32%Discussion% Sec.on%1B% Week9%web.cs.ucla.edu/~zhou.ren/cs32/CS32_1B_week9_note.pdfBinary Trees" No node has more than 2 children (left child + right child). "

Hash Table"

•  Running time"–  Insert? O(1)!–  Remove? O(1)!

–  Search? O(1)!

•  Looks great, but what are the limitations?"

…"

4530"

4531"

4532"

4533"

…"

array

D"

B"

C"

May collide, so make a linked list!