2019, Fall Pusan National University Ki-Joune Li Trees 2019, Fall Pusan National University Ki-Joune Li
Tree Definition Hierarchical Structure Recursion Tree : Finite set of one or more nodes One Root A set of n (0) children, each of which is a tree (subtree) Hierarchical Structure Recursion Recursive way of definition Divide and Conquer a b c d e f (a(b(d,e),c(f)))
Some Terms Root Child and Parent Sibling Degree of Tree Level Leaf (or Terminal) Node Internal nodes Child and Parent Sibling Degree of Tree Maximum Branching Factor Level Depth of Tree: Maximum Level Internal nodes Root Level 1 Level 2 Level 3 a b c d e f Leaf nodes
Binary Tree Binary Tree Tree whose maximum branch factor = 2 A finite set of nodes empty or Root, left subtree, and right subtree a b c d e f
Some Properties of Binary Tree A binary tree T of depth k Maximum number of nodes on level i : 2i-1 Maximum number of nodes in T : 2k – 1 n0 = n2 + 1, where n0 is the number of leaf nodes and n2 is the number of nodes with two children Full Binary Tree A Binary Tree of depth k with 2k – 1 nodes No empty subtrees except nodes on level k Complete Binary Tree Nodes correspond to numberd from 1 to n a b d e c f g 1 2 4 5 3
Representation of Binary Tree : Array If a binary tree T with n nodes is complete and i is the node number of the i-th node, LeftChild(i) = 2i if 2i n, otherwise no left child RightChild(i) = 2i + 1 if 2i +1 n, otherwise no left child Only valid for complete binary graph 1 2 3 4 5 1 2 4 5 3 root Left child Right child Parent
Representation of Binary Tree : Linked Right Child Data Node Left Child Class BinaryTree { private: TreeNode *root; public: // Operations }; Class TreeNode { friend class BinaryTree; private: DataType data; TreeNode *LeftChild; TreeNode *RightChild; };
Binary Tree Operations : Traversal Prefix Infix Postfix Level Order X + Y * Z – X * Y - - + X * Y Z * X Y X + Y * Z – X * Y + * X Y Z * + X Y * - X * X Y Y Z
Preorder Traversal + A B + A B void BinaryTree::Preorder() { Preorder(root); } void BinaryTree::Preorder(TreeNode *node) { if(node!=NULL}{ cout << node->data; preorder(node->leftChild); preorder(node->rightChild); } + A B + A B
Level Order Traversal Not Recursive Queuing a b c d e f g Output a b c void BinaryTree::LevelOrder() { Queue *queue=new Queue; queue->add(root); TreeNode *p; while(queue->isEmpty()==NO) { p=queue->delete(); cout << p->data; queue->add(node->leftChild); queue->add(node->rightChild); } d e f g Output a b c Queue a b c d e f g
Tree Copy C C A B A B Equal: Same Way TreeNode* BinaryTree::Copy() { return Copy(root); } TreeNode* BinaryTree::Copy(TreeNode *node) { TreeNode* p=NULL; if(node!=NULL}{ p=new TreeNode; p->data=node->data; p->leftVhild=copy(node->leftChild); p->rightChild=copy(node->rightChild); } return p; C A B C A B Equal: Same Way Evaluation of propositional calculus
Binary Search Tree Binary Search Tree A kind of Binary Tree For each node n in a binary search tree Max(n.leftChild) ≤ n.data < Min(n.rightChild) 55 51 62 38 74 20 45 62 80 40 51
Binary Search Tree: Search Search by Recursion Search without Recursion TreeNode* BST::SearchBST(TreeNode *node,int key) { if(node==NULL) return NotFound; else if(key==node->data) return node; else if(key<node->data) return SearchBST(node->leftChild); else return SearchBST(node->rightChild); } TreeNode* BST::SearchBST(int key) { TreeNode *t=root; while(t!=NULL) { if(key==t->data) return t; else if(key < t->data) t=t->leftChild; else /* key > t->data) t=t->rightChild); } return NULL;
Binary Search Tree: Insertion Boolean BST::Insert(int key) { TreeNode *p=root; TreeNode *q=NULL; while(p!=NULL) { q=p; if(key==p->data) return FALSE; else if(key < p->data) p=p->leftChild; else /*key > p->data*/ p=p->rightChild; } TreeNode *t=new TreeNode(key); if(root==NULL) root=t; else if(key < q->data) q->leftChild=t; else q->rightChild = t; return TRUE; + 60 q 55 q p 38 74 p q 20 45 62 80 p 40 51 60
Binary Search Tree: Deletion Three cases Largest node 55 55 51 38 38 74 38 74 20 45 45 62 45 62 40 51 40 51 55 40 51 55 Case 1: Delete a Leaf Node Case 2: Delete a node with a child Case 3: Delete a node with two children
Heap Heap (Max Heap) Priority Queue A Complete Binary Tree DeleteQueue: Delete Max Element from Queue A Complete Binary Tree Data at a node is greater than the max of its two subtrees Represented by array (or pointers) 74 45 55 23 34 12
Heap: Insertion O(log2 n) + 60 74 74 45 55 45 55 23 34 12 23 34 12 60 Heap[++k]=60 74 Heap[ k/2 ]=55 45 55 O(log2 n) Heap[k]=60 23 34 12 60
Heap: Deletion O(log2 n) 74 12 45 55 45 55 45 55 34 23 12 34 23 12 34