Download presentation
Presentation is loading. Please wait.
1
CMPT 225 Binary Search Trees
2
Trees There are no cycles in the tree.
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! yes! (but not a binary tree) yes! 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
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 A B C D G E F
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 a root 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
Tree Terminology Example
path from A to D to G C C D B D subtree rooted at B leaves: C,E,F,G E F G G E F G
9
Binary Tree A B C right child of A left subtree of A D E F 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
Height of a Binary Tree A A height of the tree is 4
height of node B is 3 B B C level 2 D E E F G level 3 depth of node E is 3 Q: how to compute height and depth of a node? H I J level 4
12
Representation of binary trees
public class 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. leftChild = left; rightChild = right; public T getItem() { // Returns the item field. return item; } // end getItem
13
public void 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 public void 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 public void setRight(TreeNode<T> right) { // Sets the right child reference to right. rightChild = right; } // end setRight } // end TreeNode
14
Representing a tree How to compute height of a node?
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)) 2 5 4 1
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
Perfect and Complete Binary Trees
G D E F A B C D E F Full binary tree Complete binary tree
17
Balanced Binary Trees A B C F D E A B C F D E G
18
Unbalanced Binary Trees
F A B C D
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(TreeNode<T> n) { if (n != null) { inOrder(n.getLeft()); visit(n) inOrder(n.getRight()); }
21
PreOrder Traversal 17 13 27 9 16 20 39 11 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 17 13 27 9 16 20 39 11 postOrder(n.leftChild) 8
postOrder(n.rightChild) 17 visit(n) 4 13 postOrder(l) postOrder(r) visit 7 27 postOrder(l) postOrder(r) visit 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
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.