Presentation is loading. Please wait.

Presentation is loading. Please wait.

Gordon College Prof. Brinton

Similar presentations


Presentation on theme: "Gordon College Prof. Brinton"— Presentation transcript:

1 Gordon College Prof. Brinton
Binary Trees Gordon College Prof. Brinton

2 Good O’ Linear Search Collection of data items to be searched is organized in a list x1, x2, … xn Assume = = and < operators defined for the type Linear search begins with item 1 continue through the list until target found or reach end of list

3 Linear Search Vector based search function
template <typename t> void LinearSearch (const vector<t> &v, const t &item, boolean &found, int &loc) { found = false; loc = 0; for ( ; ; ) { if (found || loc == v.size()) return; if (item == x[loc]) found = true; else loc++; } }

4 In both cases, the worst case computing time is still O(n)
Linear Search Singly-linked list based search function template <typename t> void LinearSearch (NodePointer first, const t &item, boolean &found, int &loc) { found = false; locptr = first; for ( ; ; ) { if (found || locptr ==) return; if (item == locptr->data) found = true; else loc++; } } In both cases, the worst case computing time is still O(n)

5 Binary Search Binary search function for vector
template <typename t> void LinearSearch (const vector<t> &v, const t &item, boolean &found, int &loc) { found = false; int first = 0; last = v.size() - 1; for ( ; ; ) { if (found || first > last) return; loc = (first + last) / 2; if (item < v[loc]) last = loc + 1; else if (item > v[loc]) first = loc + 1; else /* item == v[loc] */ found = true; } }

6 Binary Search Usually outperforms a linear search Disadvantage:
- Not appropriate for linked lists (Why?) It is possible to use a linked structure which can be searched in a binary-like manner Stopped 3/15/2006

7 Binary Search Tree Consider the following ordered list of integers
Examine middle element Examine left, right sublist (maintain pointers) (Recursively) examine left, right sublists 80 66 62 49 35 28 13

8 Binary Search Tree Redraw the previous structure so that it has a treelike shape – a binary tree

9 Trees A data structure which consists of If the tree is nonempty
a finite set of elements called nodes or vertices a finite set of directed arcs which connect the nodes If the tree is nonempty one of the nodes (the root) has no incoming arc every other node can be reached by following a unique sequence of consecutive arcs Hierarchical structure

10 Trees Tree terminology Root node Interior nodes
Children of the parent (3) Siblings to each other Leaf nodes A data structure which consists of a finite set of elements called nodes or vertices a finite set of directed arcs which connect the nodes If the tree is nonempty one of the nodes (the root) has no incoming arc every other node can be reached by following a unique sequence of consecutive arcs

11 Trees More Tree Terms The depth of a node n is the length of the path from the root to the node. The height of a tree is the depth of its furthest leaf. Siblings are nodes that share the same parent node. If a path exists from node p to node q, where node p is closer to the root node than q, then p is an ancestor of q and q is a descendant of p. The size of a node is the number of descendants it has including itself. In-degree of a vertex is the number of edges arriving at that vertex. Out-degree of a vertex is the number of edges leaving that vertex. Each node in tree is the root of a Subtree Recursive? A leaf node has no children. Root is the only node in the tree with in-Degree=0.

12 Tree Levels L e v e l : L e v e l : 1 L e v e l : 2 L e v e l : 3 A H
G F E D C B L e v e l : L e v e l : 1 L e v e l : 2 Path from A to F - Length 2 LEVEL - length of the path between root and any node ( ROOT level is zero) DEPTH - level of a node HEIGHT - Max level L e v e l : 3

13 Binary Trees Each node has at most two children
Useful in modeling processes where a comparison or experiment has exactly two possible outcomes the test is performed repeatedly Examples multiple coin tosses Expert systems (yes/no questions) encoding/decoding messages in dots and dashes such as Morse code Expression tree (compiler generates)

14 Binary Trees A binary tree T is a finite set of nodes with one of the following properties: T is a tree if the set of nodes is empty (An empty tree is a tree.) The set consists of a root, R, and exactly two distinct binary trees, the left subtree TL and the right subtreeTR. The nodes in T consist of node R and all the nodes in TL and TR. Recursive definition

15 Binary Trees A B D E G H Right Subtree Left Subtree
Recursive definition Right Subtree G H Left Subtree

