1 / 17

CSCI 143

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...

bly
Download Presentation

CSCI 143

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 143 Binary Trees

  2. Data Structures • Organize data • Process data • Insertion • Deletion • Searches • Types • Arrays • ArrayLists • LinkedLists • Stacks • Queues • Binary Trees

  3. 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!

  4. 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

  5. A C B F E D I G H Is This a Binary Tree?

  6. 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

  7. 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

  8. 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

  9. Node Code • A node consists of • A left link • Data • A right link • Data members? protected class BinaryTreeNode { DataElement data; BinaryTreeNode lLink; BinaryTreeNode rLink; }

  10. 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

  11. 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?

  12. 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

  13. Key Operations • isEmpty() • insert() • search() • remove() • numNodes() • height() • numLeaves()

  14. 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?

  15. 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?

  16. 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

  17. 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

More Related