170 likes | 331 Views
CSCI 143. Binary Trees. Data Structures. Organize data Process data Insertion Deletion Searches. Types Arrays ArrayLists LinkedLists Stacks Queues Binary Trees. Binary Tree. A binary tree is a finite set of elements that is either empty or consists of...
E N D
CSCI 143 Binary Trees
Data Structures • Organize data • Process data • Insertion • Deletion • Searches • Types • Arrays • ArrayLists • LinkedLists • Stacks • Queues • Binary Trees
Binary Tree • A binary tree is a finite set of elements that is either empty or consists of... • A single element called the root of the tree • Two subtrees, called left and right • A left or right subtree can be empty • A subtree can only have one parent • Note: This definition is recursive!
edge/branch root A B C E D F G H I node leaf leaf Terminology B = left child of A C = right child of A A = parent of B and C
A C B F E D I G H Is This a Binary Tree?
Level, Depth and Height • Level • the number of branches on the path from the root to a node • Depth • the maximum level of any leaf in the tree • Height • the number of nodes on the longest path from the root to a leaf Level 0 A Level 1 B C Level 2 D E Level 3 F G Height = 4 Depth = 3
Traversing a Binary Tree • Preorder 1. Visit the root 2. Traverse the left subtree 3. Traverse the right subtree ABDC • Inorder 1. Traverse the left subtree 2. Visit the root 3. Traverse the right subtree BDAC • Postorder 1. Traverse the left subtree 2. Traverse the right subtree 3. Visit the root DBCA A B C D
A B C D F E G H I Traversing a Binary Tree • Preorder 1. Visit the root 2. Traverse the left subtree 3. Traverse the right subtree ABDGCEHIF • Inorder 1. Traverse the left subtree 2. Visit the root 3. Traverse the right subtree DGBAHEICF • Postorder 1. Traverse the left subtree 2. Traverse the right subtree 3. Visit the root GDBHIEFCA
Node Code • A node consists of • A left link • Data • A right link • Data members? protected class BinaryTreeNode { DataElement data; BinaryTreeNode lLink; BinaryTreeNode rLink; }
Traversal Pseudocode (Inorder) inOrder (root node pointer) if the root node pointer is not null pass left pointer to inOrder print the data pass right pointer to inOrder
Traversal Code (Inorder) private void inorder (BinaryTreeNode p) { if (p != null) { inorder (p.lLink); System.out.print (p.data + “ “ ); inorder (p.rLink); } } How would the code for preorder and postorder traversal be different?
14 4 15 3 9 18 7 16 20 Binary Search Tree • A binary tree in which the data in each node is… • Larger than the data in its left child • Smaller than the data in its right child • All items smaller than the root are in the left subtree • All items larger than the root are in the right subtree
Key Operations • isEmpty() • insert() • search() • remove() • numNodes() • height() • numLeaves()
14 4 15 3 9 18 7 16 20 17 5 Traversing a Binary Search Tree • Preorder 1. Visit the root 2. Traverse the left subtree 3. Traverse the right subtree 14 4 3 9 7 5 15 18 16 17 20 • Inorder 1. Traverse the left subtree 2. Visit the root 3. Traverse the right subtree 3 4 5 7 9 14 15 16 17 18 20 • Postorder 1. Traverse the left subtree 2. Traverse the right subtree 3. Visit the root 3 5 7 9 4 17 16 20 18 15 14 Do you notice anything interesting about Inorder traversal?
14 4 15 3 9 18 7 16 20 17 5 Rebuilding a Tree • Preorder Listing 14 4 3 9 7 5 15 18 16 17 20 • First element is root • Second element is root of left tree • Which element is root of right tree? • Postorder Listing 3 5 7 9 4 17 16 20 18 15 14 • Last element is root • Second to last element is root of right tree • Which element is root of left tree?
Performance depends on shape Shape depends on the order in which items are added Best case: O(log2 n) Max 30 comparisons to find an item in a tree containing 1,000,000,000 items! 230 = 1,073,741,824 When would this be the case? Worst case: O(n) When would this be the case? Binary Search Tree: Analysis 14 4 15 3 9 18 7 16 20
8 5 11 2 7 9 15 14 20 Balanced Trees • A binary tree in which the height of the left subtree and right subtree differ by no more than one. • Is this tree still balanced? • Rotation is used to balance • O(log2 n) for any search