16 Binary Trees Trees with higher density are more desirable
Complete tree - all leaf node occupy the leftmost position in the tree. Largest possible complete tree with depth n is one that contains 2^n nodes at level n. B C D E F G H I K N o n - C o m p l e t e T r e e ( D e p t h 3 ) N o d e s a t l e v e l 3 d o n o t o c c u p y l e f t m o s t p o s i t i o n s

17 Tree Density Total nodes (for top levels) 2d - 1 23-1 = 7
C B G F J I H Total nodes (for top levels) 2d = 7 Additional Nodes 3 A E D C B G F 2^d -1 = … + 2^(d-1) d = level (where level 0 d = 1) A B C If totally complete tree: 2d d D complete tree: 2d = 2d

18 Tree Density If totally complete tree: 2d - 1 + 2d
B G F A B C If totally complete tree: 2d d D complete tree: 2d = 2d N nodes of complete tree satisfy inequality We are talking about complete trees here. 2d <= n <= 2d < 2d+1 d <= log2 n < d+1 Depth of n node tree: int (log2 n)

19 Array Representation of Binary Trees
Store the ith node in the ith location of the array

20 Array Representation of Binary Trees
Works OK for complete trees, not for sparse trees COMPLETE TREE – each level is filled out except possibily the bottom level – nodes are in the leftmost positions Example of incomplete and UNBALANCED tree. BALANCED TREE – the height of the left and right subtrees differ at by at most one. (height – number of levels.)

21 Linked Representation of Binary Trees
Pros: Uses space more efficiently Provides additional flexibility Con: more overhead Each node has two links one to the left child of the node one to the right child of the node if no child node exists for a node, the link is set to NULL

22 Linked Representation of Binary Trees
Example STOP – MARCH 17

23 Binary Trees as Recursive Data Structures
A binary tree is either empty … or Consists of a node called the root root has pointers to two disjoint binary (sub)trees called … right (sub)tree left (sub)tree Anchor Inductive step Which is either empty … or … Which is either empty … or …

24 Tree Traversal is Recursive
The "anchor" If the binary tree is empty then do nothing Else N: Visit the root, process data L: Traverse the left subtree R: Traverse the right subtree The inductive step Stopped 3/18/05

25 Traversal Order Three possibilities for inductive step:
Left subtree, Node, Right subtree the inorder traversal Node, Left subtree, Right subtree the preorder traversal Left subtree, Right subtree, Node the postorder traversal

26 Traversal Order Given expression A – B * C + D
Represent each operand as The child of a parent node Parent node, representing the corresponding operator

27 Traversal Order Inorder traversal produces infix expression A – B * C + D Preorder traversal produces the prefix expression A * B C D Postorder traversal produces the postfix or RPN expression A B C * - D + LNR – inorder (or infix) NLR – preorder (or prefix) LRN – postorder (or postfix)

28 Traversal Order Iterative Level-Order Scan Visit + Visit - D Visit A *
Visit B C How is this possible? Breadth-first search

29 Traversal Order Iterative Level-Order Scan Visit + Visit - D Visit A *
Visit B C How is this possible? Breadth-first search Use a queue as the intermediate storage device

30 Traversal Order Iterative Level-Order Scan Visit + Visit - D Visit A *
Visit B C Initialization Step: node<T> *root; queue<node<T> *> q; q.push(root); Iterative Step: Get node N pointer from Q (if Q empty exit) Delete a node N pointer from Q Visit the node N Push node N’s children onto Q Go to step 1 Breadth-first search

31 Binary Search Tree (BST)
Collection of Data Elements binary tree each node x, value in left child of x value in x in right child of x Basic operations Construct an empty BST Determine if BST is empty Search BST for given item Definition of Abstract Data Type: Binary Search Tree (BST)

32 Binary Search Tree (BST)
Basic operations (continued) Insert a new item in the BST Maintain the BST property Delete an item from the BST Traverse the BST Visit each node exactly once The inorder traversal must visit the values in the nodes in ascending order LPR

33 template <class elemType>
class BST { private: template<class T> struct nodeType T info; nodeType<T> *llink; nodeType<T> *rlink; }; public: bool isEmpty(); void inorderTraversal(); void preorderTraversal(); ... binaryTreeType(const binaryTreeType<elemType>& otherTree); binaryTreeType(); ~binaryTreeType();

