1 / 22

CMPT 225

CMPT 225. Binary Search Trees. Trees. A set of nodes with a single starting point called the root Each node is connected by an edge to some other node A tree is a connected graph There is a path to every node in the tree There are no cycles in the tree.

gayle
Télécharger la présentation

CMPT 225

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. CMPT 225 Binary Search Trees

  2. Trees • A set of nodes with a single starting point • called the root • Each node is connected by an edge to some other node • A tree is a connected graph • There is a path to every node in the tree • There are no cycles in the tree. • It can be proved by MI that a tree has one less edge than the number of nodes. • Usually depicted with the root at the top

  3. Is it a Tree? NO! All the nodes are not connected yes! (but not a binary tree) yes! NO! There is a cycle and an extra edge (5 nodes and 5 edges) yes! (it’s actually the same graph as the blue one) – but usually we draw tree by its “levels”

  4. Examples of trees • directory structure • family trees: • all descendants of a particular person • all ancestors born after year 1800 of a particular person • evolutionary tress (also called phylogenetic trees) • albegraic expressions

  5. Examples of trees Binary trees that represent algebraic expressions

  6. A B C D E F G Tree Relationships • If there is an edge between two nodes u and v, and u is “above” v in the tree (closer to the root), then v is said to be a child of u, and u the parent of v • A is the parent of B, C and D • This relationship can be generalized (transitively) • E and F are descendants of A • D and A are ancestors of G • B, C and D are siblings

  7. More Tree Terminology • A leaf is a node with no children • A path[a branch] is a sequence of nodes v1 … vn • where vi is a parent of vi+1 (1  i  n-1) [and v1 is aroot and vn is a leaf] • A subtree is any node in the tree along with all of its descendants • A binary tree is a tree with at most two children per node • The children are referred to as left and right (i.e., children are usually ordered) • We can also refer to left and right subtrees of a node

  8. C C A F E G G D Tree Terminology Example A path from A to D to G B D subtree rooted at B leaves: C,E,F,G E F G

  9. Binary Tree A B C right child of A left subtree of A F D E G right subtree of C H I J

  10. Measuring Trees • The height of a node v is the number of nodes on the longest path from v to a leaf • The height of the tree is the height of the root, which is the number of nodes on the longest path from the root to a leaf • The depth of a node v is the number of nodes on the path from the root to v • This is also referred to as the level of a node • Note that there are slightly different formulations of the height of a tree • Where the height of a tree is said to be the length (the number of edges) on the longest path from node to a leaf

  11. A Height of a Binary Tree height of the tree is 4 A level 2 height of node B is 3 B B C F E level 3 D E G depth of node E is 3 H I J level 4

  12. Representation of binary trees publicclass TreeNode<T> { private T item; private TreeNode<T> leftChild; private TreeNode<T> rightChild; public TreeNode(T newItem) { // Initializes tree node with item and no children (a leaf). item = newItem; leftChild = null; rightChild = null; } // end constructor public TreeNode(T newItem, TreeNode<T> left, TreeNode<T> right) { // Initializes tree node with item and // the left and right children references. item = newItem; leftChild = left; rightChild = right; } // end constructor public T getItem() { // Returns the item field. return item; } // end getItem

  13. publicvoid setItem(T newItem) { // Sets the item field to the new value newItem. item = newItem; } // end setItem public TreeNode<T> getLeft() { // Returns the reference to the left child. return leftChild; } // end getLeft publicvoid setLeft(TreeNode<T> left) { // Sets the left child reference to left. leftChild = left; } // end setLeft public TreeNode<T> getRight() { // Returns the reference to the right child. return rightChild; } // end getRight publicvoid setRight(TreeNode<T> right) { // Sets the right child reference to right. rightChild = right; } // end setRight } // end TreeNode

  14. 2 5 4 1 Representing a tree TreeNode<Integer> root=new TreeNode<Integer>(new Integer(2)); TreeNode<Integer> root.setLeft( new TreeNode<Integer>(new Integer(5), new TreeNode<Integer>(new Integer(4)), new TreeNode<Integer>(new Integer(1)))); • How to compute height of a node? • if tree is empty, height is 0 (no levels at all) • if tree has just a root, height is 1 • height(T) = 1 + max(height(left-subtree(T),height(right-subtree(T))

  15. Special types of Binary Trees • A binary tree is full if no node has only one child and if all the leaves have the same depth • A full binary tree of height h has (2h – 1) nodes, of which 2h-1 are leaves • A complete binary tree is one where • The leaves are on at most two different levels, • The second to bottom level is filled in and • The leaves on the bottom level are as far to the left as possible. • A balanced binary tree is one where • No leaf is more than a certain amount farther from the root than any other leaf, this is sometimes stated more specifically as: • The height of any node’s right subtree is at most one different from the height of its left subtree • A balanced tree’s height is sometime specified in terms of its relation to the number of nodes in the tree (see red-black trees)

  16. A A B C B C D E F F D E G Perfect and Complete Binary Trees Complete binary tree Full binary tree

  17. A A B C B C E D F D E F G Balanced Binary Trees

  18. A B C A E D B F C D Unbalanced Binary Trees

  19. Binary Tree Traversals • A traversal algorithm for a binary tree visits each node in the tree • and, typically, does something while visiting each node! • Traversal algorithms are naturally recursive • There are three traversal methods • Inorder • Preorder • Postorder

  20. InOrder Traversal Algorithm // InOrder traversal algorithm inOrder(TreeNode<T> n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight()); } }

  21. PreOrder Traversal visit(n) 1 preOrder(n.leftChild) 17 preOrder(n.rightChild) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) 2 13 6 27 3 9 visit preOrder(l) preOrder(r) 5 16 7 20 8 39 visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) visit preOrder(l) preOrder(r) 4 11 visit preOrder(l) preOrder(r)

  22. PostOrder Traversal postOrder(n.leftChild) 8 postOrder(n.rightChild) 17 visit(n) postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit 4 13 7 27 2 9 postOrder(l) postOrder(r) visit 3 16 5 20 6 39 postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit postOrder(l) postOrder(r) visit 1 11 postOrder(l) postOrder(r) visit

More Related