830 likes | 838 Views
Dive into the fundamental concepts of binary and general trees, understanding their structures, components, and traversals, including recursive definitions, signatures, and implementations like Huffman coding. Explore the relationship between general and binary trees, along with key properties and traversal methods. Learn about important concepts like tree depth, height, and full binary trees.
E N D
Binary Tree and General Tree Chapter 9
Overview • Two-way decision making is one of the fundamental concepts in computing. • A binary tree models two-way decisions. • A hierarchy represents multi-way choices. • The general tree is an extension of the binary tree.
Learning Objectives • Describe a binary tree in terms of its structure and components, and learn recursive definitions of the binary tree and its properties. • Study standard tree traversals in depth. • Develop a binary tree class interface based on its recursive definition. • Learn about the signature of a binary tree and understand how to build a binary tree given its signature.
Learning Objectives • Understand Huffman coding, a binary tree-based text compression application, and use the binary tree class to implement Huffman coding. • Implement the binary tree class. • Study how tree traversals may be implemented non-recursively using a stack. • Describe the properties of a general tree.
Learning Objectives • Learn the natural correspondence of a general tree with an equivalent binary tree, and the signature of a general tree.
9.1.1 Components • A binary tree consists of nodes and branches. • A node is a place in the tree where data is stored. • There is a special node called the root. • “starting point” of the tree. • The nodes are connected to each other by links or branches. • A left branch or right branch. • Binary means that there are at most two choices. • A node is said to have at most two children. • A node that does not have any children is called a leaf. • Non-leaf nodes are called internal nodes.
9.1.1 Components • There is a single path from any node to any other node in the tree.
9.1.2 Position as Meaning • If-then-else tree • Represents an if-then-else construct in a program. • Every node in this tree is conditional expression that evaluates to yes or no. • If it evaluates to yes, the left branch (if any) is taken and if evaluates to no, the right branch (if any) is taken.
9.1.2 Position as Meaning • Expression tree • (f + ((a * b) - c)) Double-click to add graphics
Create Expression tree • 1- If the current token is a '(', add a new node as the left child of the current node, and descend to the left child. • 2- If the current token is in the list ['+','-','/','*'], set the root value of the current node to the operator represented by the current token. Add a new node as the right child of the current node and descend to the right child. • 3- If the current token is a number, set the root value of the current node to the number and return to the parent. • 4- If the current token is a ')', go to the parent of the current node.
let’s look at an example of the rules outlined above in action. We will use the expression (3+(4∗5)). • We will parse this expression into the following list of character tokens['(', '3', '+', '(', '4', '*', '5' ,')',')']. • Initially we will start out with a parse tree that consists of root node.
9.1.3 Structure • Structure • Two trees with the same number of nodes may not have the same structure.
9.1.3 Structure • Depth is the distance from the root. • Nodes at the same depth are said to be at the same level, with the root being at level zero. • The height of a tree is the maximum level (or depth) at which there is a node.
9.1.3 Structure • Full Binary Tree: A binary tree in which all of the leaves are on the same level and every nonleaf node has two children
9.1.3 Structure • (a), first three are strictly binary, but the fourth is not. first two are FULL binary tree • (b), first two are complete and the last two are not. • At level i, there can be at most 2i nodes. • Maximum number of nodes over all the levels.
Traversal Definitions • Preorder traversal: Visit the root, visit the left subtree, visit the right subtree • Inorder traversal: Visit the left subtree, visit the root, visit the right subtree • Postorder traversal: Visit the left subtree, visit the right subtree, visit the root
Overview • A binary tree possesses ordering property that maintains the data in its nodes in sorted order. • Since the search tree is a linked structure, entries may be inserted and deleted without having to move other entries over, unlike ordered lists in which insertions and deletions require data movement. • The AVL tree is a height-balanced binary search tree that delivers guaranteed worst-case search, insert, and delete times that are all O(log n)
Learning Objectives • Explore the motivation for binary search trees by learning about the comparison tree for binary search. • Use the comparison tree as an analytical tool to determine the running time of binary search. • Describe the binary search tree structure and properties. • Study the primary binary search tree operations of search, insert, and delete, and analyze their running times.
Learning Objectives • Understand a binary search tree class interface and use it in application examples. • Implement the binary search tree class with a binary tree class as the reused storage component. • Study the AVL tree structure properties, the search, insert, and delete operations, and their running times.
10.2 Binary Search Tree Properties • All three trees have the same set of keys. • Their structures are different, depending on the sequence of insertion or deletion.
10.3 Binary Search Tree Operations • Three foundational operations • Search • Insert • Delete
10.3.1 Search • The tree nodes are for real. • The target key is compared against the key at the root of the tree. • If they are equal, sucess. • If not, recusively search the appropriate child. • Search terminates with failure if an empty subtree is reached.
10.3.2 Insert • To insert a value, search must force a failure. • Item in inserted in the failed location. • A newly inserted node always becomes a leaf node in the search tree.
10.3.3 Delete • The value to be deleted is first located in the binary search tree. • Three possible cases. • Case a: X is a leaf node.
10.3.3 Delete • Case b: X has one child • Replace the deleted node with the child.
10.3.3 Delete • Case c: X has two children • Find the inorder predecessor, Y, of X. • Copy the entry at Y into X. • Apply deletion on Y. • Applying deletion on Y will revert to either case b or a since Y is guaranteed to not have a right subtree.
Running Times • Search: worst case • Tied to the worst possible shape a tree can attain. • Such a tree degenerates into sequential search. • O(n).
Running Times • Insertion: worst case • O(n) • Deletion: worst case • O(n)
Balancing • Keeping a binary search tree balanced allows the height never to exceed O(log n). • There are two popular ways of maintaining and constructing balanced binary search trees. • AVL tree. • red-black tree.
10.5.1 Example Treesort • inOrder traversal method invokes visitor.visit() when a node is visited.
10.5.2 Example: Counting Keys • Count the number of keys in a binary search tree that are less than a given key. • It is possible to simply examine every node of the tree.