34 nodeType<elemType> *root;
private: nodeType<elemType> *root; void copyTree(nodeType<elemType>* &copiedTreeRoot, nodeType<elemType>* otherTreeRoot); ... void destroy(nodeType<elemType>* &p); void inorder(nodeType<elemType> *p); void preorder(nodeType<elemType> *p); }; Why protected?

35 BST Traversals Public method Private recursive auxillary method
void inorderTraversal(); Private recursive auxillary method template<class elemType> void binaryTreeType<elemType>::inorder(nodeType<elemType> *p) { if(p != NULL) inorder(p->llink); cout<<p->info<<" "; inorder(p->rlink); }

36 BST Leaf Count Create a method that returns leaf count…

37 BST Leaf Count int binaryTreeType<elemType>::treeLeavesCount() {
return leavesCount(root); } template<class elemType> int binaryTreeType<elemType>::leavesCount(nodeType<elemType> *p) if (p != NULL) if (p->llink == NULL && p->rlink == NULL) return 1; else return (leavesCount(p->llink) leavesCount(p->rlink)); } else return 0;

38 BST Searches Search begins at root
If that is desired item, done If item is less, move down left subtree If item searched for is greater, move down right subtree If item is not found, we will run into an empty subtree

39 BST Searches Search begins at root
If that is desired item, done If item is less, move down left subtree If item searched for is greater, move down right subtree If item is not found, we will run into an empty subtree Remember: N nodes of complete tree satisfy inequality 2d <= n <= 2d < 2d+1 IF WE CONSIDER THE ORDER OF MAGNITUDE FOR THE BST THEN IT IS O(log n) (drop the 1 and assume the log is power of 2) d <= log2 n < d d is levels Depth of n node tree: int (log2 n) Max compares for complete tree: (int) log2 n + 1

40 Iterative Inserting into a BST
Insert function – Letter R Uses modified version of search to locate insertion location or already existing item Pointer parent trails search pointer locptr, keeps track of parent node Thus new node can be attached to BST in proper place R

41 (Recursive) Inserting into a BST
Insert function Base case Root pointer = null (empty) Create new node and return pointer to this new tree Recursive case If root pointer != null (not empty) Compare new item to root’s item If item > root item then insert into right subtree (insert root->rllink) If item < root item then insert into left subtree If item = root item then don’t insert 30 10 70 15

42 (Recursive) Inserting into a BST
30 10 70 15

43 Recursive Deletion Three possible cases to delete a node, x, from a BST 1. The node, x, is a leaf Stopped 3/20

44 Recursive Deletion 2. The node, x has one child

45 Recursive Deletion x has two children
Delete node pointed to by xSucc as described for cases 1 and 2 Replace contents of x with inorder successor K

46 Problem of Lopsidedness
Tree can be balanced each node except leaves has exactly 2 child nodes Completely balanced

47 Problem of Lopsidedness
Trees can be unbalanced not all nodes have exactly 2 child nodes

48 Problem of Lopsidedness
Trees can be totally lopsided Suppose each node has a right child only Degenerates into a linked list Processing time affected by "shape" of tree

49 Threaded Binary Search Trees
Use special links in a BST These threads provide efficient nonrecursive traversal algorithms Build a run-time stack into the tree for recursive calls

50 Threaded Binary Search Trees
Note the sequence of nodes visited Left to right A right threaded BST Whenever a node, x, with null right pointer is encountered, replace right link with thread to inorder successor of x Inorder successor of x is its parent if x is a left child Otherwise it is nearest ancestor of x that contains x in its left subtree Must have a null right pointer to be used in this scheme…

51 Threaded Binary Search Trees
Traversal algorithm 1. Initialize a pointer, ptr, to the root of tree 2. While ptr != null a. While ptr->left not null replace ptr by ptr->left b. Visit node pointed to by ptr c. While ptr->right is a thread do following i. Replace ptr by ptr->right ii. Visit node pointed to by ptr d. Replace ptr by ptr->right End while Speeds up the traversal of the Btree.

52 Threaded Binary Search Trees
public: DataType data; bool rightThread; BinNode * left; BinNode * right; // BinNode constructors // Default -- data part is default DataType value // right thread is false; both links are null BinNode() : rightThread(false, left(0), right(0) { } //Explicit Value -- data part contains item; rightThread // is false; both links are null BinNode (DataType item) : data(item), rightThread(false), left(0), right(0) { } }; // end of class BinNode declared Additional field, rightThread required


Download ppt "Gordon College Prof. Brinton"

Similar presentations


Ads by Google