340 likes | 485 Views
Trees. Trees. Root. leaf. Definition of Tree. A tree is a finite set of one or more nodes such that: There is a specially designated node called the root. The remaining nodes are partitioned into n>=0 disjoint sets T 1 , ..., T n , where each of these sets is a tree.
E N D
Trees Root leaf
CHAPTER 5 Definition of Tree • A tree is a finite set of one or more nodes such that: • There is a specially designated node called the root. • The remaining nodes are partitioned into n>=0 disjoint sets T1, ..., Tn, where each of these sets is a tree. • We call T1, ..., Tn the subtrees of the root.
Level and Depth Level 1 2 3 4 node (13) degree of a node leaf (terminal) nonterminal parent children sibling degree of a tree (3) ancestor level of a node height of a tree (4) 3 1 2 2 1 2 3 2 3 3 2 3 3 1 3 0 3 0 0 0 0 4 0 4 4 0
CHAPTER 5 Terminology • The degree of a node is the number of subtreesof the node • The degree of A is 3; the degree of C is 1. • The node with degree 0 is a leaf or terminal node. • A node that has subtrees is the parent of the roots of the subtrees. • The roots of these subtrees are the children of the node. • Children of the same parent are siblings. • The ancestors of a node are all the nodes along the path from the root to the node.
Representation of Trees • List Representation • ( A ( B ( E ( K, L ), F ), C ( G ), D ( H ( M ), I, J ) ) ) • The root comes first, followed by a list of sub-trees data link 1 link 2 ... link n How many link fields are needed in such a representation?
CHAPTER 5 data A right sibling left child D C B J I H G F E M L K Left Child - Right Sibling
CHAPTER 5 Binary Trees • A binary tree is a finite set of nodes that is either empty or consists of a root and two disjoint binary trees called the left subtreeand the right subtree. • Any tree can be transformed into binary tree. • by left child-right sibling representation • The left subtree and the right subtree are distinguished.
*Figure 5.6: Left child-right child tree representation of a tree A B C E D G F K H L M I J
CHAPTER 5 Abstract Data Type Binary_Tree structure Binary_Tree(abbreviated BinTree) is objects: a finite set of nodes either empty or consisting of a root node, left Binary_Tree, and right Binary_Tree. functions: for all bt, bt1, bt2BinTree, itemelement Bintree Create()::= creates an empty binary tree Boolean IsEmpty(bt)::= if (bt==empty binary tree) return TRUE else return FALSE
Class NodeBT{ NodeBT bTKi, bTKa; Object item; public NodeBT(NodeBt btki, Object it, NodeBt btka){ bTKi = btki; item = it; bTKa = btka; } } Class BinaryTree{ NodeBt node; public create(){node = null;} // public BinaryTree(){node = null;} public isEmpty(BinaryTree bt){ return bt== null;} public Object inOrder(BinaryTree bt){} public Object preOrder(BinaryTree bt){} public Object posOrder(BinaryTree bt){} }
B public Object inOrder(BinaryTree bt){ inOrder(btKi); printData() intOrder(btKa); } public Object preOrder(BinaryTree bt){ printData() preOrder(btKi) preOrder(btKa) } public Object posOrder(BinaryTree bt){ posOrder(btKi) posOrder(btKa) printData() } preOrder(bt) print- A preOrder(btKi) print-B preOder(btKi) print-D preOder(btKa) print-E preOrder(btKa) print-C
CHAPTER 5 BinTree MakeBT(bt1, item, bt2)::= return a binary tree whose left subtree is bt1, whose right subtree is bt2, and whose root node contains the data item Bintree Lchild(bt)::= if (IsEmpty(bt)) return error else return the left subtree of bt element Data(bt)::= if (IsEmpty(bt)) return error else return the data in the root node of bt Bintree Rchild(bt)::= if (IsEmpty(bt)) return error else return the right subtree of bt
CHAPTER 5 Samples of Trees A A A B B B C C F G D E D E H I Complete Binary Tree 1 2 Skewed Binary Tree 3 4 5
CHAPTER 5 Maximum Number of Nodes in BT • The maximum number of nodes on level i of a binary tree is 2i-1, i>=1. • The maximum nubmer of nodes in a binary tree of depth k is 2k-1, k>=1. i-1 Prove by induction.
CHAPTER 5 Relations between Number ofLeaf Nodes and Nodes of Degree 2 For any nonempty binary tree, T, if n0 is the number of leaf nodes and n2 the number of nodes of degree 2, then n0=n2+1 proof: Let n and B denote the total number of nodes & branches in T. Let n0, n1, n2 represent the nodes with no children, single child, and two children respectively. n= n0+n1+n2, B+1=n, B=n1+2n2 ==> n1+2n2+1= n, n1+2n2+1= n0+n1+n2 ==> n0=n2+1
CHAPTER 5 Full BT VS Complete BT • A full binary tree of depth k is a binary tree of depth k having 2 -1 nodes, k>=0. • A binary tree with n nodes and depth k is complete iff its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k. k A A B C B C F G G E D F D E N O M L J I K H H I Complete binary tree Full binary tree of depth 4
CHAPTER 5 Binary Tree Sequential Representations • If a complete binary tree with n nodes (depth =logn + 1) is represented sequentially, then forany node with index i, 1<=i<=n, we have: • parent(i) is at i/2 if i!=1. If i=1, i is at the root and has no parent. • left_child(i) ia at 2i if 2i<=n. If 2i>n, then i has noleft child. • right_child(i) ia at 2i+1 if 2i +1 <=n. If 2i +1 >n, then i has no right child.
CHAPTER 5 Sequential Representation [1] [2] [3] [4] [5] [6] [7] [8] [9] A A B B C C F G D E D E H I A B C D E F G H I (1) waste space (2) insertion/deletion problem [1] [2] [3] [4] [5] [6] [7] [8] [9] . [16] A B -- C -- -- -- D -- . E
Binary Search Trees A binary search tree is a binary tree storing keys (or key-value entries) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) External nodes do not store items An inorder traversal of a binary search trees visits the keys in increasing order 6 2 9 1 4 8 Binary Search Trees
Example Binary Searches • Find ( root, 2 ) root 5 10 5 > 2, left 2 = 2, found 10 > 2, left 5 > 2, left 2 = 2, found 2 45 5 30 30 2 25 45 10 25
Example Binary Searches • Find (root, 25 ) 5 10 10 < 25, right 30 > 25, left 25 = 25, found 5 < 25, right 45 > 25, left 30 > 25, left 10 < 25, right 25 = 25, found 2 45 5 30 30 2 25 45 10 25
Binary Search Tree Construction • How to build & maintain binary trees? • Insertion • Deletion • Maintain key property (invariant) • Smaller values in left subtree • Larger values in right subtree
Binary Search Tree – Insertion • Algorithm • Perform search for value X • Search will end at node Y (if X not in tree) • If X < Y, insert new leaf X as new left subtree for Y • If X > Y, insert new leaf X as new right subtree for Y • Observations • O( log(n) ) operation for balanced tree • Insertions may unbalance tree
Example Insertion • Insert ( 20 ) 10 10 < 20, right 30 > 20, left 25 > 20, left Insert 20 on left 5 30 2 25 45 20
Iterative Search of Binary Tree Node Find( Node n, int key) { while (n != NULL) { if (n.data == key) // Found it return n; if (n.data > key) // In left subtree n = n.left; else // In right subtree n = n.right; } return null; } Node n = Find( root, 5);
Recursive Search of Binary Tree Node Find( Node n, int key) { if (n == NULL) // Not found return( n ); elseif (n.data == key) // Found it return( n ); else if (n.data > key) // In left subtree return Find( n.eft, key ); else // In right subtree return Find( n.right, key ); } Node n = Find( root, 5);
Binary Search Tree – Deletion • Algorithm • Perform search for value X • If X is a leaf, delete X • Else // must delete internal node a) Replace with largest value Y on left subtree OR smallest value Z on right subtree b) Delete replacement value (Y or Z) from subtree • Observation • O( log(n) ) operation for balanced tree • Deletions may unbalance tree
Example Deletion (Leaf) • Delete ( 25 ) 10 10 10 < 25, right 30 > 25, left 25 = 25, delete 5 30 5 30 2 25 45 2 45
Example Deletion (Internal Node) • Delete ( 10 ) 10 5 5 5 30 5 30 2 30 2 25 45 2 25 45 2 25 45 Replacing 10 with largest value in left subtree Replacing 5 with largest value in left subtree Deleting leaf
Example Deletion (Internal Node) • Delete ( 10 ) 10 25 25 5 30 5 30 5 30 2 25 45 2 25 45 2 45 Replacing 10 with smallest value in right subtree Deleting leaf Resulting tree