天天看點

java資料結構-二分樹 前序周遊非遞歸實作、層序周遊、删除最小最大值、删除任意元素學習筆記(三) 前序非遞歸實作,使用棧層序周遊

前序非遞歸實作,使用棧

28依次進去

java資料結構-二分樹 前序周遊非遞歸實作、層序周遊、删除最小最大值、删除任意元素學習筆記(三) 前序非遞歸實作,使用棧層序周遊
//二分搜尋樹的非遞歸前序周遊
	public void preOrderNR(){
		Stack<Node> stack=new Stack<BST<E>.Node>();
		stack.push(root);//把節點進棧
		while(!stack.isEmpty()){//判斷不是為空執行
			Node cur=stack.pop();
			System.out.println(cur.e);
			
			if(cur.right !=null)
				stack.push(cur.right);
			if(cur.left !=null)
				stack.push(cur.left);
		}
	}
           

層序周遊

一層一層的周遊

java資料結構-二分樹 前序周遊非遞歸實作、層序周遊、删除最小最大值、删除任意元素學習筆記(三) 前序非遞歸實作,使用棧層序周遊
java資料結構-二分樹 前序周遊非遞歸實作、層序周遊、删除最小最大值、删除任意元素學習筆記(三) 前序非遞歸實作,使用棧層序周遊
//二分搜尋樹的層序周遊
	public void levelOrder(){
		Queue<Node> queue=new LinkedList<BST<E>.Node>();
		queue.add(root);
		while(!queue.isEmpty()){
			Node cur=queue.remove();
			System.out.println(cur.e);
			
			if(cur.left !=null)
				queue.add(cur.left);
			if(cur.right !=null)
				queue.add(cur.right);
		}
	}
           

繼續閱讀