Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,

Similar presentations


Presentation on theme: "Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,"— Presentation transcript:

1 Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees, called the left and right subtrees, which are disjoint from each other and from the root.

2 Binary Tree Example Definitions: Node – element of the tree
Root – top node Subtree – binary tree associated with the root node (left and right) Children – the root node of a node’s subtrees Edge – a link from a node to its children Parent – the node to which the current node is a child Ancestor, descendant

3 Binary Tree Example Path – a sequence of nodes n1,n2,…,nk s.t. ni is a parent of ni+1 for 1 <= i < k *Length of a path – k -1 *Depth – length of path from root to a node *Height – depth of the deepest node +1 *Level – all nodes of depth d Leaf – node with two empty children Internal node – node with at least one non-empty child *No standard definitions

4 Full and Complete Binary Trees
*Full binary tree: Each node is either a leaf or internal node with exactly two non-empty children. *Complete binary tree: If the height of the tree is d, then all leaves except possibly level d are completely full. The bottom level has all nodes to the left side. These terms can be hard to distinguish. Students will need to remember which is which, since this notation will be used several times during the course.

5 Full Binary Tree Theorem (1)
Theorem: The number of leaves in a non-empty full binary tree is one more than the number of internal nodes. Proof (by Mathematical Induction): Base cases: A full, non-empty binary tree with 0 internal nodes must have 1 leaf node. A full binary tree with 1 internal node must have two leaf nodes. Induction Hypothesis: Assume any full binary tree T containing n internal nodes has n+1 leaves. This theorem is important, because it helps us to calculate space requirements. It tells us how many nodes are internal and how many are leaf. We need to know this if we have separate implementations for internal and leaf nodes

6 Full Binary Tree Theorem (2)
Induction Step: Given tree T with n internal nodes, pick leaf node L and make it internal (by adding two children). T’ is still full. How many internal leaf nodes does it contain? We added two new leaf nodes, but lost one n – 1 = n + 2 We created one new internal node n + 1 T’ has n+1 internal nodes, and n+2 leaf nodes, or n’ internal nodes and n’+1 leaf nodes By induction, the theorem holds for n >= 0

7 Traversals (1) Any process for visiting the nodes in some order is called a traversal. Any traversal that lists every node in the tree exactly once is called an enumeration of the tree’s nodes.

8 Traversals (2) Preorder traversal: Visit each node before visiting its children. Postorder traversal: Visit each node after visiting its children. Inorder traversal: Visit the left subtree, then the node, then the right subtree. Level-order traversal: Visit all nodes in level 0, then level 1, then level 2, etc. Always visiting the left child first

9 Recursive Implementation
Pre-, post-, and in-order traversals are all easily written as recursive functions (or with a stack) Level-order requires an additional data structure (a queue)

10 What are the traversals?
Give the pre-, post-, in-, and level order traversal of the tree.

11 Counting Nodes We can count the number of nodes in a tree using a traversal. Key point – the number of nodes in a tree with root node r is the number of nodes in r’s left subtree plus the number of nodes in r’s right subtree plus 1 (for r)

12 Array Implementation This is a good example of logical representation vs. physical implementation. Since the complete binary tree is so limited in its shape (there is only one possible shape for n nodes), it is reasonable to expect that space efficiency can be achieved.

13 Array Implementation Parent(r) = (r-1)/2 if r <> 0 and r < n.
Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n. Parent(r) = (r-1)/2 if r <> 0 and r < n. Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n.

14 Binary Tree Implementation
Standard implementation (illustrating BinNodePtr class shown earlier). Leaf implementation is identical to internal node implementation, resulting in much wasted space due to null pointers.

15 Array Implementation This is a good example of logical representation vs. physical implementation. Since the complete binary tree is so limited in its shape (there is only one possible shape for n nodes), it is reasonable to expect that space efficiency can be achieved.

