70 likes | 236 Views
Binary Trees Terminology. A graph G = <V,E> is a collection of nodes and edges . An edge (v 1 ,v 2 ) is a pair of vertices that are directly connected.
E N D
Binary TreesTerminology • A graph G = <V,E> is a collection of nodes and edges. An edge (v1,v2) is a pair of vertices that are directly connected. • A path, P, is a sequence of vertices <v1,v2,…,vn> whereand (vi,vi+1) is an edge of G. A path <v1,v2,…,vn,v1> is a cycle. • A tree is a graph without cycles. The Root is a distinguished node of the tree. The Parent is the adjacent node that is closer to the root. A Leaf is a node without children. A child is an adjacent node that is further from the root. The level of a vertex is the length of the path from the root. • A binary tree is a tree with at most two children
Advantage of TreesAssuming Deletions exclude time for Find • Ordered Array • Insert O(N), Delete O(N), Find O(Lg N). • Unordered Array • Insert O(1), Delete O(1), Find O(N). • Linked List • Insert O(1), Delete O(1), Find O(N). • Binary Trees • Insert O(1), Delete O(1), Find O(lg N) in most cases. • Question: When would Find not take place in O(Lg N)? Note: the above insert and removed complexities don’t include the find
Tree StructureQuestion: Can a Tree be implemented using an array? typedef struct TreeNode { Key key; Data data; struct TreeNode left; struct TreeNode right; } Tree Top of the Tree: Tree root; Basic Tree Abstract Data Type Methods Node find(Key key) {} Item insert(Key key, Data data ) {} Item delete(Key key) {}
Binary Tree Find Node find( Key key) { Node *current = root, *previous; previous = null; if (current == null) return null; while(!equal(current->key, key)) { previous = current if (greater(current->key, key)) current = current->left; else current = current->right; if (current == null) return previous; } return previous } Notes -> in C is equivalent to “.” in Java Java has no equivalent to “.” in C
Binary Tree Insertion/Deletionpseudo code • Insert node Perform Find If node was found, Return false If tree was empty, Setroot = node ElseIfnode < leaf node returned by Find Link node to left child of leaf, Return true Else Link node to right child of leaf, Return true • Delete node Perform Find Ifnode was not found, Return false If node was a leaf, Unlink parent left or right pointer, Return true If node had one child, Link child to appropriate parent pointer, Return true Find and Removesuccessor Replacenode with successor in the tree, Returntrue
Other Search Tree Operations • Find Minimum: • Go left until the leaf is found • Find Maximum: • Go right until the leaf is found • Predecessor: • Go left, and the proceed right until the leaf is found • Successor: • Go right, and then proceed left until the leaf is found
Binary Tree TraversalSee Tree Example in Text • Preorder Traversal • Recursively Call until leaf is found • Recursive step • Visit, Traversal(Left), Traversal(Right) • Inorder (Traversal(left), Visit, Traversal(right)) • Postorder (Traversal(left), Traversal(right), Visit)