Download presentation
Presentation is loading. Please wait.
Published byAleesha White Modified over 9 years ago
1
1 Joe Meehean
2
A A B B D D I I C C E E X X
3
A A B B D D I I C C E E X X Terminology each circle is a node pointers are edges topmost node is the root bottom nodes are leaves no outgoing edges Every non-empty tree has one root one or more leaves
4
Node A is the parent of node B Node B is the child of node A The root has no parent All other nodes have exactly 1 parent A A B B
5
Not a tree D has 2 parents A A B B D D C C
6
Path is a sequence of connected nodes Length of a path is the number of edges in the path A to D is 2 C to F is 1 X is 0 A A B B D D C C F F X X Tree 1 Tree 2 Tree 3
7
Height of a tree: length of its longest path from root to leaf Ex: Height of 2 A A B B D D C C E E
8
Depth of a node: length of path from root Example A: 0 B: 1 E: 2 D: 2 C: 1 A A B B D D C C E E
9
Subtrees of a node are the nodes rooted at a nodes children As subtrees rooted at B rooted at C A A B B D D C C E E
10
Special Tree No node has more than 2 children can have 0,1, or 2 Each child is either “right” or “left” A A B B D D C C E E
11
A A B B D D C C E E X X Y Y Z Z S S T T
12
template class BinaryTreenode { private: D data_; BinaryTreenode * left_; BinaryTreenode * right_; public: BinaryTreenode(D d = D(), BinaryTreenode * left = NULL, BinaryTreenode * right = NULL); };
13
Iterate through all nodes each node visited once, to… print all values see if a node has some property modify the node, etc… 4 common orders for visiting nodes preorder postorder in order (binary trees only) level order
14
Depth-first traversal Visit the root first Recursive definition visit the root do a preorder traversal of each subtree, left to right Ex: A B E D C F A A B B D D C C E E F F 1 2 34 5 6
15
Depth-first traversal Visit the root last Recursive definition do a postorder traversal of each subtree, left to right visit the root Ex: E D B F C A A A B B D D C C E E F F 6 3 12 5 4
16
Depth-first traversal For binary trees only Visit root in between subtrees Recursive definition in-order traversal of left subtree visit the root in-order traversal of right subtree Ex: E B D A F C A A B B D D C C E E F F 4 2 13 6 5
17
Visit all nodes at level 1 (depth 1) then level 2, level 3, etc… always left to right (or some order) Use a queue instead of recursion (implicitly uses a stack) q.push(root); while( !q.empty() ){ //dequeue node n //visit n //enqueue all of n’s children(L to R) }
18
Special kind of binary tree Each node stores a key sometimes an associated value For each node n all keys in n’s left subtree are < key at n all keys in n’s right subtree are > key at n if duplicate keys allowed keys that equal n can go left XOR right (not both) 18
19
Insert a key (and associated data) Lookup a key (and associated data) Remove a key (…) Print all keys in sorted using an inorder traversal 19
20
6 6 4 4 5 5 9 9 2 2 6 6 4 4 7 7 9 9 2 2 Yes In order traversal produces: 2 4 5 6 9 No: 7 is not < 6 4 2 13 5 20
21
21 6 6 4 4 5 5 9 9 3 3 lookup(2 ) 7 7 15 2 2
22
22 6 6 4 4 5 5 9 9 3 3 7 7 15 lookup(2 ) 2 2 Eliminates half the nodes at every compare
23
23 6 6 4 4 5 5 9 9 3 3 7 7 15 lookup(2 ) 2 2 Eliminates half the nodes at every compare
24
24 6 6 4 4 5 5 9 9 3 3 7 7 15 lookup(2 ) 2 2 Eliminates half the nodes at every compare
25
Worst-case O(height of tree) Worst of worst height is N lookup is O(N) Best worst-case height is log 2 N lookup is O(logN) O(LogN) is waaaay better than O(N) 25
26
New values inserted as leaves Must choose position to respect BST ordering and to ensure we can find it with a lookup Duplicate keys are not allowed 26
27
Traverse the tree like a lookup If we find a duplicate return an error If we end up at a null (child of a leaf) make a new node with the key make it the child of the leaf Note the above two were our base cases for lookup too 27
28
Similar to lookup worst-case follow path from root to leaf O(logN) for a balanced tree O(N) for a completely unbalanced tree 28
29
Find the node n w/ key to be deleted Different actions depending on n’s # of kids Case 1: n has 0 kids (it’s a leaf) set parent’s n-pointer (left or right) to null Case 2: n has 1 kid set parent’s n-pointer to point to n’s only kid Case 3: n has 2 kids replace n’s key with a key further down in the tree delete that node 29
30
8 8 20 16 15 18 17 … … … … delete(17) 30
31
8 8 20 16 15 18 … … … … 31 delete(17) 17
32
8 8 20 16 15 18 17 … … … … delete(16) 32
33
16 8 8 20 15 18 17 … … … … delete(16) 33
34
8 8 20 16 15 18 17 … … … … Smallest value in right subtree delete(15) 34
35
8 8 20 16 18 17 … … … … 35 Case 2: 1 kid Replace 16 with it’s only child delete(15)
36
8 8 20 16 18 17 … … … … 36 Case 2: 1 kid Replace 16 with it’s only child delete(15)
37
Worst-case (delete root) Root to leaf H = Height of Tree O(H) Balanced tree O(logN) where N is keys in tree O(N) for a completely unbalanced tree 37
38
DSW algorithm transform BST into a linked list transform linked list into balanced BST uses tree rotations Tree rotation rotates a part of the tree makes a child the parent makes a parent the child preserves BST property 38
39
Left-child tree rotation: (4 & 6) 39 6 6 4 4 5 5 9 9 2 2
40
Left-child tree rotation: (4 & 6) 40 6 6 4 4 5 5 9 9 2 2 6 6 4 4 5 5 9 9 2 2 Make 4 parent of 6
41
Left-child tree rotation: (4 & 6) 41 6 6 4 4 5 5 9 9 2 2 6 6 4 4 5 5 9 9 2 2 Assign 4’s abandoned child to 6.
42
General left-child tree rotation 42 A A B B X Y Z k2 k1
43
General left-child tree rotation 43 A A B B A A B B X Y Z Z X Y k2 k1 k2 k1
44
General left-child tree rotation 44 A A B B A A B B X Y Z X Y Z k2 k1 k2 k1
45
General left-child tree rotation 45 A A B B A A B B X Y Z X Y Z k2 k1 k2 k1
46
46
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.