1 trees a tree is a data structure used to represent different kinds of data and help solve a number...
TRANSCRIPT
![Page 1: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/1.jpg)
1
Trees
• A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems
• Game trees (i.e., chess ), UNIX directory trees, sorting trees etc
• We will study extensively two useful kind of trees: Binary Search Trees and Heaps
![Page 2: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/2.jpg)
2
Trees: Definitions
• Trees have nodes. They also have edges that connect the nodes. Between two nodes there is always only one path.
Tree nodes
Tree edges
![Page 3: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/3.jpg)
3
Trees: More Definitions
• Trees are rooted. Once the root is defined (by the user) all nodes have a specific level.
• Trees have internal nodes and leaves. Every node (except
the root) has a parent and it also has zero or more children.
level 0
level 1
level 2
level 3
root
internal nodes
leaves
parentandchild
![Page 4: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/4.jpg)
4
Binary Trees
• A binary tree is one that each node has at most 2 children
![Page 5: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/5.jpg)
5
Binary Trees: Definitions
• Nodes in trees can contain keys (letters, numbers, etc)• Complete binary tree: the one where leaves are only in
last two bottom levels with bottom one placed as far left as possible
14
10
15128
16
7 9 11 13
18
![Page 6: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/6.jpg)
6
Binary Trees: Array Representation
• Complete Binary Trees can be represented in memory with the use of an array A so that all nodes can be accessed in O(1) time:
– Label nodes sequentially top-to-bottom and left-to-right
– Left child of A[i] is at position A[2i]– Right child of A[i] is at position A[2i + 1]– Parent of A[i] is at A[i/2]
![Page 7: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/7.jpg)
7
Binary Trees: Array Representation
14
10
15128
16
7 9 11 13
18
1
2 3
4 5 6 7
8 9 10 11
1 2 3 4 5 6 7 8 9 10 11
14 10 16 8 12 15 18 7 9 11 13Array A:
![Page 8: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/8.jpg)
8
Binary Trees: Pointer Representation
• To freely move up and down the tree we need pointers to parent (NIL for root) and pointers to children (NIL for leaves)
typedef tree_node {int key;struct tree_node *parent;struct tree_node *left, *right;
}
![Page 9: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/9.jpg)
9
Tree Traversal: InOrder
InOrder is easily described recursively:
•Visit left subtree (if there is one) In Order•Visit root•Visit right subtree (if there is one) In Order
![Page 10: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/10.jpg)
10
Another common traversal is PreOrder.It goes as deep as possible (visiting as it goes) then left to right.
More precisely (recursively):•Visit root•Visit left subtree in PreOrder•Visit right subtree in PreOrder
Tree Traversal: PreOrder
![Page 11: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/11.jpg)
11
PreOrder can also be implemented with a stack, without recursion:
Stack Spush root onto Srepeat until S is empty
v = pop Sif v is not NULL
visit vpush v’s right child onto Spush v’s left child onto S
Tree Traversal: Non-recursive
![Page 12: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/12.jpg)
12
PostOrder traversal also goes as deep as possible, but only visitsinternal nodes during backtracking.
More precisely (recursive):•Visit left subtree in PostOrder•Visit right subtree in PostOrder•Visit root
Tree Traversal: PostOrder
![Page 13: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/13.jpg)
13
Tree Traversal: LevelOrder
LevelOrder visits nodes level by level from root node:
• Can be implemented easily with a queue (how??)• We will learn this is called Breadth First Search in the data structure called graphs later in 242
![Page 14: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/14.jpg)
14
Binary Search Trees
• A Binary Search Tree (BST) is a binary tree with the following properties:
– The key of a node is always greater than the
keys of the nodes in its left subtree
– The key of a node is always smaller than the
keys of the nodes in its right subtree
![Page 15: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/15.jpg)
15
Binary Search Trees: Examples
C
A D
14
10
15118 18
16
14
10
15118
16
root
root
root
![Page 16: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/16.jpg)
16
Binary Search Trees
• BST is a tree with the following BST property:
key.left < key.parent < key.right
NOTE! When nodes of a BST are traversed by Inorder traversal the keys appear in sorted order:
inorder(root) { inorder(root.left) print(root.key) inorder(root.right)}
![Page 17: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/17.jpg)
17
Binary Search Trees: Inorder
14
10
15118 18
16
print(8)print(10)print(11)print(14)print(15)print(16)print(18)
Inorder visits and prints:
Example:
![Page 18: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/18.jpg)
18
Searching for a key in a BST
Picture BST’s Parent and Left/Right subtrees as follows:
Problem: how do you search BST for node with key x ?
L
P
R
![Page 19: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/19.jpg)
19
Searching for a key in a BST
search(root, x)
compare x to key of root:
- if x = key return - if x < key => search in L - if x > key => search in R
search L or R in the exact same way
Example:
14
10
15118
16
x=8 is ??? X=17 is ???
![Page 20: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/20.jpg)
20
Searching for a key in a BST
searchBST(root, key) /* found init to false */
if root=nil return
if root.key=key then
found=true
return root
else if key < root.key then
searchBST(root.L, key)
else
searchBST(root.R, key)
How can you rewrite searchBST non-recursively?
![Page 21: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/21.jpg)
21
Inserting a new key in a BST
How to insert a new key?
The same procedure used for search also applies: Determine the location by searching. Search will fail. Insert new key where the search failed.
Example:
10
8
5
3 4
2
9
Insert 4?
![Page 22: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/22.jpg)
22
Building a BST
Build a BST from a sequence of nodes read one a time
Example: Inserting C A B L M (in this order!)
1) Insert C
C
2) Insert A
C
A
![Page 23: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/23.jpg)
23
Building a BST
3) Insert B C
A
B
4) Insert L
5) Insert M
C
A
B
L
MC
A
B
L
![Page 24: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/24.jpg)
24
Building a BST
Is there a unique BST for letters A B C L M ?
NO! Different input sequences result in different trees
C
A
B
L
M
A
B
C
L
M
Inserting: A B C L M Inserting: C A B L M
![Page 25: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/25.jpg)
25
Sorting with a BST
Given a BST can you output its keys in sorted order?
Visit keys with Inorder: - visit left
- print root - visit right
How can you find the minimum? How can you find the maximum?
C
A
B
L
M
Example:
A B C L M
Inorder visit prints:
![Page 26: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/26.jpg)
26
Deleting from a BST
To delete node with key x first you need to search for it. Once found, apply one of the following three cases
CASE A: x is a leaf
x
delete x BST property maintained
q
r
q
r
p p
![Page 27: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/27.jpg)
27
Deleting from a BST cont.
Case B: x is interior with only one subtree
L
x
q
r
delete x L
q
r
BST property maintained
![Page 28: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/28.jpg)
28
Deleting from a BST cont.
Case C: x is interior with two subtrees
W
x
q
r
delete x
Z
t s
r
W
q
r
delete x
s Z
r
BST property maintained
t
![Page 29: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/29.jpg)
29
Deleting from a BST cont.
Case C cont: … or you can also do it like this
W
q
r
Zs
r
t
q < x < r
Q is smaller than the smaller element in Z R is larger than the largest element in W
Can you see other possible ways ?
![Page 30: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/30.jpg)
30
Complexity of Searching with BST
• What is the complexity of searchBST ?
• It depends on:– the key x– The other data– The shape of the tree (full, arbitrary)
Complexity Analysis: We are interested in best case, worst case and average case
![Page 31: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/31.jpg)
31
Complexity of Searching with BST
level 0
level 1
level 2
level 3
height (or depth) of tree = 1 + maximum_level
(1+3=4)
![Page 32: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/32.jpg)
32
Complexity of Searching with BST
If all nodes in the tree existthen it is called a full BST
If all levels are full except for the last level then it iscalled minimum-level BST
![Page 33: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/33.jpg)
33
Complexity of Searching with BST
Theorem: A full BST of height h has 2 - 1 nodes
Proof: Use induction
Inductive Basis: A tree of height 1 has one node (root)
Inductive Hypothesis: Assume that a tree of height h has
2 - 1 nodes
h
h
![Page 34: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/34.jpg)
34
Complexity of Searching with BST
Inductive step: Connect two trees of height h to make one of height h+1. You need to add one more node for the new root
root
L Rh
h+1
By inductive hypothesis the new number of nodes is
(2 - 1) + (2 -1) + 1 = 2 - 1 ……proved!h h h+1
![Page 35: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/35.jpg)
35
Complexity of Searching with BST
Theorem: For a minimum level tree of height h:
2 - 1 < # of nodes < 2 - 1hh - 1
Corollary: The tree with the smallest height with n # of nodes it has a height of h = 1 + log n
n 2 WHY?
![Page 36: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/36.jpg)
36
Therefore, for a full BST with N nodes the following holds for searchBST:
best time analysis ………… O(1)
worst time analysis ………… O(log N)
average case analysis ………… ???
We need to find what the average is!!
Complexity of Searching with BST
![Page 37: 1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),](https://reader036.vdocuments.mx/reader036/viewer/2022062409/56649ebb5503460f94bc4238/html5/thumbnails/37.jpg)
37
• To define what average means you need to:– Find out all possibilities and …– Determine the probability of each possibility …– that is, you need to find the expected value:
Complexity of Searching with BST
n
j
h
j
xxE1
jxx Pr
Possible values for the number of steps j are 1, 2, …, h as we assume that key search value is equally to occur