16 Array Implementation Parent(r) = (r-1)/2 if r <> 0 and r < n.
Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n. Parent(r) = (r-1)/2 if r <> 0 and r < n. Leftchild(r) = 2r + 1 if 2r + 1 < n. Rightchild(r) = 2r + 2 if 2r + 2 < n. Leftsibling(r) = r - 1 if r is even, r > 0, and r < n. Rightsibling(r) = r + 1 if r is odd and r + 1 < n.

17 Binary Search Trees BST Property: All elements stored in the left subtree of a node with value K have values < K. All elements stored in the right subtree of a node with value K have values >= K. Lists have a major problem: Either insert/delete on the one hand, or search on the other, must be (n) time. How can we make both update and search efficient? Answer: Use a new data structure.

18 BST Search Algorithm – search(K,T) for key K and tree T
if currnode is NULL, return false if K < currnode->value return search(K,currnode->left) else if K > currnode->value return search(K,currnode->right) else // must equal currnode return true

19 BST Insert (1)

20 BST Insert (2) Insert algorithm (no duplicates)
insert(K,T) – insert key K into Tree T Find leaf node L where search(K,T) fails If K < L->value add K as L’s left child else add K as L’s right child

21 BST Remove (1)

22 BST Remove Remove algorithm – Remove(K,T) – remove key K from tree T
find node N with K using search(K,T) if N is a leaf, remove and exit else find the smallest node in the right subtree of N, called n N’ exchange values for N and N’ remove N’

23 Cost of BST Operations Find: Insert: Delete:
All cost depth of the node in question. Worst case: (n). Average case: (log n). All cost depth of the node in question. Worst case: (n). Average case: (log n).

24 Heaps Heap: Complete binary tree with the heap property:
Min-heap: All values less than child values. Max-heap: All values greater than child values. The values are partially ordered. Heap representation: Normally the array-based complete binary tree representation.

25 Building the Heap (a) (4-2) (4-1) (2-1) (5-2) (5-4) (6-3) (6-5) (7-5) (7-6) (b) (5-2), (7-3), (7-1), (6-1) How to get a good number of exchanges? Induction leads to the following algorithm: Heapify the root’s subtrees, then push the root to the correct level.

26 Siftdown (1) For fast heap construction:
Work from high end of array to low end. Call siftdown for each item. Don’t need to call siftdown on leaf nodes. Siftdown(H) if(H->value > H->left->value and H > H->right->value) then return; else H’ = max(H->left, H->right) swap values(H,H’) siftdown(H’)

27 Siftdown (2)

28 Buildheap Cost i=1 Cost for heap construction: log n
 (i - 1) n/2i  n. i=1 (i-1) is number of steps down, n/2i is number of nodes at that level.

29 Remove Max Value RemoveMax(H) -- get the max element off heap H
maxelem = H->value; // Max is at the top H->value = last elem->value; shiftdown(H); return maxelem;

30 Priority Queues (1) A priority queue stores objects, and on request releases the object with greatest value. Example: Scheduling jobs in a multi-tasking operating system. The priority of a job may change, requiring some reordering of the jobs. Implementation: Use a heap to store the priority queue.

31 Priority Queues (2) To support priority reordering, delete and re-insert. Need to know index for the object in question.

32 Huffman Coding Trees ASCII codes: 8 bits per character.
Fixed-length coding. Can take advantage of relative frequency of letters to save space. Variable-length coding Build the tree with minimum external path weight. Z K F C U D L E 2 7 24 32 37 42 120

33 Huffman Tree Construction (1)

34 Huffman Tree Construction (2)

35 Assigning Codes Letter Freq Code Bits C 32 D 42 E 120 F 24 K 7 L U 37
Z 2 C: 1110 D: 101 E: 0 F: 1111 K: L: 110 U: 100 Z:

36 Coding and Decoding A set of codes is said to meet the prefix property if no code in the set is the prefix of another. Code for DEED: Decode : Expected cost per letter: Code for DEED: Decode: DUCK Expected cost: (1 * * * * * 9)/ 306 = 785/306 = 2.57


Download ppt "Binary Trees A binary tree is made up of a finite set of nodes that is either empty or consists of a node called the root together with two binary trees,"

Similar presentations


Ads by Google