Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees 2015, Fall Pusan National University Ki-Joune Li.

Similar presentations


Presentation on theme: "Trees 2015, Fall Pusan National University Ki-Joune Li."— Presentation transcript:

1 Trees 2015, Fall Pusan National University Ki-Joune Li

2 STEMPNU 2 Tree Definition  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 bc de f (a(b(d,e),c(f)))

3 STEMPNU 3 Some Terms Root  Leaf (or Terminal) Node  Internal nodes Child and Parent Sibling Degree of Tree  Maximum Branching Factor Level  Depth of Tree: Maximum Level a bc de f Root Leaf nodes Internal nodes Level 1 Level 2 Level 3

4 STEMPNU 4 Binary Tree  Tree whose maximum branch factor = 2  A finite set of nodes empty or Root, left subtree, and right subtree a bc de f

5 STEMPNU 5 Some Properties of Binary Tree A binary tree T of depth k  Maximum number of nodes on level i : 2 i-1  Maximum number of nodes in T : 2 k – 1  n 0 = n 2 + 1, where n 0 is the number of leaf nodes and n 2 is the number of nodes with two children Full Binary Tree  A Binary Tree of depth k with 2 k – 1 nodes  No empty subtrees except nodes on level k Complete Binary Tree  Nodes correspond to numberd from 1 to n a b de c fg 1 2 45 3

6 STEMPNU 6 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 45 3 root 0 1 2 3 4 5 Left child Right childParent

7 STEMPNU 7 Representation of Binary Tree : Linked Class BinaryTree { private: TreeNode*root; public: // Operations }; Class BinaryTree { private: TreeNode*root; public: // Operations }; Class TreeNode { friend class BinaryTree; private: DataTypedata; TreeNode *LeftChild; TreeNode*RightChild; }; Class TreeNode { friend class BinaryTree; private: DataTypedata; TreeNode *LeftChild; TreeNode*RightChild; }; Right Child Data Node Left Child

8 STEMPNU 8 Binary Tree Operations : Traversal Traversal  Prefix  Infix  Postfix  Level Order - + X* * XY X + Y * Z – X * Y - + X * Y Z * X Y X Y Z * + X Y * - YZ X + Y * Z – X * Y

9 STEMPNU 9 Preorder Traversal void BinaryTree::Preorder() { Preorder(root); } void BinaryTree::Preorder() { Preorder(root); } void BinaryTree::Preorder(TreeNode *node) { if(node!=NULL}{ cout data; preorder(node->leftChild); preorder(node->rightChild); } + A B +AB

10 STEMPNU 10 Level Order Traversal Not Recursive a b de c fg Queuing void BinaryTree::LevelOrder() { Queue *queue=new Queue; queue->add(root); TreeNode *p; while(queue->isEmpty()==NO) { p=queue->delete(); cout data; queue->add(node->leftChild); queue->add(node->rightChild); } a ab Output Queue c b de c fg

11 STEMPNU 11 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; } Tree Copy TreeNode* BinaryTree::Copy() { return Copy(root); } TreeNode* BinaryTree::Copy() { return Copy(root); } C A B C A B Equal: Same WayEvaluation of propositional calculus

12 STEMPNU 12 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 38 2045 74 6280 4051 62

13 STEMPNU 13 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 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 data) t=t->leftChild; else /* key > t->data) t=t->rightChild); } return NULL; }

14 STEMPNU 14 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 data) p=p->leftChild; else /*key > p->data*/ p=p->rightChild; } TreeNode *t=new TreeNode(key); if(root==NULL) root=t; else if(key data) q->leftChild=t; else q->rightChild = t; return TRUE; } 55 38 2045 74 6280 4051 + 60 p q p q p q 60

15 STEMPNU 15 Binary Search Tree: Deletion Three cases 38 2045 4051 55 38 45 74 62 405155 Case 1: Delete a Leaf Node Case 2: Delete a node with a child 55 38 45 74 62 405155 Case 3: Delete a node with two children 51 Largest node

16 STEMPNU 16 Heap Heap (Max Heap)  Priority Queue 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 34 55 12 23

17 STEMPNU 17 Heap: Insertion 74 45 34 55 12 23 + 60 74 45 34 55 12 23 60 74 45 34 55 12 23 60 Heap[++k]=60 Heap[  k/2  ]=55 Heap[k]=60 O(log 2 n)

18 STEMPNU 18 Heap: Deletion 74 45 34 55 12 23 45 34 55 12 23 45 34 55 12 23 45 34 55 12 23 O(log 2 n)


Download ppt "Trees 2015, Fall Pusan National University Ki-Joune Li."

Similar presentations


Ads by Google